Count Primes

目录


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,划掉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值