Hackerrank – Problem description
The problem description – Hackerrank.
Solution
The catch is to use a feature of chosen programming language, which allows us operation with big numbers (e. g. BigInteger
in Java)
I created solution in:
All solutions are also available on my GitHub profile.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.math.BigInteger; import java.util.Scanner; public class PowerDigitSum { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tests = Integer.parseInt(scanner.nextLine()); for (int i = 0; i < tests; i++) { BigInteger base = new BigInteger("2"); int exp = Integer.parseInt(scanner.nextLine().trim()); base = base.pow(exp); long result = sumDigits(base); System.out.println(result); } scanner.close(); } private static long sumDigits(BigInteger num) { long result = 0; char[] digits = num.toString().toCharArray(); for (char digit : digits) { result += Character.digit(digit, 10); } return result; } } |