Traditional Culture Encyclopedia - Traditional culture - How to know whether a large number is a prime number?

How to know whether a large number is a prime number?

For high-speed judgment? Miller-Rabin algorithm or aks algorithm.

1. It is agreed that x%y is the modulo y of x, that is, the remainder obtained by dividing x by y. When x < Y and x%y=x, all modulo operands are integers.

X y represents the y power of x, and the priority of power operation is higher than that of multiplication and division, and the priority of addition and subtraction is the lowest.

Look at x y/z like this, first calculate the power, then divide it.

A/B, called a divided by b, also called b divided by a.

If A%B=0, A is divisible by B, and B is divisible by A. ..

A*B means a times b or a times b, b times a, b times a … all the same.

2. Fermat's Last Theorem: If n is an arbitrary positive integer, p is a prime number, and n is not divisible by p (obviously n and p are coprime), then there is: n p% p = n (that is, the remainder of n divided by p power is n).

But I looked up a lot of information, and the formulas I saw were all like this:

(n (p- 1))% p = 1 After analysis, the two formulas are actually the same and can be obtained by mutual deformation.

The original formula can be changed to: (n p-n)% p = 0 (that is, the power of n minus n can be divisible by p, because Fermat's theorem knows that the remainder of the power of n divided by p is n). If you put one, N p becomes your n * (n (p- 1)), then (n)

(N*(N^(P- 1)- 1))%P=0

Please note the above formula, which means that n * (n (p- 1)- 1) can be divisible by p.

And because n * (n (p- 1)- 1) is definitely divisible by n (needless to say! )

So n * (n (p- 1)- 1) is the common multiple of n and p, and the knowledge of primary school is _

And because the premise is that n and p are coprime, and the least common multiple of coprime numbers is their product, they must exist.

The positive integer m makes the equation hold: n * (n (p-1)-1) = m * n * p.

Cut off n on both sides, which is simplified as: n (p-1)-1= m * p.

Because m is an integer, obviously: n (p- 1)- 1)% p = 0, that is: n (p- 1)% p = 1.

2. Fermat's Last Theorem: If n is an arbitrary positive integer, p is a prime number, and n is not divisible by p (obviously n and p are coprime), then there is: n p% p = n (that is, the remainder of n divided by p power is n).

But I looked up a lot of information, and the formulas I saw were all like this:

(n (p- 1))% p = 1 After analysis, the two formulas are actually the same and can be obtained by mutual deformation.

The original formula can be changed to: (n p-n)% p = 0 (that is, the power of n minus n can be divisible by p, because Fermat's theorem knows that the remainder of the power of n divided by p is n). If you put one, N p becomes your n * (n (p- 1)), then (n)

(N*(N^(P- 1)- 1))%P=0

Please note the above formula, which means that n * (n (p- 1)- 1) can be divisible by p.

And because n * (n (p- 1)- 1) is definitely divisible by n (needless to say! )

So n * (n (p- 1)- 1) is the common multiple of n and p, and the knowledge of primary school is _

And because the premise is that n and p are coprime, and the least common multiple of coprime numbers is their product, they must exist.

The positive integer m makes the equation hold: n * (n (p-1)-1) = m * n * p.

Cut off n on both sides, which is simplified as: n (p-1)-1= m * p.

Because m is an integer, obviously: n (p- 1)- 1)% p = 0, that is: n (p- 1)% p = 1.

2. Fermat's Last Theorem: If n is an arbitrary positive integer, p is a prime number, and n is not divisible by p (obviously n and p are coprime), then there is: n p% p = n (that is, the remainder of n divided by p power is n).

But I looked up a lot of information, and the formulas I saw were all like this:

(n (p- 1))% p = 1 After analysis, the two formulas are actually the same and can be obtained by mutual deformation.

The original formula can be changed to: (n p-n)% p = 0 (that is, the power of n minus n can be divisible by p, because Fermat's theorem knows that the remainder of the power of n divided by p is n). If you put one, N p becomes your n * (n (p- 1)), then (n)

(N*(N^(P- 1)- 1))%P=0

Please note the above formula, which means that n * (n (p- 1)- 1) can be divisible by p.

And because n * (n (p- 1)- 1) is definitely divisible by n (needless to say! )

So n * (n (p- 1)- 1) is the common multiple of n and p, and the knowledge of primary school is _

And because the premise is that n and p are coprime, and the least common multiple of coprime numbers is their product, they must exist.

The positive integer m makes the equation hold: n * (n (p-1)-1) = m * n * p.

Cut off n on both sides, which is simplified as: n (p-1)-1= m * p.

Because m is an integer, obviously: n (p- 1)- 1)% p = 0, that is: n (p- 1)% p = 1.

3. The product module decomposition formula has a lemma at first. If: X%Z=0, that is, x is divisible by z, then: (x+y)% z = y% z.

If there are three positive integers x, y and z, there must be: (x * y)% z = ((x% z) * (y% z))% z (y% z))% z.

It took a long time to prove it, but we have to discuss it according to the situation:

1. When both x and y are greater than z, there must be integers A and B to make the following equation hold:

X=Z*I+A( 1)

Y=Z*J+B(2)

Needless to say, this is the essence of modular operation!

Substituting (1) and (2) into (X*Y)modZ, we get: (Z*I+A)(Z*J+B))%Z, and then taking out the first three terms of z, it is transformed into: (z * (z * I * j+I * a).

Because Z*(Z*I*J+I*A+I*B) is an integer multiple of z ... Oh, here we go again.

According to the lemma, formula (3) can be simplified as: (A*B)%Z and becomes the original formula because: A=X%Z and b = y% z.

2. When x is greater than z and y is less than z, the same transformation:

X=Z*I+A

Substituting (X*Y)%Z gives:

(Z*I*Y+A*Y)%Z

According to lemma, it is transformed into: (a * y)% z.

Because A=X%Z and Y=Y%Z, you can get the original formula by substituting it into the above formula.

Similarly, when x is less than z and y is greater than z, the original formula also holds.

3. When X is less than Z and Y is less than Z, X=X%Z and Y=Y%Z, so the original formula holds.

=====================================================

4. A fast algorithm for calculating power, such as 2 13, requires 12 times multiplication in the traditional way.

[cpp] view plaincopyprint? /* calculate n p */? Unsigned power (unsigned n, unsigned p)? { ? for(int I = 0; I & ltp;; i++)n * = n; ? Returns n; ? } ? /* calculate n p */

Not signed? Power (unsigned? N, unsigned? p)

{

for(int? I = 0; I & ltp;; i++)? n * = n

Return? n;

Damn multiplication, it's time to optimize! Save the result of 2*2 and see if it becomes:

4*4*4*4*4*4*2

Then save the result of 4*4:16 *16 *16 * 2.

1* * 5 operations, which are 2*2, 4*4,16 *16 *.

In this way, our algorithm should only need to calculate less than half of the multiplication.

To clarify this algorithm, let's give another example, 2 7: 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2.

Separated in pairs: (2*2)*(2*2)*(2*2)*2

If 2*2 is used to calculate, then the index can be divided by 2, and there is nothing extra, and then multiplied separately later.

Divide by 2 again: ((2 * 2) * (2 * 2)) * (2 * 2) * 2.

In fact, the 2 * 2 in the last bracket is left this time. Then, multiply separately later. Now the index is 1, and the final result can be calculated: 16*4*2= 128.