-
Notifications
You must be signed in to change notification settings - Fork 0
/
p048.java
28 lines (25 loc) · 941 Bytes
/
p048.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
import java.math.BigInteger;
class p048 {
/*
* The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.
*
* Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.
*
* -----
*
* BigInteger can contain numbers up to 10^10 if we only use the last 10
* digits every time we calculate each i^i. Because every digit before the
* last 10 digits doesn't factor into the sum we want, we can ignore
* those digits.
*/
public static String selfPowersFirst10(int n) {
BigInteger sum = BigInteger.ZERO;
BigInteger modLastTen = BigInteger.TEN.pow(10);
for (int i = 1; i <= n; i++)
sum = sum.add(BigInteger.valueOf(i).modPow(BigInteger.valueOf(i), modLastTen));
return sum.mod(modLastTen).toString();
}
public static void main(String[] args) {
System.out.println(selfPowersFirst10(1000));
}
}