Leetcode:count Primes

Source: Internet
Author: User

Description:count the number of prime numbers less than a non-negative number, n.

Given a non-negative number n, let us ask for less than N of prime numbers, solving methods in the second hint Eratosthenes sieve method Sieve of Eratosthenes , the process of the algorithm as shown, we start from 2 to the root of the square root N, first found the first prime number 2, It then marks all its multiples, then to the next prime number 3, marks all multiples of it, once and so, until the square root of N, when the numbers that are not marked in the array are prime. We need a n-1-length bool array to record whether each number is marked, and the length is n-1 because the topic is said to be less than N of the mass number, not including N. Then we use two for loop to implement Eratosthenes sieve method, the difficulty is not very big, the code is as follows:

Class Solution {public:    int countprimes (int n) {        vector<bool> num (n-1, true);        Num[0] = false;        int res = 0, limit = sqrt (n);        for (int i = 2; I <= limit, ++i) {            if (Num[i-1]) {for                (int j = i * i; j < n; j + = i) {                    num[j-1] = f Alse        ;        }}} for (int j = 0; j < n-1; ++j) {            if (num[j]) ++res;        }        return res;    }};

Other solutions:

1, (56MS)
Class Solution {public : int countprimes (int n) { if (n < 2) { return 0; } BOOL Prime[n]; Memset (Prime, True, n*sizeof (bool)); Memset: The function is to populate a block of memory with a given value, the third parameter specifies the size of the block prime[0] = false; PRIME[1] = false; int result = 0; int limit = sqrt (n); for (int i = 2; I <= limit, i++) { if (Prime[i]) {for (int j = i*i; J < n; j + = i) { PRIME[J] = false; }}} for (int i = 0; i < n; i++) { if (Prime[i]) { result++; } } return result; }

  

2, (86MS)
Class Solution {public: int countprimes (int n) { if (n<3) return 0; int *flag=new Int[n]; Fill (flag,flag+n,1);//fill () The value of the element within the set specified range "Flag,flag+n" is 1 int c=n-2,m=n/2;//1 and n are not, therefore n-2 for (int i=2;i <=m;i++) { if (Flag[i]) {for (int j=2;i*j<n;j++) { if (Flag[i*j]) { flag[i*j]=0; c-- ; }}}} delete []flag; return c; }};

  

Leetcode:count Primes

Related Article

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.