package y2020.interview.huawei.gougushu; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * @Auther: xiaof * @Date: 2020/3/11 10:25 * @Description:勾股数元祖 素勾股数的个数 * * 勾股数,是由三个正整数组成的数组;能符合勾股定理 a*a + b*b = c*c ,(a, b, c) 的正整数解。如果 (a, b, c) 是勾股数, * 它们的正整数倍数,也是勾股数。如果 (a, b, c) 互质,它们就称为素勾股数。给定正整数N, 计算出小于或等于N的素勾股数个数。 * */ public class Main { public static List solution(int n, int m) { List res = new ArrayList(); for (int a = n; a <= m - 2; ++a) { for (int b = a + 1; b <= m - 1; ++b) { //求c double c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2)); long cz = (long) c; if (c - cz == 0 && c <= m && isPrim(a,b) && isPrim(a, (int) c) && isPrim(b, (int) c)) { res.add(new int[]{a, b, (int) c}); } else if (c > m) { break; } } } return res; } //判断a,b,c互质 public static boolean isPrim(int a, int b) { if(a < b) { int tmp = a; a = b; b = tmp; } int c; //辗转相除 while((c = a % b) != 0) { a = b; b = c; } return b == 1; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); List<int[]> res = solution(n, m); res.forEach(res1 -> { System.out.println(res1[0] + " " + res1[1] + " " + res1[2]); }); } }