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 #35.
“How many circular primes are there below one million?”
The first step in solving this puzzle, is to steal is_prime
from many a previous Euler exercise. The next step is to write a method to test if a prime number is also a rotary prime number, my first iteration goes something like this:
fn rotary(number: u64) -> bool {
let mut chars = number.to_string().chars().collect::<Vec<char>>();
(0..chars.len()).all(|_| {
chars.rotate_left(1);
let rotated: u64 = chars
.iter()
.collect::<String>()
.parse()
.unwrap();
is_prime(rotated)
})
}
#[test]
fn test_rotary() {
assert_eq!(rotary(1970), false);
assert_eq!(rotary(197), true);
assert_eq!(rotary(19), false);
assert_eq!(rotary(2), true);
}
To solve the actual puzzle:
fn problem_35() -> usize {
(1..=1_000_000)
.filter(|n| is_prime(*n) && rotary(*n))
.collect::<Vec<u64>>()
.len()
}
#[test]
fn test_problem_35() {
assert_eq!(problem_35(), 55)
}
The answer is 55 prime numbers. Easy enough.
The full solution is available on GitHub.