【基础入门题】2021.11.06
求斜边长小于100的勾股数组,且三边两两互质(即两数的公质数只有1)。
方法一:
def prime(a,b): n = min(a,b) for i in range(2,n+1): if a%i==0 and b%i==0: return False return True res = [] for a in range(1,100): for b in range(1,100): for c in range(1,100): if prime(a,b) and prime(a,c) and prime(b,c) and a*a+b*b==c*c and a<b<c<=100: res.append((a,b,c)) for r in res: print(r)
(3, 4, 5)
(5, 12, 13)
(7, 24, 25)
(8, 15, 17)
(9, 40, 41)
(11, 60, 61)
(12, 35, 37)
(13, 84, 85)
(16, 63, 65)
(20, 21, 29)
(28, 45, 53)
(33, 56, 65)
(36, 77, 85)
(39, 80, 89)
(48, 55, 73)
(65, 72, 97)
方法二:
def prime(a,b): n = min(a,b) for i in range(2,n+1): if a%i==0 and b%i==0: return False return True res = [] for c in range(3,100): for b in range(1,c): for a in range(1,b): if prime(a,b) and prime(a,c) and prime(b,c) and a*a+b*b==c*c: print(a,b,c) res.append((a,b,c)) for r in res: print(r)
(3, 4, 5)
(5, 12, 13)
(8, 15, 17)
(7, 24, 25)
(20, 21, 29)
(12, 35, 37)
(9, 40, 41)
(28, 45, 53)
(11, 60, 61)
(33, 56, 65)
(16, 63, 65)
(48, 55, 73)
(36, 77, 85)
(13, 84, 85)
(39, 80, 89)
(65, 72, 97)