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
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
pub mod mat;

use std::ops::Neg;
use num_bigint::BigInt;
use mat::EuclideanDomain;
use mat::Exponent;

pub trait Index: EuclideanDomain + Exponent + PartialOrd + Neg<Output = Self> + Clone {}

impl Index for i64 {}
impl Index for BigInt {}

fn fibo<T: Index>(func: fn(T, T) -> T, n: T, m: T) -> T {
    if n < T::zero() {
        if (n.clone() & T::one()).is_zero() {
            -func(-n, m)
        } else {
            func(-n, m)
        }
    } else {
        func(n, m)
    }
}

fn fibo_rec_pos<T: Index>(n: T, m: T) -> T {
    if n.is_zero() {
        return T::zero();
    }
    if n.is_one() {
        return T::one();
    }
    (fibo_rec(n.clone() - T::one() - T::one(), m.clone()) + fibo_rec(n - T::one(), m.clone())) % m
}

/// Compute the n-th Fibonacci number modulo m using recurssion (F(n) <= F(n-1) + F(n-2)).
/// Since the recurrsion computes the same Fibonacci numbers multiple times,
/// it is very slow to compute.
/// The computation time is O(F(n)) = O(φ^n).
/// Maybe n = ~40 is the maximum value that this function can compute the Fibonacci number.
pub fn fibo_rec<T: Index>(n: T, m: T) -> T {
    fibo(fibo_rec_pos, n, m)
}

fn fibo_seq_pos<T: Index>(n: T, m: T) -> T {
    let mut a = T::zero();
    let mut b = T::one();
    let mut i = T::zero();
    while i != n {
        let t = (a.clone() + b.clone()) % m.clone();
        a = b;
        b = t;
        i += T::one();
    }
    a
}

/// Compute the n-th Fibonacci number modulo m using sequencial computation ((F(n), F(n-1)) <= (F(n-1) + F(n-2), F(n-1))).
/// The computation time is O(n).
pub fn fibo_seq<T: Index>(n: T, m: T) -> T {
    fibo(fibo_seq_pos, n, m)
}

fn fibo_mat_pos<T: Index>(n: T, m: T) -> T {
    let mut t = mat::Mat::one();
    let mut i = T::zero();
    while i != n {
        t = t.mul(&mat::Mat::q_matrix(), m.clone());
        i += T::one();
    }
    t.0.1
}

/// Compute the n-th Fibonacci number modulo m using a Q-matrix with sequential matrix
/// multiplication.
/// The computation time is O(n).
pub fn fibo_mat<T: Index>(n: T, m: T) -> T {
    fibo(fibo_mat_pos, n, m)
}

fn fibo_mat_req_pos<T: Index>(n: T, m: T) -> T {
    mat::exp_req(&mat::Mat::q_matrix(), n, m).0.1
}

/// Compute the n-th Fibonacci number modulo m using a Q-matrix with repeated squaring
/// (using recurrsion).
/// The computation time is O(log(n)).
pub fn fibo_mat_req<T: Index>(n: T, m: T) -> T {
    fibo(fibo_mat_req_pos, n, m)
}

fn fibo_mat_loop_pos<T: Index>(n: T, m: T) -> T {
    mat::Mat::q_matrix().pow(n, m).0.1
}

/// Compute the n-th Fibonacci number modulo m using a Q-matrix with repeated squaring
/// (using loops).
/// Slightly faster than `fibo_mat_req()` since there are no function call overhead.
/// The computation time is O(log(n)).
pub fn fibo_mat_loop<T: Index>(n: T, m: T) -> T {
    fibo(fibo_mat_loop_pos, n, m)
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn fibo_rec_10_1000_i64() {
        assert_eq!(fibo_rec(10i64, 1000i64), 55i64);
    }
    #[test]
    fn fibo_rec_minus10_1000_i64() {
        assert_eq!(fibo_rec(-10i64, 1000i64), -55i64);
    }
    #[test]
    fn fibo_rec_10_1000_big_int() {
        assert_eq!(fibo_rec(BigInt::from(10i64), BigInt::from(1000i64)), BigInt::from(55i64));
    }
    #[test]
    fn fibo_seq_1000_1000000000_i64() {
        assert_eq!(fibo_seq(1000i64, 1000_000_000i64), 849228875i64);
    }
    #[test]
    fn fibo_seq_minus1000_1000000000_i64() {
        assert_eq!(fibo_seq(-1000i64, 1000_000_000i64), -849228875i64);
    }
    #[test]
    fn fibo_seq_1000_1000000000_big_int() {
        assert_eq!(fibo_seq(BigInt::from(1000i64), BigInt::from(1000_000_000i64)), BigInt::from(849228875i64));
    }
    #[test]
    fn fibo_mat_1000000_1000_i64() {
        assert_eq!(fibo_mat(1000_000i64, 1000i64), 875i64);
    }
    #[test]
    fn fibo_mat_minus1000000_1000_i64() {
        assert_eq!(fibo_mat(-1000_000i64, 1000i64), -875i64);
    }
    #[test]
    fn fibo_mat_1000000_1000_big_int() {
        assert_eq!(fibo_mat(BigInt::from(1000i64), BigInt::from(1000_000_000i64)), BigInt::from(849228875i64));
    }
    #[test]
    fn fibo_mat_req_1000000_1000_i64() {
        assert_eq!(fibo_mat_req(1000_000i64, 1000i64), 875i64);
    }
    #[test]
    fn fibo_mat_req_minus1000000_1000_i64() {
        assert_eq!(fibo_mat_req(-1000_000i64, 1000i64), -875i64);
    }
    #[test]
    fn fibo_mat_req_1000000_1000_big_int() {
        assert_eq!(fibo_mat_req(BigInt::from(1000_000i64), BigInt::from(1000i64)), BigInt::from(875i64));
    }
    #[test]
    fn fibo_mat_loop_1000000_1000_i64() {
        assert_eq!(fibo_mat_loop(1000_000i64, 1000i64), 875i64);
    }
    #[test]
    fn fibo_mat_loop_minus1000000_1000_i64() {
        assert_eq!(fibo_mat_loop(-1000_000i64, 1000i64), -875i64);
    }
    #[test]
    fn fibo_mat_loop_1000000_1000_big_int() {
        assert_eq!(fibo_mat_loop(BigInt::from(1000_000i64), BigInt::from(1000i64)), BigInt::from(875i64));
    }
}