forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci_sum_last_digit.cpp
55 lines (43 loc) · 971 Bytes
/
fibonacci_sum_last_digit.cpp
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
#include <iostream>
#include<math.h>
#include<bits/stdc++.h>
int fibonacci_sum_naive(long long n) {
if (n <= 1)
return n;
long long previous = 0;
long long current = 1;
long long sum = 1;
for (long long i = 0; i < n - 1; ++i) {
long long tmp_previous = previous;
previous = current;
current = tmp_previous + current;
sum += current;
}
return sum % 10;
}
long long int fibonacci_sum_good(long long int n){
//n is a very very long number
//use pisano period
// According to it fibonacci numbers occur in periods of 60
n = (n + 2) % 60;
int num[n + 1];
int result = 1;
int i;
num[0] = 0;
num[1] = 1;
for(i = 2; i <= n; i++){
num[i] = ((num[i - 1] % 10) + (num[i - 2] % 10))%10;
}
if(num[n] == 0){
return 9;
}
else
{
return((num[n] % 10) - 1);
}
}
int main() {
long long n = 0;
std::cin >> n;
std::cout << fibonacci_sum_good(n);
}