[Tutor] To find the least number divisible by all numbers from 1-20

Alan Gauld alan.gauld at btinternet.com
Tue Aug 13 19:19:50 CEST 2013


On 13/08/13 17:45, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote:

> def check(num):
>    lis=[20,19,18,17,16,14,13,11]  #because a no. divisible by 20 is
>    for i in lis:
>      if num%i==0:
>        continue
>      else:
>        return False
>        break
>
>    return True

This is a bit convoluted. All you need is

     for i in lis:
       if num%i != 0:
         return False
       return True

> def main():
>    num=2520  # Because we can start from the no. divisible by 1-10
>    while not check(num):
>      print(num)
>      num+=2    # Because num has to be even

Surely you can increment by 20 since that's the next number that is 
divisible by 20.

eg if we start at 20 the next number divisible by 20 will be 40, not 
22... That should speed up the search considerably!

But my brain is nagging me that there should be a
cleverer mathematical trick too, but I can't remember
what it is... I'm sure one of our math gurus will tell us! :-)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list