-
Notifications
You must be signed in to change notification settings - Fork 10
/
pe039.java
35 lines (31 loc) · 1.09 KB
/
pe039.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
interface EulerSolution {
public String run();
}
public final class Main implements EulerSolution {
public static void main(String[] args) {
System.out.println(new Main().run());
}
public String run() {
int maxPerimeter = 0; //Initial default values
int maxTriangles = 0; // Set to 0 so that it get overwritten by any value grt 0
for (int p = 1; p <= 1000; p++) { //Bruteforce from 1 to 1000
int triangles = countSolutions(p);
if (triangles > maxTriangles) { //If the number of solutions found are more than the current max than replacement occurs
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) //Condition to check if the triangle satisifies the pythagoras formula
count++;
}
}
return count;
}
}