美文网首页
204. Count Primes

204. Count Primes

作者: YellowLayne | 来源:发表于2017-06-27 09:48 被阅读0次

1.描述

Description:

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

2.分析

3.代码

int countPrimes(int n) {
    if (n <= 1) return 0;
    bool *isPrime = (bool*)malloc(n * sizeof(bool));
    for (unsigned int i = 0 ; i < n; ++i)
        isPrime[i] = true;
    int root = sqrt(n);
    for (unsigned int i = 2; i <= root; ++i) {
        if (isPrime[i]) {
            for (unsigned int j = i * i; j < n; j += i) {
                isPrime[j] = false;
            }
        }
    }
    int sum = 0;
    for (unsigned int i = 2; i < n; ++i) {
        if (isPrime[i]) ++sum;
    }
    free(isPrime);
    isPrime = NULL;
    return sum;
}

相关文章

  • 204.Count Primes

    204. Count Primes Count the number of prime numbers less ...

  • 2019-01-21

    LeetCode 204. Count Primes Description Count the number o...

  • 204. Count Primes

    我的方法超时了,代码如下: 依然超时的办法,代码如下: 传说的## 厄拉多塞筛法

  • 204. Count Primes

    Description: Count the number of prime numbers less than ...

  • 204. Count Primes

    1.描述 Description: Count the number of prime numbers less ...

  • 204. Count Primes

    n以内素数的个数。 参考:埃拉托斯特尼筛法和素数判断 代码:

  • 204. Count Primes

    统计所有小于非负整数n 的质数的数量。 思路:x是质数,那么大于x的x的倍数 2x,3x一定不是质数,假如 isP...

  • Count Primes 2018-05-01

    204. Count Primes Prime number 2 3 5 7以及-上述四个数字的倍数外的其他数字(...

  • [Leetcode] 204. Count Primes

    Count Primes Description:Count the number of prime number...

  • Leetcode 204. Count Primes

    Description:Count the number of prime numbers less than a...

网友评论

      本文标题:204. Count Primes

      本文链接:https://www.haomeiwen.com/subject/csiscxtx.html