写一个 Python 程序打印从 1 到 100,或 1 到 n,或最小到最大的质数,并举例计算它们的总和。
使用 For 循环打印从 1 到 100 的质数的 Python 程序
这个 python 程序显示从 1 到 100 的质数。首先,我们使用 For 循环来迭代 1 到 100 个值之间的循环。在 for 循环中,我们使用了另一个 For 循环来检查数字是否可以被整除。如果为真,计数递增,break 语句跳过该数字。
接下来,if 语句检查计数是否为零,并且给定的数字不等于 1。如果是真的,它会打印数字,因为它是质数。
for Number in range (1, 101):
count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break
if (count == 0 and Number != 1):
print(" %d" %Number, end = ' ')
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
建议大家参考、 While 、质数、 if 语句、 break 语句文章,了解 Python 逻辑。
这个 python 程序让用户输入最小值和最大值,而不是疯狂地从 1 到 100 打印它们。接下来,它打印最小值和最大值之间的质数。
minimum = int(input(" Please Enter the Minimum Value: "))
maximum = int(input(" Please Enter the Maximum Value: "))
for Number in range (minimum, maximum + 1):
count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break
if (count == 0 and Number != 1):
print(" %d" %Number, end = ' ')
使用 While 循环打印从 1 到 100 的质数的 Python 程序
我们刚刚用 While 循环替换了上面 Python 质数示例中的 For 循环。
Number = 1
while(Number <= 100):
count = 0
i = 2
while(i <= Number//2):
if(Number % i == 0):
count = count + 1
break
i = i + 1
if (count == 0 and Number != 1):
print(" %d" %Number, end = ' ')
Number = Number + 1
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
这个显示从 1 到 N 的质数的程序同上。我们将 For 循环替换为 While 循环。
minimum = int(input(" Please Enter the Minimum Value: "))
maximum = int(input(" Please Enter the Maximum Value: "))
Number = minimum
while(Number <= maximum):
count = 0
i = 2
while(i <= Number//2):
if(Number % i == 0):
count = count + 1
break
i = i + 1
if (count == 0 and Number != 1):
print(" %d" %Number, end = ' ')
Number = Number + 1
Please Enter the Minimum Value: 100
Please Enter the Maximum Value: 250
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241
Python 程序返回从 1 到 100 的质数之和
这个程序找出 1 到 100 之间的质数,然后将这些值相加得到总和。
minimum = int(input(" Please Enter the Minimum Value: "))
maximum = int(input(" Please Enter the Maximum Value: "))
total = 0
for Number in range (minimum, maximum + 1):
count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break
if (count == 0 and Number != 1):
print(" %d" %Number, end = ' ')
total = total + Number
print("\n\nSum from %d to %d = %d" %(minimum, maximum, total))
Please Enter the Minimum Value: 10
Please Enter the Maximum Value: 150
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149
Sum from 10 to 150 = 2259
这个 Python 程序允许用户输入最小值和最大值,并找到总和。接下来,Python 返回最小值和最大值之间的质数之和
minimum = int(input(" Please Enter the Minimum Value: "))
maximum = int(input(" Please Enter the Maximum Value: "))
total = 0
Number = minimum
while(Number <= maximum):
count = 0
i = 2
while(i <= Number//2):
if(Number % i == 0):
count = count + 1
break
i = i + 1
if (count == 0 and Number != 1):
print(" %d" %Number, end = ' ')
total = total + Number
Number = Number + 1
print("\n\nSum = %d" %total)
Please Enter the Minimum Value: 1
Please Enter the Maximum Value: 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
Sum = 1060