aboutsummaryrefslogtreecommitdiff
path: root/entries/dewert99/fib.rs
blob: b1744d52ac5c9fc104b2f63450e9e29be69bc3d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const MAX: usize = 94;

const fn fibc<const N: usize>() -> [u64; N] {
    let mut res = [0; N];
    res[1] = 1;
    let mut i = 2;
    while i < N {
        res[i] = res[i - 1] + res[i - 2];
        i += 1;
    }
    res
}

static FIB: [u64; MAX] = fibc();

pub fn fib(n: usize) -> Option<u64> {
    if n > MAX {
        None // Fib of n wouldn't fit in 64-bits
    } else {
        Some(FIB[n])
    }
}