Current location - Trademark Inquiry Complete Network - Futures platform - How to carry out modular operation in C language
How to carry out modular operation in C language
Modular operation: a% p (or a modular p), which represents the remainder of a divided by p.

For example, given a positive integer p and an arbitrary integer n, there must be an equation: n = KP+r; Where k and r are integers, and 0 ≤ R.

The rules of modular operation are as follows:

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) % p .

4. A, B,% C = ((A% p)^b)% C. 。

Extended data:

Application scope of modular operation in c language;

1, parity discrimination

The discrimination between odd and even numbers is the most basic application of modular operation, which is very simple. As we all know, the integer n is modulo 2. If the remainder is 0, it means that n is even, otherwise it is odd.

2. Distinguish prime numbers

If a number has only two factors: 1 and itself, it is called a prime number. For example, 2, 3, 5 and 7 are prime numbers, but 4, 6, 8 and 9 are not. The latter is called a composite number or a composite number. Divide a natural number by a positive integer not greater than the square root of the natural number. If a natural number is divisible, it means that it is not a prime number.

3. Find the greatest common divisor

The most commonly used method to find the greatest common divisor is Euclid algorithm (also called division by turns), and its calculation principle depends on the theorem: gcd(a, b) = gcd(b, a mod b).

Baidu Encyclopedia: Modular Operation