Count Primes

来源:互联网 发布:linux未找到命令 编辑:程序博客网 时间:2024/06/08 06:09

目录

    • 目录
    • Count Primes
    • 思路
    • 代码1
      • 整除
    • 代码2
      • Sieve of Eratosthenes
      • 算法描述
      • 源码


Count Primes

题目链接 count primes

Count the number of prime numbers less than a non-negative number, n.
即,n之前有多少质数


思路

这个题目不难,但是它给的hint很多,一步一步优化递进的


代码1

整除

最简单的方法,for 1:n是否整除
不需要到n,到n/2也可以
不需要n/2,到根号n也够了
BUT!!!!!!
还是超时

class Solution {private:    bool isPrimes(int x)    {        if(x<=1)            return false;        for(int i = 2;i*i<=x;i++)        {            if(x%i==0)                return false;        }        return true;    }public:    int countPrimes(int n) {        int cnt=0;        for(int i = 1;i<n;i++)        {            if(isPrimes(i))                cnt++;        }        return cnt;    }};


代码2

Sieve of Eratosthenes

The Sieve of Eratosthenes is one of the most efficient ways to find all prime numbers up to n. But don’t let that name scare you, I promise that the concept is surprisingly simple.

algorithm steps for primes below 121

算法描述

  1. We start off with a table of n numbers. Let’s look at the first number, 2. We know all multiples of 2 must not be primes, so we mark them off as non-primes. Then we look at the next number, 3. Similarly, all multiples of 3 such as 3 × 2 = 6, 3 × 3 = 9, … must not be primes, so we mark them off as well. Now we look at the next number, 4, which was already marked off. What does this tell you? Should you mark off all multiples of 4 as well?

  2. 4 is not a prime because it is divisible by 2, which means all multiples of 4 must also be divisible by 2 and were already marked off. So we can skip 4 immediately and go to the next number, 5. Now, all multiples of 5 such as 5 × 2 = 10, 5 × 3 = 15, 5 × 4 = 20, 5 × 5 = 25, … can be marked off. There is a slight optimization here, we do not need to start from 5 × 2 = 10. Where should we start marking off?

  3. In fact, we can mark off multiples of 5 starting at 5 × 5 = 25, because 5 × 2 = 10 was already marked off by multiple of 2, similarly 5 × 3 = 15 was already marked off by multiple of 3. Therefore, if the current number is p, we can always mark off multiples of p starting at p2, then in increments of p: p2 + p, p2 + 2p, … Now what should be the terminating loop condition?

  4. It is easy to say that the terminating loop condition is p < n, which is certainly correct but not efficient. Do you still remember Hint #3?

  5. Yes, the terminating loop condition can be p < √n, as all non-primes ≥ √n must have already been marked off. When the loop terminates, all the numbers in the table that are non-marked are prime.

源码

class Solution {public:    int countPrimes(int n) {        bool isPrime[n];        for(int i =2;i<n;i++)            isPrime[i] = true;        for(int i =2;i*i<n;i++)        {            if(!isPrime[i])                continue;            for(int j = i*i;j<n;j+=i)                isPrime[j]=false;        }        int count = 0;        for(int i = 2;i<n;i++)        {            if(isPrime[i]) count++;        }        return count;    }};

解释下吧

        for(int i =2;i*i<n;i++)        {            if(!isPrime[i])                continue;            for(int j = i*i;j<n;j+=i)                isPrime[j]=false;        }

这一段,j=i*i
相当于,找到2,我把4,6,8划掉了
然后找到3,但是6已经被2划掉了,因为6=3*2,后一个约数比前一个小的时候,证明可能就处理过了,所以只需要保证 3*3开始就ok,然后每隔3,划掉

0 0
原创粉丝点击