# Python 查找孪生素数

55 篇文章 1 订阅

（什么是孪生素数：3和5、5和7【间隔一个数字的素数对】）

if __name__ == "__main__":
n = int(input("how many twins do you what?:"))
for i in range(1, n+1):
print(get_twins(i))


def is_prime(n):
for i in range(2 , n//2+1):
if n % i == 0:
return False
return True


def is_twins(n):
return is_prime(n) and is_prime(n+1)


def get_twins(ord):
n = 1
while ord > 0:
n += 2
while not is_twins(n):
n += 2
ord -= 1
return n,n+2


#----------------------------------------------------------------------
def is_prime(n):
for i in range(2 , n//2+1):
if n % i == 0:
return False
return True

#----------------------------------------------------------------------
def is_twins(n):
""""""
return is_prime(n) and is_prime(n+2)

#----------------------------------------------------------------------
def get_twins(ord):
n = 1
while ord > 0:
n += 2
while not is_twins(n):
n += 2
ord -= 1
return n,n+2

if __name__ == "__main__":
n = int(input("how many twins do you what?:"))
for i in range(1, n+1):
print(get_twins(i))


how many twins do you what?:5
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)


06-03 73
12-29 3402
12-11 8892
04-12 6543
02-25
03-05 637

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

©️2022 CSDN 皮肤主题：技术黑板 设计师：CSDN官方博客

Martin の Blog

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。