SAJW
SAJW

Reputation: 235

Python how to shorten my code (not making it more effecient)

How do I make the if-ladder part below shorter?

(The task is to find the smallest multiple that can be evenly divided by all numbers from 1-20. My code is inefficient, and, as I learned completely obsolete because you can do this problem with multiplying the primefactors. But anyway how to make this shorter?)

a=20
b=0
while b<1:
    if (a%20==0 and
        a%19==0 and
        a%18==0 and
        a%17==0 and
        a%16==0 and
        a%15==0 and
        a%14==0 and
        a%13==0 and
        a%12==0 and
        a%11==0 and
        a%10==0 and
        a%9==0 and
        a%8==0 and
        a%7==0 and
        a%6==0 and
        a%5==0 and
        a%4==0 and
        a%3==0 and
        a%2==0):
        b=1
    else:
        a=a+1
print(a)

Upvotes: 1

Views: 42

Answers (1)

Kevin
Kevin

Reputation: 76184

if all(a%x==0 for x in range(2,21)):

Upvotes: 4

Related Questions