How to calculate the modulus of the form (a * b)% c?

How to calculate the modulus of the form (a * b)% c?

I want to calculate the multiplication module of two int numbers, where they are almost at the overflow stage ...

here c is also int

+5
source share
3 answers
(a * b) % c == ((a % c) * (b % c)) % c
+15
source

How about ((a % c) * (b % c)) % c? Depending on your architecture, this may be faster or slower than casting to a larger type.

+7
source

a c long long, .

((long long)a * (long long)b) % c
+5

All Articles