forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp231.java
43 lines (33 loc) · 979 Bytes
/
p231.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
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Solution to Project Euler problem 231
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p231 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p231().run());
}
private static final int N = 20000000;
private static final int K = 15000000;
public String run() {
int[] smallestPrimeFactor = Library.listSmallestPrimeFactors(N);
long sum = factorialPrimeFactorSum(N, smallestPrimeFactor);
sum -= factorialPrimeFactorSum(K, smallestPrimeFactor);
sum -= factorialPrimeFactorSum(N - K, smallestPrimeFactor);
return Long.toString(sum);
}
private static long factorialPrimeFactorSum(int n, int[] smallestPrimeFactor) {
long sum = 0;
for (int i = 1; i <= n; i++) {
int j = i;
while (j > 1) {
int p = smallestPrimeFactor[j];
sum += p;
j /= p;
}
}
return sum;
}
}