2020-09-22 09:47:09 +08:00
|
|
|
package ProjectEuler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
|
2020-10-24 18:23:28 +08:00
|
|
|
*
|
|
|
|
* <p>a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
|
|
|
|
*
|
|
|
|
* <p>There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
|
|
|
|
*
|
|
|
|
* <p>link: https://projecteuler.net/problem=9
|
2020-09-22 09:47:09 +08:00
|
|
|
*/
|
|
|
|
public class Problem09 {
|
2020-10-24 18:23:28 +08:00
|
|
|
public static void main(String[] args) {
|
|
|
|
assert solution1() == 31875000;
|
|
|
|
}
|
2020-09-22 09:47:09 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
private static int solution1() {
|
|
|
|
for (int i = 0; i <= 300; ++i) {
|
|
|
|
for (int j = 0; j <= 400; ++j) {
|
|
|
|
int k = 1000 - i - j;
|
|
|
|
if (i * i + j * j == k * k) {
|
|
|
|
return i * j * k;
|
2020-09-22 09:47:09 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
}
|
2020-09-22 09:47:09 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
return -1; /* should not happen */
|
|
|
|
}
|
|
|
|
}
|