-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLDSFibbonaci.cpp
50 lines (41 loc) · 1021 Bytes
/
LDSFibbonaci.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
#include <iostream>
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;
}
int get_fibonacci_last_digit_fast(long long n) {
if (n <= 1)
return n;
int previous = 0;
int current = 1;
for (int i = 0; i < n - 1; ++i) {
int tmp_previous = previous % 10;
previous = current % 10;
current = tmp_previous + current % 10;
}
return current % 10;
}
int fibonacci_sum_fast(long long n) {
int new_n = (n + 2) % 60;
int new_last = get_fibonacci_last_digit_fast(new_n);
if (new_last == 0) {
return 9;
} else {
return new_last - 1;
}
}
int main() {
long long n = 0;
std::cin >> n;
std::cout << fibonacci_sum_fast(n);
}