~joshleeb/euler

b396177e08e21da95f099bd87a74bda79480750c — Josh Leeb-du Toit 9 months ago d37fa54
Solve problem 10
2 files changed, 32 insertions(+), 0 deletions(-)

M src/main.rs
A src/p010.rs
M src/main.rs => src/main.rs +3 -0
@@ 8,6 8,7 @@ pub use p003::Problem as Problem3;
pub use p004::Problem as Problem4;
pub use p006::Problem as Problem6;
pub use p007::Problem as Problem7;
pub use p010::Problem as Problem10;
pub use solve::Solve;

use std::env;


@@ 20,6 21,7 @@ mod p003;
mod p004;
mod p006;
mod p007;
mod p010;
mod solve;

fn solve<T: Solve>() {


@@ 35,6 37,7 @@ fn main() {
        Some(4) => solve::<Problem4>(),
        Some(6) => solve::<Problem6>(),
        Some(7) => solve::<Problem7>(),
        Some(10) => solve::<Problem10>(),
        Some(_) => unimplemented!(),
        None => println!("usage: ./euler PROBLEM_NUM"),
    };

A src/p010.rs => src/p010.rs +29 -0
@@ 0,0 1,29 @@
use crate::Solve;
use primal::Primes;

/// Summation of primes
///
/// Since we are using an iterator over primes, we can simple iterate over all
/// elements below 2 million, and sum each element.
#[derive(Default)]
pub struct Problem;

impl Solve for Problem {
    type Solution = usize;

    fn solve(self) -> Self::Solution {
        Primes::all().take_while(|n| *n < 2_000_000).sum()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const SOLUTION: usize = 142913828922;

    #[test]
    fn solution() {
        assert_eq!(Problem::default().solve(), SOLUTION)
    }
}