๐ŸŒŽ Eli Heuerโ€™s Blog

Project Euler in Rust: Problem One

January 9, 2018

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

fn question() {
    println!("Sum of all multiples of 3 or 5 below 1000.\n");
}

fn compute(bound: u32) -> u32 {
    (1..bound).filter(|&n| n % 3 == 0 || n % 5 == 0).sum()
}

fn solve(x: u32) -> String {
    compute(x).to_string()
}

fn main() {
    question();
    let answer = solve(1000);
    println!("{}", answer);
}