Created
March 20, 2014 13:47
-
-
Save wunki/9664090 to your computer and use it in GitHub Desktop.
Euler - Problem 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* | |
* 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 is_multiple(num: int) -> bool { | |
num % 3 == 0 || num % 5 == 0 | |
} | |
fn main() -> () { | |
let mut total = 0; | |
for n in range(0, 1000) { | |
if is_multiple(n) { | |
total += n | |
} | |
}; | |
println!("Total is {:d}", total) | |
} | |
#[test] | |
fn test_seven_is_not_multiple() { | |
if is_multiple(7) { | |
fail!("7 is not a multiple of 3 or 5"); | |
} | |
} | |
#[test] | |
fn test_five_is_multiple() { | |
if !is_multiple(5) { | |
fail!("Five is multiple of 5!") | |
} | |
} | |
#[test] | |
fn test_nine_is_multiple() { | |
if !is_multiple(9) { | |
fail!("Five is multiple of 9!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment