Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modulo of Division of Two Numbers

Tags:

algorithm

math

we know that

(A + B) % P = (A % P + B % P) % P (A * B) % P = (A % P * B % P) % P 

where P is a prime .

I need to calculate (A / B) % P where A,B can be very large and can overflow .

Does such kind of formula for modular arithmetic holds for (A / B) % P and (A - B) % P.

If not then please explain what the correct answer is.

I.e is it true that (A / B) % P = ((A % P) / (B % P)) % P?

I WAS TRYING TO CALULATE (N*(N^2+5)/6)%P where N can be as large as 10^15

here A=n*(n^2+5) can surely overflow for n=10^15

like image 220
Rahul Kumar Dubey Avatar asked Sep 02 '12 10:09

Rahul Kumar Dubey


People also ask

Can you use modulo division?

The modulus operator returns the remainder of a division of one number by another. In most programming languages, modulo is indicated with a percent sign. For example, "4 mod 2" or "4%2" returns 0, because 2 divides into 4 perfectly, without a remainder.

What comes first modulo or division?

Most programming languages adopt the convention that the modulo operator (denoted by % rather than mod ) occupies the same place in the order of operations as multiplication and division. Hence, it comes AFTER the operations in parentheses, but BEFORE addition and subtraction.

What is the modulo of 2 and 3?

2 mod 3 equals 2, since 2/3 = 0 with a remainder of 2. To find 2 mod 3 using the modulus method, we first find the highest multiple of the divisor, 3 that is equal to or less than the dividend, 2.

What does modulo (%) do?

Modulo is a math operation that finds the remainder when one integer is divided by another. In writing, it is frequently abbreviated as mod, or represented by the symbol %.


1 Answers

Yes, but it's different:

(a - b) mod p = ((a mod p - b mod p) + p) mod p  (a / b) mod p = ((a mod p) * (b^(-1) mod p)) mod p 

Where b^(-1) mod p is the modular inverse of b mod p. For p = prime, b^(-1) mod p = b^(p - 2) mod p.

Edit:

(N*(N^2+5)/6)%P

You don't need any modular inverses from this. Just simplify the fraction: N or N^2+5 will be divisible by 2 and 3. So divide them and then you have (a*b) mod P.

like image 77
IVlad Avatar answered Sep 22 '22 23:09

IVlad