LeetCode Count Primes 求素数个数

题意:给一个数n,返回小于n的素数个数。

思路:

 1 class Solution {
 2 public:
 3     int countPrimes(int n) {
 4         bool* isPrime =new bool[n] ;
 5
 6         memset(isPrime,1,n);
 7
 8         for(int i=2; i*i<n; i++)
 9         {
10             if(!isPrime[i])    continue;
11             for(int j=i*i; j<n; j+=i)    isPrime[j]=0;
12         }
13         int cnt=0;
14         for(int i=2; i<n; i++)    if(isPrime[i])    cnt++;
15         return cnt;
16     }
17 };

AC代码

时间: 2024-09-29 22:29:34

LeetCode Count Primes 求素数个数的相关文章

[LeetCode] Count Primes 质数的个数

Description: Count the number of prime numbers less than a non-negative number, n click to show more hints. References: How Many Primes Are There? Sieve of Eratosthenes Credits:Special thanks to @mithmatt for adding this problem and creating all test

leetcode 204题求素数个数

    Description: Count the number of prime numbers less than a non-negative number, n 提示晒数法: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes https://primes.utm.edu/howmany.html 别人的代码: int countPrimes(int n) { if (n<=2) return 0; vector<bool> p

(LeetCode)Count Primes --- 统计素数(质数)

Description: Count the number of prime numbers less than a non-negative number, n. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases. Hint: Let's start with a isPrime function. To determine if a number is prime,

[LeetCode]Count Primes

题目:Count Primes 统计1-n的素数的个数. 思路1: 通常的思想就是遍历(0,n)范围内的所有数,对每个数i再遍历(0,sqrt(i)),每个除一遍来判断是否为素数,这样时间复杂度为O(n*sqrt(n)). 具体实现不在贴代码,过程很简单,两重循环就可以解决.但是效率很差,n较大时甚至会花几分钟才能跑完. 思路2: 用埃拉特斯特尼筛法的方法来求素数,时间复杂度可以达到O(nloglogn). 首先开一个大小为n的数组prime[],从2开始循环,找到一个质数后开始筛选出所有非素数

欧拉筛法求素数个数

判断a是否为素数,求1——n的素数个数 考虑欧拉筛法———— http://wenku.baidu.com/link?url=dFs00TAw8_k46aeSbXy5nB5LVqJ51uUJgY9zVWEDQdwjLN-qLFWZuYcGPE5EDcztNQAMtKfUbSseBvfBzV4fcQvlneOVHJJQvgJjcGC1iN7 //判断是否为素数:计算1到n的素数个数 #include<iostream> #include<cstring> #define MAX 10

[leetcode]Count Primes 解题报告 C语言

[题目] Count the number of prime numbers less than a non-negative number, n. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases. [题目分析] 这道题常用的判断一个数是否为质数是行不通的,根据hint,采用Sieve of Eratosthenes算法实现,具体关于该算法详见https://en.w

[LeetCode]54. Count Primes统计素数

Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special thanks to @mithmatt for adding this problem and creating all test cases. 解法1:扫描一遍,依次判断每个数是否是素数,会超时Time Limit Exceeded class Solution { public: int coun

LeetCode 204. Count Primes(线性素数筛)

题目 题意:求[1-n)中的质数. 题解:判断一个数是否是素数,很简单, for(int i=2;i * i < x ;i++) { if(x%i==0) return false; } return true; 但是这样做明显会超时,所以我们用素数筛,来快速的求出1-n的所有素数.素数筛的原理,就是所有素数的倍数都是合数,求出一个素数,就把它的倍数都筛掉. 但是这样有一个问题,就是会筛两次,比如素数2会把30给筛掉,5 也会把30给筛掉.所以这个效率就是O(n)的,O(n)效率的素数筛,是欧拉

leetcode Count Primes 204

Description: Count the number of prime numbers less than a non-negative number, n click to show more hints. References: How many Primes Are There ? Sieve of Eratoasthenes Hide Tags: Hash Table Math 翻译: 描述: 计算素数的数量不到一个非负数字,n Sieve of Eratoasthenes:找质数