- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
43 lines (35 loc) · 1.16 KB
/
Copy pathMain.java
File metadata and controls
43 lines (35 loc) · 1.16 KB
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
/*
@author: mc-es
Problem 39
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
{20,48,52}, {24,45,51}, {30,40,50}
For which value of p ≤ 1000, is the number of solutions maximised?
Answer: 840
*/
publicclassMain {
publicstaticvoidmain(String[] args) {
intmaxSolutions = 0;
intmaxP = 0;
for (intp = 1; p <= 1000; p++) {
intsolutions = countRightAngleTriangles(p);
if (solutions > maxSolutions) {
maxSolutions = solutions;
maxP = p;
}
}
System.out.println("p value with the most solutions: " + maxP);
System.out.println("Number of solutions: " + maxSolutions);
}
publicstaticintcountRightAngleTriangles(intp) {
intcount = 0;
for (inta = 1; a <= p / 3; a++) {
for (intb = a; b <= (p - a) / 2; b++) {
intc = p - a - b;
if (a * a + b * b == c * c) {
count++;
}
}
}
returncount;
}
}