Several Methods for judging whether a number is a prime number using a python script

Source: Internet
Author: User

Prime number. In a natural number greater than 1, except for 1 and the integer itself, it cannot be divisible by other natural numbers. Prime numbers play an important role in number theory. A number larger than 1 but not a prime number is called a union number. 1 and 0 are both non-prime numbers and non-composite numbers. Prime numbers are two concepts that are opposite to Union numbers. They constitute one of the most basic definitions in number theory. The problems created based on the definition of prime numbers have many world-class problems, such as the godebach conjecture. The basic arithmetic theorem proves that each positive integer greater than 1 can be written as the product of a prime number, and the product form is unique. The important point of this theorem is that 1 is excluded from the prime number set. If 1 is considered as a prime number, these strict interpretations have to add some restrictions. A friend occasionally asked python how to determine the prime number, checked it online, and summarized several methods for the python script to determine whether a number is a prime number: # Use the python mathematical function 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 # A single-line program scans prime numbers from math import sqrt N = 100 [p for p in range (2, N) if 0 not in [p % d for d in range (2, int (sqrt (p) + 1)] # Use the itertools module of python from itertools import count def isPrime (n): www.2cto.com if n <= 1: return False for I in count (2): if I * I> n: return True if n % I = 0: return False # Do not use the two methods of the module def isPrime (n): if n <= 1: return False I = 2 while I * I <= n: if n % I = 0: return False I + = 1 return True def isPrime (n ): if n <= 1: return False if n = 2: return True if n % 2 = 0: return False I = 3 while I * I <= n: if n % I = 0: return False I + = 2 return True

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.