The so-called prime number is a number that cannot be divisible by other numbers except 1 and itself. It is called a prime number.
The program requires the user to specify the range for finding prime numbers, and then find all prime numbers that meet the requirements within this range, print out which ones are, and sum all prime numbers at the same time.
The procedure is as follows:
import random
lower = int(input("输入区间最小值: "))
upper = int(input("输入区间最大值: "))
sum=0
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)
sum+=num
print('{}到{}的素数之和为{}'.format(lower,upper,sum))
The execution is as follows: