forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp039.java
43 lines (35 loc) · 858 Bytes
/
p039.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 39
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p039 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p039().run());
}
public String run() {
int maxPerimeter = 0;
int maxTriangles = 0;
for (int p = 1; p <= 1000; p++) {
int triangles = countSolutions(p);
if (triangles > maxTriangles) {
maxTriangles = triangles;
maxPerimeter = p;
}
}
return Integer.toString(maxPerimeter);
}
private static int countSolutions(int p) {
int count = 0;
for (int a = 1; a <= p; a++) {
for (int b = a; b <= p; b++) {
int c = p - a - b;
if (b <= c && a * a + b * b == c * c)
count++;
}
}
return count;
}
}