forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp006.java
More file actions
35 lines (27 loc) · 656 Bytes
/
p006.java
File metadata and controls
35 lines (27 loc) · 656 Bytes
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
/*
* Solution to Project Euler problem 6
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p006 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p006().run());
}
private static final int N = 100;
public String run() {
int sum = 0;
int sum2 = 0;
for (int i = 1; i <= N; i++) {
sum += i;
sum2 += i * i;
}
/*
* For the mathematically inclined:
* sum = N(N + 1) / 2.
* sum2 = N(N + 1)(2N + 1) / 6.
*/
return Integer.toString(sum * sum - sum2);
}
}