Skip to content

Instantly share code, notes, and snippets.

@j-walk
Created January 16, 2018 03:39
Show Gist options
  • Save j-walk/3bae6caf58b7092952eedd8c63421792 to your computer and use it in GitHub Desktop.
Save j-walk/3bae6caf58b7092952eedd8c63421792 to your computer and use it in GitHub Desktop.
#[inline(three_and_five)]
fn three_and_five() -> usize {
(1..1000).filter(|x| { x % 3 == 0 || x % 5 == 0 }).sum()
}
#[inline(fib)]
fn fib() -> usize {
struct Fib {
curr : usize,
next : usize,
};
let fib = Fib { curr : 0, next : 1};
impl Iterator for Fib {
type Item = usize;
fn next(&mut self) -> Option<usize> {
let new_next = self.curr + self.next;
self.curr = self.next;
self.next = new_next;
Some(self.curr)
}
};
fib.take_while(|x| x < &4000000usize ).filter(|x| x % 2 == 0).fold(0, |x,y|x+y)
}
fn main() {
println!("problem 1: {}", three_and_five());
println!("problem 2: {}", fib());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment