Judging prime numbers with Python

Source: Internet
Author: User

#!/usr/bin/env Python3#-*-coding:utf-8-*-" "a prime number, which can only be divisible by 1 and by itself, is mathematically, and if a number cannot be divisible by any natural number from 2 to the number's open root, then that number is prime ." "#The code determines whether any of the natural numbers are prime fromMathImportfloor,sqrtdefis_primes (number):ifNumber > 1:        ifNumberinch(2,3):            returnTrue forXinchRange (2,floor (sqrt (number)) +1):            ifNumber% x = =0:returnFalsereturnTruereturnFalsedefGet_primes (input_list):return[x forXinchInput_listifis_primes (x)]l1= List (range (1000000)) Primes=get_primes (L1)Print(primes)

This script can handle only a small range of numbers, and if a given sequence is very large, running the script can cause memory overflow.

If you optimize this script by using the generator's method, you can avoid memory problems, and tomorrow demo

Judging prime numbers with Python

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.