I had the doubt in finding the last digit of sum of terms of fibonacci series ranging from index m to index n(Consider starting term to have index 0).
I have lots of different ways to solve the problem. But it is required to pass very long cases as well for ex m=2,n=82364572389 etc. But when I tried with this algorithm, mine some test cases were passed but some didn't.
S can you help me out that is there any problem in my Code or is this Algorithm wrong.
Also how to do this Problem with better approach.
#include <iostream>
using namespace std;
long long calc_fib(long long n) {
n = (n+2)%60;
int fib[n+1];
fib[0]=0;
fib[1]=1;
int res = 1;
for(int i = 2; i<=n;i++){
fib[i] = (fib[i-1]%10 + fib[i-2]%10)%10;
// res = res + fib[i];
}
// cout<<fib[n]<<"\n";
if(fib[n] == 0){
return 9;
}
return (fib[n]%10-1);
}
int main() {
long long n = 0,m;
std::cin >> m;
std::cin >> n;
std::cout << calc_fib(n)-calc_fib(m-1) << '\n';
return 0;
}
Test cases
Test Case: 5 10
Correct Output: 6
My Output: -4
Test Case: 1 10000000
Correct Output: 5
My Output: 5