There is a task "It is required to find the last digit of the n-th Fibonacci number." at http://acmp.ru/?main=task&id_task=623
My solution is (I do not know how ethical it is to put the solution code in public)
#include <iostream> #include <stdio.h> using namespace std; int main(){ int a=1, b=1, c=a+b, n; cin >> n; if (n <= 1) c = 1; else for (int i = 2; i <= n; i++){ c = (a + b) % 10; a = b; b = c; } cout << c % 10; return 0; } Result: Runtime 0.858 Memory 772 Kb
According to the table on the page http://acmp.ru/index.asp?main=bstatus&id_t=623&lang=CPP due to a small increase in memory consumption as it manages to invest in 1-2 seconds. On the one hand, I may have slipped the input number 1000, and the other 10.
Any ideas for better performance?
double. - VladD