Skip to content

Instantly share code, notes, and snippets.

@pingiun
Created August 7, 2015 20:10
Show Gist options
  • Save pingiun/964713a8d567cd3583c0 to your computer and use it in GitHub Desktop.
Save pingiun/964713a8d567cd3583c0 to your computer and use it in GitHub Desktop.
Problem 2 of the Euler project
struct Fibonacci {
cur: i32,
prev: i32,
}
//! Problem 2 of the Euler project
impl Iterator for Fibonacci {
type Item = i32;
fn next(&mut self) -> Option<Self::Item>{
let ret = self.cur;
self.cur = self.prev + self.cur;
self.prev = ret;
Some(ret)
}
}
fn main() {
let fib = Fibonacci{cur: 1, prev: 1};
println!("{}", fib.take_while(|&x| x < 4000000)
.filter(|x| x % 2 == 0).fold(0, |a,b| a + b));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment