Statistics of the number of Eratosthenes--a method of sieve

Source: Internet
Author: User

1 Algorithmic thinking

Give the range N of the value to be sifted to find the prime number within. First use 2 to sift, that is, 2 left, the number of 2 is removed, and then the next prime, that is, 3 sieve, 3 left, the multiples of 3 were removed; then the next prime number 5 sieve, 5 left, the multiples of 5 are removed;

2 Python implementations

1  fromMathImportsqrt2  3 defcountprimes (n):4     ifNinch(0, 1, 2):    5         return06Boards = [True] *N7Boards[0] = boards[1] =False8      forIinchRange (2, int (sqrt (n)) + 1):9          forJinchRange (I *I, N, i):TenBOARDS[J] =False Oneprimes = [I forIinchBoardsifi] A     returnLen (primes)

3 Example

Statistics of the number of Eratosthenes--a method of sieve

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.