## Project Euler: Problem 39 – Integer right triangles

#### 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; // 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) {
max = solutions[i];
}
}```

## Project Euler: Problem 9 – Special Pythagorean triplet

#### Problem 9:

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

#### Idea:

For efficiency, we know that a + b + c = 1000, so a and b have to be less than 500. Using a double nested loop, just check to see if a2 + b2 = c2 where c2 is (1000-a-b)2. Since we’re told there is only one solution, we can stop once we find a case that works and multiply the variables together.

```int answer = -1;

for(int i = 0; i < 500; i++) {
for(int j = 0; j < 500; j++) {
if((i*i)+(j*j)==(1000-i-j)*(1000-i-j)) {