I have some basic code that solves a problem on project Euler, number 48 to be exact. Although it technically produces the answer I'd like my program to present the answer neatly, as such I'd like to print the last TEN digits to the console and omit everything else.
I've seen a couple of post suggesting that people use modulo operator on primitive data types and though I have no idea why returning a remainder could work as a solution I tried it anyway, to no avail.
System.out.println(GrandTotal.mod(BigInteger.TEN));
import java.math.BigInteger;
public class eulerNo48 {
public static void main(String[] args)
{
/**
* Loop that calculates the each number from 1 to 1000 to the
* power of itself and sums the results.
*/
BigInteger sum = BigInteger.ZERO;
BigInteger tempVar = BigInteger.ONE;
BigInteger GrandTotal = BigInteger.ZERO;
for (int i = 1; i <= 1000; i++) {
sum = tempVar.pow(i); // Calculates the value of tempVar(1 - 1000) to its respective power (i).
tempVar = tempVar.add(BigInteger.ONE); // Increments temVar for each iteration of the loop.
GrandTotal = GrandTotal.add(sum); // Adds sum to the grand total.
}
System.out.println(GrandTotal);
}
}
Any and all help is appreciated.