**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 maximized?

**Idea:**

By ensuring that one side is always at least equal to the other, I avoid duplicate answers (although since I’m just looking for the perimeter with the maximum number of solutions, if all are duplicated, it makes no difference). I also created an array of length 1001 (so the last index is 1000).

Just apply the pythagorean formula, see if the 3rd side is a whole integer, and if so, see if the perimeter is less than or equal to 1000. If so, increment the count of the index of that perimeter in the array. When done, find the index with the greatest count (and that’s the perimeter with the maximum number of solutions).

int answer = -1; int max = 0; int[] solutions = new int[1001]; // 0 - 1000 for(int i = 1; i < 500; i++) { for(int j = i; j < 500; j++) { int square = (i*i)+(j*j); int root = (int)Math.sqrt(square); if(root*root == square && i+j+root <= 1000) { solutions[i+j+root] += 1; } } } for(int i = 0; i < solutions.length; i++) { if(solutions[i] > max) { answer = i; max = solutions[i]; } }