A method of detecting Python primes

Source: Internet
Author: User
Tags integer

This article mainly introduced the Python prime number detection method, the example analyzes the Python prime number detection related skill, needs the friend to be possible to refer to the next

This article illustrates the method of detecting Python primes. Share to everyone for your reference. Specifically as follows:

Factor Detection:

Detection factor, Time complexity O (n^ (1/2))

?

1 2 3 4 5 6 7 def is_prime (n): if n < 2:return false for I in xrange (2, int (n**0.5+1)): If n%i = 0:return False return True

Fermat's little theorem:

If n is a prime number, and A is any positive integer less than n, then the N-second square of A and a modulo n congruence

Implementation method:

Select a base (for example, 2), for a large integer p, if 2^ (p-1) and 1 are not modulo p with the remainder, p must not be prime; otherwise, p is probably a prime number.

2** (n-1)%n is not an easy to calculate number

Modular Operation Rules:

?

1 2 (a^b)% P = ((a% p) ^b)% P (A * b)% P = (a% p * b% p)% p

Calculate x^n (% P)

OK

If N is an even number, then X^n = (x*x) ^[N/2];

If N is odd, then x^n = x*x^ (N-1) = X * (x*x) ^[N/2];

?

1 2 3 4 5 6 7 def xn_mod_p (x, N, p): if n = = 0:return 1 res = xn_mod_p ((x*x)%p, n>>1, p) if n&1!= 0:res = (res*x)%p return Res

Can also be summed up as the following algorithm two functions are the same

?

1 2 3 4 5 6 7 8 def xn_mod_p2 (x, N, p): res = 1 N_bin = Bin (n) [2:] for I in range (0, Len (n_bin)): res = res**2% p if n_bin[i] = = ' 1 ': res = res * x% P return res

With the fast processing of the modular power operation, we can realize the pony detection

Fermat's test is accurate when giving a negative conclusion, but the positive conclusion may be wrong, the efficiency of large integers is very high, and the rate of miscalculation decreases with the increment of integers.

?

1 2 3 4 5 6 7 def fermat_test_prime (n): if n = = 1:return False if n = 2:return True res = xn_mod_p (2, n-1, n) Return res = = 1

Miller-rabin detection

Miller-rabin detection is a kind of widely used

Two-time detection theorem: If P is a prime number, and 0

Fermat's little theorem: a^ (p-1) ≡1 (mod p)

This is the Miller-rabin test method. Constantly extracting factor 2 from the exponential n-1, n-1 is represented as D*2^r (where D is an odd number). Then what we need to compute becomes the d*2^r of a, divided by the remainder of N. Thus, a^ (d * 2^ (R-1)) is either equal to 1 or equal to n-1. If a^ (d * 2^ (r-1)) equals 1, the theorem continues to apply to a^ (d * 2^ (r-2)) so that it is continuously opened until the 2^i mod n-1 or a^d is used up for an I satisfying a^ (d * n=1) mod n = n-1 or the final index of 2. In this way, the Fermat small theorem is strengthened to the following form:

Extract factor 2 as much as possible, n-1 as D*2^r, if N is a prime, then either A^d mod n=1, or some I make a^ (d*2^i) mod n=n-1 (0<=i

Theorem: If n is a prime number, A is a positive integer less than n, then N to the Miller test based on a, the result is true.

The miller test is performed k times and the error probability of treating composite numbers as primes is no more than 4^ (k)

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Def miller_rabin_witness (A, p): if p = = 1:return False if p = = 2:return True #p-1 = u*2^t solver U, t n = p-1 t = Int (math . Floor (Math.log (n, 2)) U = 1 while t > 0:u = n/2**t if n% 2**t = 0 and u% 2 = 1:break T = t-1 B1 = B2 = Xn_m OD_P2 (A, U, p) for I in range (1, T + 1): b2 = b1**2% p If b2 = 1 and B1!= 1 and B1!= (p-1): return False B1 = B2 If B1!= 1:return False return True def prime_test_miller_rabin (P, K): While k > 0:a = Randint (1, p-1) if not miller_r Abin_witness (A, p): return False k = k-1 return True

I hope this article will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.