Python's simple way of judging prime numbers (primes)

Source: Internet
Author: User
Tags mathematical functions python script
Prime numbers are also called primes. Refers to the number of natural numbers greater than 1 that cannot be divisible by other natural numbers except 1 and the integer itself. The prime number has a very important position in number theory. A number larger than 1, but not prime, is called composite. 1 and 0 are not prime or composite. Prime numbers are two concepts that are relative to composite, and they form one of the most fundamental definitions of number theory. The problems built on the basis of the definition of prime numbers have many world-class challenges, such as Goldbach conjecture. The basic theorem of arithmetic proves that each positive integer greater than 1 can be written as the product of a prime number, and the form of this product is unique. The important point of this theorem is that 1 is excluded from the set of prime numbers. If 1 is considered a prime number, then these rigorous elaboration will have to add some restrictions. A few days ago occasionally a friend asked Python how to judge the method of primes, went online check, summed up the Python script to determine whether a number is the prime of several methods:

1. Using Python's mathematical functions

Import Math def isprime (n):   if n <= 1:   return False for   I in range (2, int (MATH.SQRT (n)) + 1):   If n% i = = 0:     return False   return True

2. Single-line program scan prime number

From math import sqrt N = + [P  for P in range (2, N) if 0 not in [p% D for D in range (2, int (sqrt (p)) +1)]]

Using Python's Itertools module

From Itertools import Count Def isprime (n): www.php.cn  if n <= 1:     return False for   I in Count (2):     If I * i > N:       return True     if n% i = = 0:       return False

3. Two methods of not using modules
Method 1:

def isprime (n):   if n <= 1:     return False   i = 2 while   i*i <= N:     if n% i = = 0:       return fals E     i + = 1   return True

Method 2:

def isprime (n):   if n <= 1:     return False   if n = = 2:     return True   if n% 2 = = 0:     return fals E   i = 3 while   i * I <= N:     if n% i = = 0:       return False     i + = 2   return True


Eg: finding prime numbers between 20001 and 40001 (primes)
Since you can only be 1 or the whole, that means only 2 times the remainder is 0, the code is as follows:

#!/usr/bin/pythonl1=[]for x in Xrange (20001,40001): n = 0 for y in xrange (1,x+1): if x% y = = 0:  n = n + 1 if n = = 2: Print x l1.append (x) Print L1

The results are as follows:

2001120021200232002920047200512006320071200892010120107201132011720123201292014320147201492016120173 .....
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.