Fibonacci gen from iterator

Published 331d ago

 Rob

Generate n fib numbers from an iterator

fn fib(n: f64) -> f64 {
    let sqrt5 = 5f64.sqrt();
    let phi = (1. + sqrt5) / 2.;
    (phi.powf(n) - ((1. - phi).powf(n))) / sqrt5
}

fn main() {
    let v: Vec<i32> = (0..100).map(|i| (fib(i as f64) as i32)).collect();
    println!("{:?}\n", v);
}

0

Please login or sign up to comment and collaborate