Created
October 15, 2017 09:37
-
-
Save javamultiplex/1e9451d987b3db4feedaee44f4824e77 to your computer and use it in GitHub Desktop.
Project Euler Problem 16 - Power digit sum in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.projecteuler; | |
import java.math.BigInteger; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category Project Euler Problems | |
* @Problem 16 - Power Digit Sum. | |
* | |
*/ | |
public class Problem16 { | |
public static void main(String[] args) { | |
int a = 2; | |
int b = 1000; | |
// Converting Integer to BigInteger. | |
BigInteger number = new BigInteger(String.valueOf(a)); | |
// Getting 2^1000. | |
BigInteger power = number.pow(b); | |
// Converting BigInteger to String. | |
String powerInString = String.valueOf(power); | |
int length = powerInString.length(); | |
int sum = 0; | |
int temp = 0; | |
for (int i = 0; i < length; i++) { | |
// Converting char to int. | |
temp = powerInString.charAt(i) - 48; | |
sum += temp; | |
} | |
System.out.println("The sum of the digits of the number 2^1000 is : " + sum); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment