日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Python程序:尋找質(zhì)數(shù)

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

目前創(chuàng)新互聯(lián)已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、噶爾網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

編寫(xiě)一個(gè) Python 程序,使用 For 循環(huán)、While 循環(huán)和函數(shù)來(lái)查找質(zhì)數(shù)。除 1 以外不能被任何其他數(shù)整除的自然數(shù),其本身稱(chēng)為質(zhì)數(shù)。

質(zhì)數(shù):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、101、103、107、109 等。2 是唯一的偶數(shù)。

使用 For 循環(huán)尋找質(zhì)數(shù)的 Python 程序

該程序允許用戶(hù)輸入任何整數(shù)值。接下來(lái),這個(gè) Python 程序使用 For 循環(huán)檢查給定的數(shù)字是否是質(zhì)數(shù)。

Number = int(input(" Please Enter any Number: "))
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 is a Prime Number" %Number)
else:
    print(" %d is not a Prime Number" %Number)

在 for 循環(huán)中,有一個(gè) If 語(yǔ)句來(lái)檢查被 I 整除的值是否正好等于 0。如果條件為真,則計(jì)數(shù)值遞增,然后執(zhí)行中斷語(yǔ)句。接下來(lái),我們使用另一個(gè) If 語(yǔ)句來(lái)檢查 Count 是否為零,Num 是否不等于 1。

用戶(hù)在上面的 Python 程序中輸入整數(shù)來(lái)檢查質(zhì)數(shù)示例是 365

第一次迭代:對(duì)于 I 在范圍(2,365//2) 意味著,對(duì)于 I 在范圍(2,182.5)–條件為真 現(xiàn)在,檢查 if 條件–if(365% 2 = = 0)。如你所知,條件是假的 接下來(lái),我變成 3

對(duì)剩余的和迭代進(jìn)行同樣的操作

接下來(lái),進(jìn)入 Python If 語(yǔ)句。如果(計(jì)數(shù)== 0 & &個(gè)數(shù)!= 1 ).在上面的所有迭代中,如果條件失敗,那么計(jì)數(shù)值沒(méi)有從初始化 0 開(kāi)始增加。我們使用的是 365(不是零)。所以,條件是真,也就是質(zhì)數(shù)。

使用 While 循環(huán)尋找質(zhì)數(shù)的 Python 程序

這個(gè) Python 程序和上面的一樣。我們剛剛將上面 python 程序中的 For 循環(huán)替換為 While 。

Number = int(input(" Please Enter any Num: "))
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 is a Prime" %Num)
else:
    print(" %d is not a Prime" %Num)
 Please Enter any Num: 14
 14 is not a Prime
>>> 
 Please Enter any Num: 109
 109 is a Prime

用函數(shù)求質(zhì)數(shù)的 Python 程序

這個(gè) python 程序與第一個(gè)示例相同。然而,我們通過(guò)定義新的函數(shù)來(lái)分離邏輯。

def finding_factors(Number):
    count = 0

    for i in range(2, (Number//2 + 1)):
        if(Number % i == 0):
            count = count + 1
    return count

Num = int(input(" Please Enter any Num: "))

cnt = finding_factors(Num)

if (cnt == 0 and Num != 1):
    print(" %d is a Prime" %Num)
else:
    print(" %d is not a Prime" %Num)
 Please Enter any Num: 44
 44 is not a Prime
>>> 
 Please Enter any Num: 139
 139 is a Prime

網(wǎng)頁(yè)名稱(chēng):Python程序:尋找質(zhì)數(shù)
URL鏈接:http://m.5511xx.com/article/dpocsih.html