So I am a new programmer, studying CS in university. I am new to Python and have been solving project euler puzzles, and I am wondering why my number 5 takes so long to compute! Looks like 287 seconds. I get the correct answer, but the compute time is very long. Can anyone explain to me why this is, and how I could better optimize it to run faster?
For anyone unfamiliar with project euler, this question is asking me to find the first positive number divisible by all numbers 1 through 20.
Edit: Thanks for all the help guys. I don't know how to comment on comments but your suggestions have been very helpful. Thanks!!
import time
def main():
time_start = time.clock()
x=2
while True:
if divBy20(x)==True:
print(x)
break
else:
x=x+1
time_elapsed = (time.clock() - time_start)
print(time_elapsed)
def divBy20(a):
for i in range(1,21):
if a%i!=0:
return False
return True
main()