int PisanoLength(int m){
std::vector<int> v;
v.push_back(0);
v.push_back(1);
int i;
for(i = 2; ; i++){
v[i] = v[i - 1] + v[i - 2];
int a = v[i - 1] % m;
int b = v[i] % m;
if( a == 0 && b == 1)
break;
}
return (i - 2);
}
Hello, I am new to C++ and I have been trying to write a function to calculate the length of the Pisano Period. I have used the fact that once you hit 0 and 1 again, the sequence starts repeating itself so the index number before that 0 is the Pisano period length. But this one (the one which I wrote above) shows 'Dumping stack trace to pisano2.exe.stackdump' error (pisano2.cpp is the file name)
There are a couple of errors.
One, as already noted, is the access out of bounds of the vector
v
inside the loop.The other, sneakier, is that the module is applied after the elements are inserted, while applying it before storing the values avoids integer overflows.
Demo vs Failing demo