Euler 5
So the other night I was a bit bored and decided to do something to pass the time. I first came across Project Euler a while ago, but had never gone further than problem #1. Boredom is a great motivator and I went through problems #2 thru #9 last night and I decided to post my solutions in search of better ones. Feel free to comment with your suggestions.
Project Euler’s Problem #5 statement is —
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
The number must be the least common multiple of every integer from 1 through
20. Python’s integer lcm implements exactly that operation:
from math import lcm
answer = 1
for number in range(2, 21):
answer = lcm(answer, number)
print(answer)
This prints 232792560. Folding the LCM keeps only the prime powers required
by the numbers seen so far, with no brute-force search.