diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-13 20:29:59 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-13 20:29:59 +0100 |
commit | 0f13a35dee0df21f4c1a387b50582a958c7bc439 (patch) | |
tree | d5e7e2d59ea9a709b660d94428ea951a80f085ba /01-algorithmic_toolbox/01-intro/05-fibonacci_huge/fibonacci_huge.cpp | |
parent | 13eab863735725f9a02cd4ae0b1c25725cc27569 (diff) | |
download | coursera-0f13a35dee0df21f4c1a387b50582a958c7bc439.zip coursera-0f13a35dee0df21f4c1a387b50582a958c7bc439.tar.gz |
Algorithms : complete 01-algorithmic_toolbox 01-intro
Diffstat (limited to '01-algorithmic_toolbox/01-intro/05-fibonacci_huge/fibonacci_huge.cpp')
-rw-r--r-- | 01-algorithmic_toolbox/01-intro/05-fibonacci_huge/fibonacci_huge.cpp | 53 |
1 files changed, 50 insertions, 3 deletions
diff --git a/01-algorithmic_toolbox/01-intro/05-fibonacci_huge/fibonacci_huge.cpp b/01-algorithmic_toolbox/01-intro/05-fibonacci_huge/fibonacci_huge.cpp index db0c074..8a9a61c 100644 --- a/01-algorithmic_toolbox/01-intro/05-fibonacci_huge/fibonacci_huge.cpp +++ b/01-algorithmic_toolbox/01-intro/05-fibonacci_huge/fibonacci_huge.cpp @@ -1,8 +1,55 @@ #include <iostream> -long long get_fibonaccihuge(long long n, long long m) { - //write your code here - return 0; +long long get_fibonaccihuge(long long n, long long m) +{ + long long a, b, c, i; //, r, g, R; + long long g; + + // length of pisano period pi(m) + a = 0; b = 1; + g = 0; + /* Fi mod m */ + for (i = 0; ; i++) { + c = a + b; + if (c >= m) + c -= m; + // start pattern is 011 + if (c == 0) + g = ((g == 0) ? 1 : 0); + else if (g == 1) + g = ((c == 1) ? 2 : 0); + else if (g == 2) { + if (c == 1) + break; + g = 0; + } + /* if (g == 2) { */ + /* i += 1; */ + /* break; */ + /* } */ + + a = b; + b = c; + } + + /* g = n % i; */ + g = (n / i); + g = n - (g * i); + + if (g <= 1) + return g; + + /* Fg mod m */ + a = 0; b = 1; + for (i = 1; i < g; i++) { + c = a + b; + if (c >= m) + c -= m; + a = b; + b = c; + } + + return b; } int main() { |