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 #10.
This article features only an answer, because I’ve started writing from problem 14.
fn is_prime(number: i64) -> bool {
if number < 2 {
return false
}
let mut is_prime: bool = true;
let end = (number as f64).sqrt().floor() as i64;
for i in 2..end+1 {
if number % i == 0 {
is_prime = false;
break
}
}
is_prime
}
fn total_primes_for(num: i64) -> i64 {
let mut total = 0;
for i in 0..num {
if is_prime(i) {
total += i;
}
}
total
}
#[test]
fn total_primes_below_x() {
assert_eq!(total_primes_for(10), 17);
assert_eq!(total_primes_for(2_000_000), 142913828922);
}
The full solution is available on GitHub.