According to the Project Euler website, “Project Euler is a series of challenging mathematical/computer programming problems…”
Always a fan of puzzles, I have decided to share my attempts and solutions here.
Note: If you are trying to solve Problem #5 for yourself, DO NOT READ AHEAD!
Problem #5
Problem Description
This problem asks for the smallest number that is evenly divisible by all of the numbers from 1 to 20.
Solution
As with Problem #1, this problem required the use of the modulo mathematical operator (%).
Essentially this solution is a loop that starts at 2520 (the smallest number divisible by 1-10) and increments by 2520. This is because no numbers that aren’t divisible by 2520 will not also be divisible by 1-10, so they certainly won’t be divisible by 1-20.
We check the modulo for 11-20 against the current number, and if any of them return a remainder, continue the loop. When we find a number divisible by 11-20, we know it is also divisible by 1-10, because it is divisible by 2520.
Code (PHP)
Final Number: 232,792,560