I'm currently trying to problem solve to the question 'What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?'
So far I've coded something up that seems to work, but takes a very long time. Furthermore, I'm having to huge amounts of 'and' statements within an if, which doesn't seem awfully efficient nor professional.
What can I do to optimise this code and make it tidier perhaps?
number = 1
result = 0
def divide(candidate):
if candidate % 2 == 0 and candidate % 3 == 0 and candidate % 4 == 0 and candidate % 5 == 0 and candidate % 6 == 0 and candidate % 7 == 0 and candidate % 8 == 0 and candidate % 9 == 0 and candidate % 10 == 0 and candidate % 11 == 0 and candidate % 12 == 0 and candidate % 13 == 0 and candidate % 14 == 0 and candidate % 15 == 0 and candidate % 16 == 0 and candidate % 17 == 0 and candidate % 18 == 0 and candidate % 19 == 0 and candidate % 20 == 0:
global result
result = 1
return 1
else:
global number
result = 0
number = number + 1
return 0
while result == 0:
divide(number)
print "The lowest number divisible by all integers between 1-20 is:", number
Just to clarify, this isn't homework, I'm self-teaching myself Python and am attempting some ProjectEuler problems as part of this.