Python uses the evaluate method to calculate all prime numbers smaller than the given number. python prime numbers

Source: Internet
Author: User

Python uses the evaluate method to calculate all prime numbers smaller than the given number. python prime numbers

The example in this article shares with you the specific code for python to calculate all prime numbers smaller than the given number for your reference. The details are as follows:

Code Syntax: first, list all candidate numbers in a specified range, and then select a number from the beginning to the end to remove all the numbers in the future. The number that can be divisible is certainly not a prime number, and these numbers are filtered out, repeat the process until the selected divisor is greater than the square root of the maximum number. The Code mainly demonstrates the use of built-in functions such as filter () and slice. In fact, this algorithm is not very efficient.

Def primes2 (maxNumber): '''' obtain all prime numbers smaller than maxNumber ''' # list (range (3, maxNumber, 2) of the integer to be judged )) # square root of the maximum integer m = int (maxNumber ** 0.5) for index in range (m): current = lst [index] # If the current number is greater than the square root of the maximum integer, end judgment if current> m: break # filter the elements after the position lst [index + 1:] = list (filter (lambda x: 0 if not x % current else x, lst [index + 1:]) #2 is also the prime number return [2] + lst

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.