I have made a code for computing nth Fibonacci number's mod m ie Fn%m but this code gives correct answers sometimes and incorrest in other cases. one particular case where the answer comes out to be incorrect is when n=1000 and m=100 where the actual answer is 75 and this code gives 71. Can anyone help me out?
#include <iostream>
#include<vector>
#define ull unsigned long long
ull fibmod(ull n, ull m)
{
ull temp,prev=0,curr=1,pp;
for(int i=0;i<=m*m;i++)
{
temp=prev;
prev=curr;
curr=(temp+curr)%m;
std::cout<<curr<<" ";
if(prev==0&&curr==1)
{
pp=i+1;
break;
}
}
n=n%pp;
//std::cout<<pp<<" "<<n<<"\n";
prev=0;
curr=1;
if(n==0)
return(0);
else if(n==1)
return(1);
else
{
for(int i=0;i<n-1;i++)
{
temp=prev;
prev=curr;
curr=temp+curr;
}
return(curr%m);
}
}
int main() {
unsigned long long n, m;
std::cin >> n >> m;
std::cout << fibmod(n, m) << "\n";
}