This article is part of a series where I'll be diving head first into the Project Euler puzzles. I want to document the challenge of solving such a puzzle and how I got to the answer. I want to prefix this by stating that I can't cheat for any of these challenges; with that I mean I can't look up any other implementations online. After the implementation, I will validate the answer by using this document or a similar sheet.
In this article I'll be solving: Project Euler #5.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
The lazy and expensive way of doing this is:
fn problem_5() -> u64 {
let mut start = 20;
loop {
if (1..=20).all(|x| start % x == 0) {
break start
}
start += 1
}
}
#[test]
fn test_problem_5() {
assert_eq!(problem_5(), 0);
}
Voila! The answer is 232792560.
But a smarter way is increasing start
by 20 each time, making the whole process 20 times faster.
The full solution is available on GitHub.