# Large Factorials

i was solving problems for very large factorials , the way that i read was storing digits of each step in an array …!! but if i want to use the value in the array at any instant , suppose to apply Modulo operation, than how should i proceed further…??

It’ll be really great if you can mention your exact problem with an example or show some example code. However, I think you need this.

See if the number is lets say, 5234, then:

``````   5234= 1000*5 + 100*2 + 10*3 + 4
5234%m= (1000*5 + 100*2 + 10*3 + 4)%m;
= ((1000*5)%m + (100*2)%m + (10*3)%m + 4%m)%m
``````

So, basically break up the number and calculate the modulus value this manner:

``````for(i=digits-1;i>=0;i--)
{
ans= ans*10 + a[i]%m;
ans%=m;
}
``````
1 Like

You should have reference to Modular arithmetic you can start taking mode from Ones place i.e
(ones plac)*10^0%mod+(tens place)*10^1%mod+…+goes on
and at the last you have to take modulus of whole value…
Google Modular arithmetic to make yourself more comfortable with it

okay i will explain you my problem… suppose i am calculating some factorial and i got it’s value as 10^32 , whose digits are stored in array…! now if i want to compute the modulus of this value , than how can i do this…?

thanks @damn_me… finally i understood it now…

thanks @anh1l1ator