python3练习100题——012
今天继续,答案都通过py3测试。
原题链接:http://www.runoob.com/python/python-exercise-example12.html
题目:判断101-200之间有多少个素数,并输出所有素数。
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
我的代码:
def primes():
li=[]
for m in range(101,200):
state=1
x=int(m**0.5) #可以不用math.sqrt()比较省事~
for i in range(2,x+1):
if m%i==0: #注意是余数为0的数,才不是素数,需要break
state=0
break
if state==1:
li.append(m)
print("total:%d" %len(li))
print(li)
思考:
需要一个state变量,来判断状态。
在每个循环开始,state=1,也就是默认一个数的状态是1,表示它是素数。
只有在if语句判断成功的时候,state改为0,表示这个数不是素数了。if语句要循环判断很多次,可能其中只有1次满足了。设置state可以达到要求:只有一次满足就不是素数。
接下来再经过一个if语句,可以把所有state=1的数加入列表中。
python3练习100题——012的更多相关文章
- python3练习100题——003
今天继续-答案都会通过python3测试- 原题链接:http://www.runoob.com/python/python-exercise-example3.html 题目:一个整数,它加上100 ...
- python3练习100题——002
因为特殊原因,昨天没有做题.今天继续- 原题链接:http://www.runoob.com/python/python-exercise-example2.html 题目: 企业发放的奖金根据利润提 ...
- python3练习100题——004
继续做题-经过python3的测试 原题链接:http://www.runoob.com/python/python-exercise-example4.html 题目:输入某年某月某日,判断这一天是 ...
- python3练习100题——036
原题链接:http://www.runoob.com/python/python-exercise-example36.html 题目:求100之内的素数. 之前有类似的题,所以这次遇到觉得很容易了, ...
- python3练习100题——035
原题链接:http://www.runoob.com/python/python-exercise-example34.html 题目:文本颜色设置. 学习了一下python3 的文本颜色设置. 其实 ...
- python3练习100题——020
原题链接:http://www.runoob.com/python/python-exercise-example20.html 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半:再落下 ...
- python3练习100题——013
熟悉的水仙花数来了,,,... 原题链接:http://www.runoob.com/python/python-exercise-example13.html 题目:打印出所有的"水仙花数 ...
- python3练习100题——056
题目:画图,学用circle画圆形. 可以用turtle.circle画图. import turtle turtle.setup(0.6,0.6) turtle.pensize(3) turtle. ...
- python3练习100题——050
题目:输出一个随机数. 程序分析:使用 random 模块. import random print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数 pri ...
随机推荐
- linux系统安装及Centos7配置
- git签名设置
作用:只区分不同开发人员的身份 一.项目级别/仓库级别:仅在当前本地库范围内有效 签名设置用户名(UserName)和邮箱(User@email),邮箱可以是任意邮箱(无效邮箱也可以) git con ...
- 论文阅读笔记(二十二)【CVPR2017】:See the Forest for the Trees: Joint Spatial and Temporal Recurrent Neural Networks for Video-based Person Re-identification
Introduction 在视频序列中,有些帧由于被严重遮挡,需要被尽可能的“忽略”掉,因此本文提出了时间注意力模型(temporal attention model,TAM),注重于更有相关性的帧. ...
- java Spring boot Docker打包
https://blog.csdn.net/Stephanie_1/article/details/88831993
- B - Draw!
You still have partial information about the score during the historic football match. You are given ...
- Linux 命令之 linux 四剑客
Linux命令-- 四剑客 一:Linux命令 之 AWK 符号:^ 开头 $ 结尾 awk 是一种处理文本的语言,一个强大的文本分析命令! 1:提取文件中的每行的第二个 提取前文本中内容为 命令: ...
- C#中System.ServiceProgress报错
场景 在C#中检索本地计算机所有服务时,使用 System.ServiceProcess.ServiceController[] services = System.ServiceProcess.Se ...
- Exec msdb.dbo.sp_send_dbmail 参数详解(SQL Server 存储过程发邮件)
转载oriency755 发布于2012-12-04 11:34:45 阅读数 6870 收藏 sp_send_dbmail [ [ @profile_name = ] 'profile_name ...
- codeforces每日一题1-10
目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...
- 获取URL地址参数方法
//获取url参数 getQueryVariable(variable){ var query =decodeURIComponent(window.location.search.substring ...