簡體   English   中英

尋找能被 1 到 20 的所有數整除的最小數的程序

[英]Program to find the smallest number evenly divisible by all numbers from 1 to 20

以下是我試圖計算所述問題的解決方案的代碼。 但它什么都不返回。 我的代碼有什么問題?

flag = False
i = 20
while flag==False:
    c = 0    
    for x in range(1,21):
        if i%x==0:
            c = c + 1
    if c==20:
        print i
        flag = True
    i = i + 1 

這是我在python中的代碼。 我們可以像這樣簡單地做到這一點。 導入分數

def lcm(n):
    x=1
    for i in range(1, n+1):
        x = (x*i)/fractions.gcd(x,i)
    return x

ans = lcm(20)
print(ans)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM