Skip to main content

fibonacci/
lib.rs

1pub type IndexType = u64; // this is currently much larger than your mem/cpu limitation
2
3use std::ops::Mul;
4
5use gmp::mpz::Mpz;
6use rayon::join;
7
8pub fn fib(x: IndexType) -> Mpz {
9    if x < 3 {
10        Mpz::from(1)
11    } else {
12        if x % 2 == 1 {
13            let n = (x + 1) / 2;
14            let (a, b) = join(|| fib(n).pow(2), || fib(n - 1).pow(2));
15            a + b
16        } else {
17            let n = x / 2;
18            let (a, b) = join(|| fib(n), || fib(n - 1));
19            (b.mul(Mpz::from(2)) + &a) * a
20        }
21    }
22}