Created
July 19, 2016 05:50
-
-
Save aadimator/370fb465f6732561089e84ed889129a2 to your computer and use it in GitHub Desktop.
The Last Digit of a Large Fibonacci Number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int get_fibonacci_last_digit(long long n) { | |
int first = 0; | |
int second = 1; | |
int res; | |
for (int i = 2; i <= n; i++) { | |
res = (first + second) % 10; | |
first = second; | |
second = res; | |
} | |
return res; | |
} | |
int main() { | |
int n; | |
std::cin >> n; | |
int c = get_fibonacci_last_digit(n); | |
std::cout << c << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment