i was doing a CS homework, the question is to get n!, where n <=10^6, and to fit it, i need n! % m,
i wrote the following code:
#include <bits/stdc++.h>
using namespace std;
int main ()
{
long long n, m = 7+10e9, fact = 1;//value of m is given
cin>>n;
n++;
while (--n)
{
fact = ((fact%m)*(n%m))%m;
cout<<fact<<endl;//added this of debugging
if (fact == 0) break;//and this also
}
cout <<fact;
return 0;
}
the code is fine with small numbers (i.e < 10^5) however when the numbers get bigger the fact output is 0, after printing the fact in each step, i realized that when for some reason the fact(x) == fact (x-1) (x is the step), the new fact is zero
for example when i entered 10^6, the fact got 8478216162 two times successively, and then got zero.
any help is thanked in advance