#!/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