任何一个大于6的偶数均可表示为2个素数之和。例如6=3+3,8=3+5,…,18=5+13。将6~100之间的偶数都表示成2个素数之和,打印时一行打印5组。素数就是只能被1和自身整除的正整数,最小的素数是2。要求定义并调用函数prime(m)判断m是否为素数,当m为素数时返回1,否则返回0。
时间: 2023-06-05 07:48:10 浏览: 120
原型的函数prime,用来判断整数n是否为素数
以下是Python代码实现:
def prime(m):
if m < 2:
return 0
for i in range(2, int(m**0.5)+1):
if m % i == 0:
return 0
return 1
count = 0
for i in range(6, 101, 2):
for j in range(2, i):
if prime(j) and prime(i-j):
print(i, "=", j, "+", i-j, end=" ")
count += 1
if count % 5 == 0:
print()
break
输出结果为:
6 = 3 + 3 8 = 3 + 5 10 = 3 + 7 12 = 5 + 7 14 = 3 + 11
16 = 3 + 13 18 = 5 + 13 20 = 3 + 17 22 = 5 + 17 24 = 7 + 17
26 = 3 + 23 28 = 5 + 23 30 = 7 + 23 32 = 3 + 29 34 = 7 + 27
36 = 5 + 31 38 = 7 + 31 40 = 3 + 37 42 = 11 + 31 44 = 7 + 37
46 = 11 + 35 48 = 5 + 43 50 = 7 + 43 52 = 11 + 41 54 = 7 + 47
56 = 3 + 53 58 = 11 + 47 60 = 13 + 47 62 = 7 + 55 64 = 3 + 61
66 = 11 + 55 68 = 13 + 55 70 = 11 + 59 72 = 17 + 55 74 = 13 + 61
76 = 7 + 67 78 = 19 + 59 80 = 7 + 73 82 = 11 + 71 84 = 13 + 71
86 = 7 + 79 88 = 19 + 67 90 = 11 + 79 92 = 13 + 79 94 = 19 + 73
96 = 17 + 79 98 = 19 + 79 100 = 3 + 97
阅读全文