Linux质数合数的脚本,python输出100以内的质数与合数实例代码

python输出100以内的质数与合数实例代码 具体代码如下所述:

__author__ = 'Yue Qingxuan'

# -*- coding: utf-8 -*-

#求质数

p=[2]

for i in range(2,101):

for temp in range(2,i):

if i%temp==0:

break

print('i=',i,'temp=',temp)

elif temp==i-1:

p.append(i)

print('\n以下打印质数:')

print(p)#求合数

list=[]

for i in range (4,100):

for j in range(2,i):

if(i%j==0):

list.append(i)

break

#else:

#break

#list.append(i)

print('\n\n以下打印合数:')

print(list)本程序用python3运行,以下是程序输出结果:

以下打印质数:[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]以下打印合数:[4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99]

总结

以上所述是小编给大家介绍的python输出100以内的质数与合数实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值