blob: 8ef7faa84774698530604c1596ff8d830e3fe711 (
plain)
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
|
#include <iostream>
//#include <gtest/gtest.h>
//int get_fibonacci_last_digit_naive(int n) {
// if (n <= 1)
// return n;
//
// int previous = 0;
// int current = 1;
//
// for (int i = 0; i < n - 1; ++i) {
// int tmp_previous = previous;
// previous = current;
// current = tmp_previous + current;
// }
//
// return current % 10;
//}
int get_fibonacci_last_digit_optimized(int n) {
if (n <= 1)
return n;
int fibLastDigitArray[n];
fibLastDigitArray[0] = 0;
fibLastDigitArray[1] = 1;
for (int i = 2; i < n + 1; i++) {
fibLastDigitArray[i] = (fibLastDigitArray[i - 1]
+ fibLastDigitArray[i - 2]) % 10;
}
return fibLastDigitArray[n];
}
//TEST(FibLastDigitTest, Zero) {
// ASSERT_EQ(get_fibonacci_last_digit_naive(0), 0);
//}
//
//TEST(FibLastDigitTest, One) {
// ASSERT_EQ(get_fibonacci_last_digit_naive(1), 1);
//}
//
//TEST(FibLastDigitTest, Three) {
// ASSERT_EQ(get_fibonacci_last_digit_naive(3), 2);
//}
//
//TEST(FibLastDigitTest, Forty) {
// ASSERT_EQ(get_fibonacci_last_digit_naive(40), 5);
// ASSERT_EQ(get_fibonacci_last_digit_optimized(40), 5);
//}
//
//TEST(FibLastDigitTest, ThreeThreeOne) {
// ASSERT_EQ(get_fibonacci_last_digit_optimized(331), 9);
// ASSERT_EQ(get_fibonacci_last_digit_optimized(327305), 5);
//}
int main() {
int n;
std::cin >> n;
int c = get_fibonacci_last_digit_optimized(n);
std::cout << c << '\n';
}
|