1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
pub type IndexType = u64; // this is currently much larger than your mem/cpu limitation

use std::ops::Mul;

use gmp::mpz::Mpz;
use rayon::join;

fn _fib(x: IndexType) -> Mpz {
    if x < 3 {
        Mpz::from(1)
    } else {
        if x % 2 == 1 {
            let n = (x + 1) / 2;
            let (a, b) = join(|| fib(n).pow(2), || fib(n - 1).pow(2));
            a + b
        } else {
            let n = x / 2;
            let (a, b) = join(|| fib(n), || fib(n - 1));
            (b.mul(Mpz::from(2)) + &a) * a
        }
    }
}


pub fn fib<T>(n: T) -> Mpz
where
    T: Into<IndexType>,
{
    return _fib(n.into());
}