public class Problem9 {
/*
* A Pythagorean triplet is a set of three natural numbers, a < b < c, for
* which, a2 + b2 = c2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists
* exactly one Pythagorean triplet for which a + b + c = 1000. Find the product
* abc.
*/
// it seems reasonable to assume that c > b >= a;
// 1000 ^ 3 is within the boundary of Integer.
int solve() {
for (int a = 1;; a++) {
for (int b = a; 2 * b < 1000 - a; b++) {
if (Math.pow(1000 - a - b, 2) == a * a + b * b) {
return a*b*(1000-a-b);
}
}
}
}
public static void main(String[] args) {
//31875000
System.out.println(new Problem9().solve());
}
}
for 문에서 조건확인을 우아하게 잘 썼음.