python输出100以内的质数与合数实例代码
作者:新手老岳 时间:2021-01-30 20:47:52
具体代码如下所述:
__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以内的质数与合数实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:http://www.cnblogs.com/laoyueblogs/p/9278644.html
标签:python,输出,质数,合数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
常用ASCII 码对照表
2007-08-21 14:35:00
Excute后都需要返回一个RecordSet对象吗?
2009-10-28 18:28:00
Python Socket传输文件示例
2023-10-18 17:19:00
python traceback捕获并打印异常的方法
2022-11-06 10:35:30
如何运用python读写CSV文件
2021-11-13 04:35:36
![](https://img.aspxhome.com/file/2023/4/97674_0s.png)
Python统计词频并绘制图片(附完整代码)
2022-01-01 09:28:24
![](https://img.aspxhome.com/file/2023/7/110447_0s.jpg)
Python实现PS图像调整黑白效果示例
2022-08-06 09:15:13
![](https://img.aspxhome.com/file/2023/1/93781_0s.jpg)
关于Pyinstaller闪退的补救措施
2021-04-22 06:27:08
![](https://img.aspxhome.com/file/2023/5/80635_0s.jpg)
基于Python实现MUI侧滑菜单a标签跳转
2021-06-12 22:49:22
![](https://img.aspxhome.com/file/2023/1/70851_0s.jpg)
Python实现截屏的函数
2022-03-12 01:46:53
Go语言编程中字符串切割方法小结
2023-06-16 01:41:24
Win10搭建Pyspark2.4.4+Pycharm开发环境的图文教程(亲测)
2021-03-21 02:07:59
![](https://img.aspxhome.com/file/2023/6/99336_0s.png)
web程序员的思考
2009-08-04 13:10:00
保护SQL服务器的安全 用户识别问题
2008-12-24 15:26:00
![](https://img.aspxhome.com/file/UploadPic/200812/20081225122629996.jpg)
13个超级有用的 jQuery 内容滚动插件和教程
2011-08-10 19:10:08
利用Python绘制一个可爱的米老鼠
2022-01-29 14:34:30
浅谈Python数学建模之线性规划
2021-02-11 17:57:35
![](https://img.aspxhome.com/file/2023/5/82885_0s.png)
MySQL的root帐户密码重置方法
2007-08-24 15:53:00
python3实现无权最短路径的方法
2023-07-11 23:26:40
![](https://img.aspxhome.com/file/2023/2/76872_0s.png)
pytorch __init__、forward与__call__的用法小结
2023-09-04 13:20:47
![](https://img.aspxhome.com/file/2023/4/117284_0s.jpg)