一個畢達哥拉斯三元組是一個包含三個自然數的集合,a<b<c,滿足條件:
例如:32 + 42 = 9 + 16 = 25 = 52.
已知存在並且只存在一個畢達哥拉斯三元組滿足條件a + b + c = 1000。
找出該三元組中abc的乘積。
原題目鏈接:Problem 9
差不多也是暴力算
public class Problem9 { public static void main(String[] args) { int res = 1; lableA: for (int c = 1000; c > 1; c--) { for (int b = c - 1; b > 1; b--) { int a = 1000 - c - b; if (a > b || a< 0) break; if (isPythagoras(a, b, c)) { res = a * b * c; break lableA; } } } System.out.println(res); } private static Boolean isPythagoras(int a, int b, int c) { long p_a = Math.round(Math.pow(a, 2)); long p_b = Math.round(Math.pow(b, 2)); long p_c = Math.round(Math.pow(c, 2)); return p_a + p_b == p_c; } }