日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python程序:打印從1到100的質(zhì)數(shù)

創(chuàng)新互聯(lián)Python教程:

成都創(chuàng)新互聯(lián)主營(yíng)于洪網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶App定制開(kāi)發(fā),于洪h5微信小程序搭建,于洪網(wǎng)站營(yíng)銷推廣歡迎于洪等地區(qū)企業(yè)咨詢

寫(xiě)一個(gè) Python 程序打印從 1 到 100,或 1 到 n,或最小到最大的質(zhì)數(shù),并舉例計(jì)算它們的總和。

使用 For 循環(huán)打印從 1 到 100 的質(zhì)數(shù)的 Python 程序

這個(gè) python 程序顯示從 1 到 100 的質(zhì)數(shù)。首先,我們使用 For 循環(huán)來(lái)迭代 1 到 100 個(gè)值之間的循環(huán)。在 for 循環(huán)中,我們使用了另一個(gè) For 循環(huán)來(lái)檢查數(shù)字是否可以被整除。如果為真,計(jì)數(shù)遞增,break 語(yǔ)句跳過(guò)該數(shù)字。

接下來(lái),if 語(yǔ)句檢查計(jì)數(shù)是否為零,并且給定的數(shù)字不等于 1。如果是真的,它會(huì)打印數(shù)字,因?yàn)樗琴|(zhì)數(shù)。

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 、質(zhì)數(shù)、 if 語(yǔ)句、 break 語(yǔ)句文章,了解 Python 邏輯。

這個(gè) python 程序讓用戶輸入最小值和最大值,而不是瘋狂地從 1 到 100 打印它們。接下來(lái),它打印最小值和最大值之間的質(zhì)數(shù)。

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 循環(huán)打印從 1 到 100 的質(zhì)數(shù)的 Python 程序

我們剛剛用 While 循環(huán)替換了上面 Python 質(zhì)數(shù)示例中的 For 循環(huán)。

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 

這個(gè)顯示從 1 到 N 的質(zhì)數(shù)的程序同上。我們將 For 循環(huán)替換為 While 循環(huán)。

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 的質(zhì)數(shù)之和

這個(gè)程序找出 1 到 100 之間的質(zhì)數(shù),然后將這些值相加得到總和。

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

這個(gè) Python 程序允許用戶輸入最小值和最大值,并找到總和。接下來(lái),Python 返回最小值和最大值之間的質(zhì)數(shù)之和

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

網(wǎng)頁(yè)題目:Python程序:打印從1到100的質(zhì)數(shù)
網(wǎng)站路徑:http://www.dlmjj.cn/article/djiodpc.html