I have to take modulo like of (ab)%m and (a/b)%m. But the thing is **m** can be non-prime.

I studied fermats theorem but found out it was only for prime numbers only.

**(something like a^(p-2) = 1/a ( mod p ) , where p is prime)**

I found similar threads though, but that were not enough.

Take any example. Please help me.!

For (ab)%m :

It’s direct that you can do as (a%m * b%m)%m

For (a/b)%m :

If b is not too large I mean if (b*m) will not over flow … then you can do some thing like: call ‘M’ as b*m i.e M=b*m so (a/b)%m = (a%M)/b . For proof look Here

4 Likes

ok for (a/b)%m.

but for (ab)%m you said “It’s direct that you can do as (a%m * b%m)%m”. How can you be sure that a%m or b%m will not result in 0. As i said ‘m’ can be a non prime.

What if they result in 0 ?? as an example :

6%2 = 0 also

(2*3)%2 = (2%2 * 3%2)%2 = (0*1)%2 = 0

that’s what i asked…!!

what to do then.How to take mod,should i just print 0.

For questions like these, really trouble, when u’r asked to take mod (not to any prime no.) but taking modulo ‘m’ where m is entered by the user.