Python 输出指定范围内的素数
Python 3 教程
· 2019-02-12 11:43:11
素数(prime number)又称质数,有无限个。除了1和它本身以外不再被其他的除数整除。
以下实例可以输出指定范围内的素数:
实例(Python 3.0+)
#!/usr/bin/python3
# 输出指定范围内的素数
# take input from the user
lower = int(input("输入区间最小值: "))
upper = int(input("输入区间最大值: "))
for num in range(lower,upper + 1):
# 素数大于 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
执行以上程序,输出结果为:
$ python3 test.py 输入区间最小值: 1 输入区间最大值: 100 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
点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html
Head First Design Patterns
Elisabeth Freeman、Eric Freeman、Bert Bates、Kathy Sierra、Elisabeth Robson / O'Reilly Media / 2004-11-1 / USD 49.99
You're not alone. At any given moment, somewhere in the world someone struggles with the same software design problems you have. You know you don't want to reinvent the wheel (or worse, a flat tire),......一起来看看 《Head First Design Patterns》 这本书的介绍吧!