Project Euler #16: Power digit sum

  • + 0 comments

    Java8

    public static int sumDigit(BigInteger n) {
            int count = 0;
            while (n.compareTo(BigInteger.ZERO) > 0) {
                count += n.mod(BigInteger.TEN).intValue(); 
                n = n.divide(BigInteger.TEN); 
            }
            return count;
        }
    
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int testcase = s.nextInt();
            for (int i = 0; i < testcase; i++) {
                int n = s.nextInt();
                BigInteger res = BigInteger.valueOf(2).pow(n); 
                System.out.println(sumDigit(res));
            }
            s.close();
        }