Skip to content

Instantly share code, notes, and snippets.

@zsiciarz
Created April 27, 2014 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsiciarz/11352954 to your computer and use it in GitHub Desktop.
Save zsiciarz/11352954 to your computer and use it in GitHub Desktop.
Euler problem #1 in Rust
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