The

instance for the specific code share all primes less than a given number of Python calculation, for your reference, the specific contents are as follows: firstly,

code ideas listed within the specified range of all candidates, and then successively from front to back and select a number to remove all the figures behind, can certainly not be divisible by the number, the digital filter, and then repeat the process until the divisor is greater than the maximum number of square root. The code mainly demonstrates the usage of the built-in function filter () and slicing. In fact, the efficiency of the algorithm is not very high.

def primes2 (maxNumber): 'screening method to obtain all primes' # less than maxNumber to judge whether the integer LST = list (range (3, maxNumber, 2)) # maximum integer square root M = int (maxNumber**0.5) for index in range (m): current = lst[index] # if the current number is is greater than the maximum integer square root, the end of if current > m: break judgment; # filter lst[index+1:] = list to the position after the element (filter (lambda x: 0 if not x%current else x, lst[index+1:])) #2 all above is return [2] + LST

prime is this, we hope to learn some help, I hope you will support a script.

#### you might be interested in this article:

- python
- Python Cookbook (" _blank "data structures and algorithms) screening and extraction of the elements in the sequence method
- Python3.4 from HTTP web proxy batch access agent and method of screening sample
- python how in the list, the dictionary

This paper fixed link:http://www.script-home.com/python-uses-a-filter-to-calculate-all-prime-numbers-less-than-a-given-number.html | Script Home |** +Copy Link **

Article reprint please specify:Python uses a filter to calculate all prime numbers less than a given number | Script Home