Eli Heuer’s Blog


Project Euler in Rust: Problem One

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!("Find the sum of all the 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);
}

Contact


Tags


Source Code


Licensing

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC_BY-SA 4.0) license.

Except where otherwise noted, all source code on this blog is licensed under the GPLv3 License.

© 2018


Feeds

Atom

Tux

This site was built with free software.