Java Implementation of Problem 9

View source code here on GitHub!

public class p0009 implements IEuler
Object answer()
Returns:

The answer to Project Euler problem 9

 1/*
 2Project Euler Problem 9
 3
 4
 5
 6Problem:
 7
 8A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
 9a**2 + b**2 = c**2
10
11For example, 3**2 + 4**2 = 9 + 16 = 25 = 5**2.
12
13There exists exactly one Pythagorean triplet for which a + b + c = 1000.
14Find the product abc.
15*/
16package euler;
17
18public class p0009 implements IEuler {
19    @Override
20    public Object answer() {
21        for (int c = 3;; c++) {
22            int c_square = c * c;
23            for (int b = 2; b < c; b++) {
24                int b_square = b * b;
25                for (int a = 1; a < b; a++) {
26                    int a_square = a * a;
27                    if (a_square + b_square == c_square && a + b + c == 1000)
28                        return a * b * c;
29                }
30            }
31        }
32    }
33}

Tags: pythagorean-triple