Created
April 27, 2014 18:57
-
-
Save zsiciarz/11352954 to your computer and use it in GitHub Desktop.
Euler problem #1 in Rust
This file contains 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
fn solution1 () -> int { | |
let mut sum = 0; | |
for i in range(1, 1000) { | |
if i % 3 == 0 || i % 5 == 0 { | |
sum += i; | |
} | |
} | |
sum | |
} | |
fn solution2 () -> int { | |
let mut numbers = range(1, 1000).filter(|i| i % 3 == 0 || i % 5 == 0); | |
numbers.fold(0, |acc, x| acc + x) | |
} | |
fn solution3 () -> int { | |
use std::iter::AdditiveIterator; | |
let mut numbers = range(1, 1000).filter(|i| i % 3 == 0 || i % 5 == 0); | |
numbers.sum() | |
} | |
fn main () { | |
println!("{}", solution1()); | |
println!("{}", solution2()); | |
println!("{}", solution3()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment