随笔:判断一个范围内有多少质数,分别是多少
思路:
1、负数都能被-1整除,所以负数都是合数
while True:
print('请输入一个整数范围')
num_min = input('请输入最小值:')
num_max = input('请输入最大值:')
s_min = int(num_min)
s = int(num_max)
Judge_prime_Numbers_List = []
for i in range(s_min, s+1):
for j in range(2, i+1):
if j < i:
if i % j == 0:
break
elif j == i:
Judge_prime_Numbers_List.append(i)
if Judge_prime_Numbers_List == []:
print('在[%s, %s]区间范围内没有质数' % (num_min, num_max))
else:
print('在[%s, %s]区间范围内有质数%s个,分别是:' % (num_min, num_max, len(Judge_prime_Numbers_List)), end='')
for value in Judge_prime_Numbers_List:
if value == Judge_prime_Numbers_List[-1]:
print(value)
else:
print(value, end=', ')
assert_y_n = input('是否继续输入y/n:')
if assert_y_n == 'n' or assert_y_n == 'N':
break
运行结果
请输入一个整数范围
请输入最小值:10
请输入最大值:100
在[10, 100]区间范围内有质数21个,分别是:11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
是否继续输入y/n:n
Process finished with exit code 0