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 #52.
“It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.”
I’ll reuse the unique_digits
method from the previous Euler puzzle and slightly alter it to be more in line with int_to_vec()
of “Power digit sum”. The next thing I did was to sort the digits that come out in the end.
To resolve problem 52:
fn problem_52() -> u64 {
let mut x = 1;
loop {
let dx = to_digits(x);
if (2..=6).all(|m| to_digits(x * m) == dx) {
break x
}
x += 1
}
}
#[test]
fn test_problem_52() {
assert_eq!(problem_52(), 142857);
}
Solved! This one was too easy.
The full solution is available on GitHub.