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?
Pretty straight forward. Iterate over possible values of p, a & b then test for the classic pythagorean equation. Sort the resulting list, then find the longest list of perimeters. Out pops the answer.
import java.util.*;
public class Euler39
{
public static void main(String[] args)
{
System.out.format(“Problem 39:\n”);
Euler39 e = new Euler39();
System.out.format(“Answer = %s\n”, e.Problem());
}
public String Problem ()
{
ArrayList<Integer> P = new ArrayList<Integer>();
for (int p = 3; p <= 1000; p++)
for (int a = 1; a <= 998; a++)
for (int b = 1; b <=998; b++)
if (p-a-b == Math.sqrt(b*b + a*a))
P.add(p);
Collections.sort(P);
int max = 0, maxNum = 0;
while (P.get(0) != 1000)
{
int number = P.get(0), count = 0;
while (P.get(0) == number)
{
P.remove(0);
count++;
}
if (count > max)
{
max = count;
maxNum = number;
}
}
return String.valueOf(maxNum);
}
}