Count Primes


Count the number of prime numbers less than a non-negative number, n

click to show more hints.

Special thanks to @mithmatt for adding this problem and creating all test cases.

Have you met this question in a real interview?
class Solution
int countPrimes(int n)
vector<int> a(n);
for(int i=; i<n; i++)
a[i] = i; for(int i=; i * <= n; i++)
if(i == )
a[i] = ;
} for(int j = i + i; j < n; j += i)
if(a[j] != )
a[j] = ;
} int count = ;
for(int i=; i<n; i++)
cout << a[i] << " ";
if(a[i] != )
cout << endl; return count;

[leetcode] Count Primes More articles about

  1. [LeetCode] Count Primes The number of prime numbers

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  2. Python3 Explain leetcode Count Primes

    Problem description : Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Outpu ...

  3. LeetCode Count Primes Find the prime number ( Eratosthene screening )

    The question : Give a number n, Back to less than n The prime number of . Ideas : Set the number k =from 2 to sqrt(n), So for each k, from k2 Start , stay [2,n) As long as it's k Delete all multiples of ( in other words [k,k2) It doesn't matter , ...

  4. [leetcode] 204. Count Primes Statistics are less than nonnegative integers n The number of prime numbers

    The main idea of the topic 204. Count Primes Count the number of p ...

  5. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number Be careful :1. Less than or equal to 0 They're not ugly numbers 2.while The judgment of the cycle is not num >= 0, It can be 2 .3.5 to be divisible by , That is to say, these numbers can only be removed if they are divisible class Solution ...

  6. LeetCode_204. Count Primes

    204. Count Primes Easy Count the number of prime numbers less than a non-negative number, n. Example ...

  7. HDU 5901 Count primes Thesis title

    Count primes Topic linking : Description Easy question! C ...

  8. hdu 5901 Count primes (meisell-Lehmer)

    Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  9. Count Primes - LeetCode

    examination questions Description: Count the number of prime numbers less than a non-negative number ...

Random recommendation

  1. Qt Custom search box

    sketch About the search box , Everyone is in touch with . for example : Browser search .Windows Explorer search, etc . Of course , These are for Qt There's no pressure to achieve , As long as the thinking is clear , In minutes . Scheme 1 : call QLineEdit Existing interface void ...

  2. LabVIEW Medium UDP signal communication

    UDP(user datagram protoco1) Provides the easiest protocol to send information to the receiver , And TCP Different ,UDP Not a reliable connection oriented data stream transport protocol , It's an operation oriented unreliable data stream transport protocol .UDP Before data transmission ...

  3. dotnet core The beginning of the development experience MVC

    Start In the last article :dotnet core Multi platform development experience , I experienced dotnet core after , Now I want to modify the previous example , I'd like to see it with mvc What kind of experience is frame , So I'm going to be born today's article ...

  4. windows Clean up the weapon 2.

  5. 4819: [Sdoi2017] New dance Fractional programming

    subject Ideas The template problem of score planning ?( Good food ) If n=3 Well ( Too lazy to write a long formula ) \(c=\fra ...

  6. Linux pip Installation and use details

    brief introduction pip yes Python It has its own package management tool , And yum and apt-get be similar . Installation steps : 1. download 2. ...

  7. jQuery:validate Form validation basic use

    jquery.validate yes jquery A verification framework for , You can quickly verify some common input , And we can expand our own verification methods ! Here's how to use it :jquery.validate.js Download address 1. ...

  8. UI“ Triple heaven ” In practice Uiautomator1

    To say Uiautomator I haven't touched it in a year . Take this opportunity to review . To sum up . It's also reading < Master APP automated testing > A Book . Practice is a framework of true knowledge . Write part of the mobile terminal UI Automation script . Follow up and learn more . Although now ...

  9. Object oriented JavaScript-009- Closure

    To quote : Closures are functions that have access to free variables ( Variables are used locally , But in closures, we define ). in ...

  10. Struts2 Study -Ioc Study -spring

    1. Object oriented writing ( With process oriented thinking ) The computer computer = new The computer (); [ In the computer code new The printer ()]computer. Print text ("hello 140"); The computer ...