📅  最后修改于: 2023-12-03 15:10:53.442000             🧑  作者: Mango
毕达哥拉斯素数(Pythagorean prime)是指可以写成 $p = a^2 + b^2$ 形式的质数 $p$。
一个正整数 $n$ 是否为毕达哥拉斯素数,需满足以下两个条件之一:
代码实现:
import math
def is_pythagorean_prime(n):
if n == 2:
return True
if n % 2 == 0:
return False
for i in range(3, int(math.sqrt(n))+1, 2):
if n % i == 0:
return False
return True if math.sqrt(n - 1) % 1 == 0 else False
代码说明:
assert is_pythagorean_prime(2) == True
assert is_pythagorean_prime(5) == True
assert is_pythagorean_prime(13) == True
assert is_pythagorean_prime(17) == True
assert is_pythagorean_prime(29) == True
assert is_pythagorean_prime(37) == True
assert is_pythagorean_prime(41) == True
assert is_pythagorean_prime(53) == True
assert is_pythagorean_prime(61) == True
assert is_pythagorean_prime(73) == True
assert is_pythagorean_prime(89) == True
assert is_pythagorean_prime(97) == True
assert is_pythagorean_prime(4) == False
assert is_pythagorean_prime(6) == False
assert is_pythagorean_prime(15) == False
assert is_pythagorean_prime(21) == False
assert is_pythagorean_prime(25) == False
assert is_pythagorean_prime(30) == False
assert is_pythagorean_prime(45) == False
assert is_pythagorean_prime(55) == False
assert is_pythagorean_prime(69) == False
assert is_pythagorean_prime(75) == False
assert is_pythagorean_prime(82) == False
assert is_pythagorean_prime(85) == False
以上测试样例输出均为 True。