The mod operation, that is, the remainder (modulus) operation, is an operation that finds the remainder of dividing an integer x by another integer y in integer operations, and does not consider the quotient of the operation. There is MOD operation in computer programming, and its format is: mod(nExp1,nExp2), which is the remainder after dividing two numerical expressions.
Modulo is mainly used in computer terminology. Remainder is more of a mathematical concept. Modular arithmetic is widely used in number theory and programming. From the identification of odd and even numbers to the identification of prime numbers, from modular exponentiation to the finding of the greatest common divisor, from Sun Tzu's problem to the Caesar cipher problem, modular arithmetic is everywhere. . (Recommended study:PHP video tutorial)
Although many number theory textbooks have a certain introduction to modular operations, most of them are based on pure theory. Modular operations are in programming There are not many applications involved.
Given a positive integer p and any integer n, there must be an equation:
Modulo operation: a % p (or a mod p), which means dividing a by the remainder of p.
Operation rules
Modular operation is somewhat similar to the basic four arithmetic operations, but division is an exception.The rules are as follows:
(a b) % p = (a % p b % p) % p (1)
(a - b) % p = (a % p - b % p) % p (2)
(a * b) % p = (a % p * b % p) % p (3)
a ^ b % p = ( (a % p)^b) % p (4)
Associative law:
((a b) % p c) % p = (a (b c) % p) % p (5 )
((a*b) % p * c)% p = (a * (b*c) % p) % p (6)
Commutative law:
(a b) % p = (b a) % p (7)
(a * b) % p = (b * a) % p (8)
Distributive law:
(a b) % p = ( a % p b % p ) % p (9)
((a b)% p * c) % p = ((a * c) % p ( b * c) % p) % p (10)
Important theorem
If a≡b (% p), then for any c, (a c) ≡ (b c) (%p); (11)
If a≡b (% p), then for any c, (a * c) ≡ (b * c) (%p); (12)
If a≡b (% p), c≡d (% p), then (a c) ≡ (b d) (%p), (a - c) ≡ (b - d) (%p ),
(a * c) ≡ (b * d) (%p); (13)
For more PHP related technical articles, please visitPHP Graphic Tutorialcolumn for learning!
The above is the detailed content of mod operation rules. For more information, please follow other related articles on the PHP Chinese website!