Fib big numbers!

Published 330d ago

 ayebear

Using the num_bigint crate we can generate much larger numbers with full accuracy.

use num_bigint::{BigUint, ToBigUint};

struct Fib(BigUint, BigUint);

impl Iterator for Fib {
    type Item = BigUint;
    fn next(&mut self) -> Option<Self::Item> {
        *self = Self(self.1.clone(), self.0.clone() + self.1.clone());
        Some(self.0.clone())
    }
}

fn fib() -> Fib {
    Fib(0.to_biguint().unwrap(), 1.to_biguint().unwrap())
}

fn main() {
    for (n, i) in fib().take(1000).enumerate() {
        println!("{n}: {i}");
    }
}

bigint
0

Please login or sign up to comment and collaborate