Python prime number Detection Method

Source: Internet
Author: User
This article mainly introduces the Python prime number detection method. The example analyzes the skills related to Python prime number detection. If you need a friend, refer to the example in this article to describe the Python prime number detection method. Share it with you for your reference. The details are as follows:

Factor Detection:

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

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

Ferma's theorem:

If n is a prime number and a is any positive integer less than n, then the Npower of a is the same as that of a modulo n.

Implementation Method:

Select a base number (for example, 2). For the big integer p, if 2 ^ (p-1) and 1 are not the same remainder of the modulo p, p must not be a prime number; otherwise, p is probably a prime number.
2 ** (n-1) % n is not an easy-to-calculate number.

Modulo operation rules:

(a^b) % p = ((a % p)^b) % p(a * b) % p = (a % p * b % p) % p

Calculate X ^ N (% P)

Yes
If N is an even number, X ^ N = (X * X) ^ [N/2];
If N is an odd number, then X ^ N = X * X ^ (N-1) = X * (X * X) ^ [N/2];

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

We can also conclude that the two functions in the following algorithm are the same.

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 Modulo-power operation, fast processing can enable the Fermat detection.

Ferma test is accurate when a negative conclusion is given, but the positive conclusion may be wrong. The efficiency of the big integer is very high, and the false positive rate decreases with the increase of the integer.

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 currently widely used.

Quadratic probe theorem: If p is a prime number and 0 Ferma's theorem: a ^ (p-1) limit 1 (mod p)

This is Miller-Rabin's method for the testing of the kernel. Continuously extract factor 2 from the exponent N-1 and express n-1 as d * 2 ^ r (where d is an odd number ). What we need to calculate is the remainder of d * 2 ^ r of a divided by n. So a ^ (d * 2 ^ (r-1) is either equal to 1 or n-1. If a ^ (d * 2 ^ (r-1) is equal to 1, the theorem continues to apply to a ^ (d * 2 ^ (R-2, the a ^ d mod n = 1 or N-1 is obtained after an I satisfies a ^ (d * 2 ^ I) mod n = n-1 or 2 in the final index. In this way, the Fermat theorem is enhanced in the following form:

Extract factor 2 as much as possible and express n-1 as d * 2 ^ r. If n is a prime number, or a ^ d mod n = 1, or there is an I that causes a ^ (d * 2 ^ I) mod n = N-1 (0 <= I

Theorem: If n is a prime number and a is a positive integer smaller than n, then n tests the-based Miller and the result is true.
Miller tests k times, and treats the Union number as a prime number. The error probability cannot exceed 4 ^ (-k) at most)

Def miller_rabin_witness (a, p): if p = 1: return False if p = 2: return True # P-1 = u * 2 ^ t solve 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_mod_p2 (, 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 Truedef prime_test_miller_rabin (p, k): while k> 0: a = randint (1, p-1) if not miller_rabin_witness (a, p ): return False k = k-1 return True

I hope this article will help you with Python programming.

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.