寫一個 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
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/239866.html