I am trying to find the sum of the digits of a number 2 ^ 1000, and for this I am using the Java class BigInteger. However, I could not make it real. In the end, I get 0 (zero) with the following code. What could be the problem?
Thank...
After Kon's help, I fixed the problem, but this time I get the wrong result. Can anyone see a problem with the algorithm?
public static void main(String []args) throws Exception
{
BigInteger big = BigInteger.valueOf(2).pow(1000);
BigInteger big2 = BigInteger.valueOf(0);
for(long i = 1; i<283; i++)
{
big2 = big2.add(big.mod(BigInteger.valueOf((long) Math.pow(10,i))).divide(BigInteger.valueOf((long)Math.pow(10,i-1))));
}
System.out.println(big2);
}
source
share