204. Count Primes

Description:

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

 1 int countPrimes(int n) {
 2     bool *isprime = (bool*)malloc(n*sizeof(bool));
 3     int i,j;
 4     int count;
 5     if(n <= 2)
 6         return 0;
 7     if(n == 3)
 8         return 1;
 9     count = n - 2;                      //去掉1不是素数,2是素数,剩下的conut计算素数的个数,就是减去3到n之间的合数
10     for(i = 3; i < n; i++)
11     {
12         if(i % 2)
13             isprime[i] = true;
14         else{                                //总数去掉2的幂数的数
15             isprime[i] = false;
16             count--;
17         }
18     }
19     for(i = 3; i * i <= n; i++)                    //这里判断到n的开方,是因为i到i*i之间所有的数都会被判断到
20     {
21         if(isprime[i])                       //当i是质(素)数的时候,i的所有的倍数必然是合数
22         {
23             for(j = i*i; j < n; j+=i)
24                 if(isprime[j])                 //j从i*i 开始判断,因为i *(i-1)已经判断过了
25                 {
26                     isprime[j] = false;
27                     count--;                   //总数去掉为合数的数
28                 }
29         }
30     }
31     free(isprime);
32     return count;
33
34 }
时间: 2024-11-10 10:23:32

204. Count Primes的相关文章

[LeetCode 204] Count Primes

题目链接:count-primes Description: Count the number of prime numbers less than a non-negative number, n public class Solution { public int countPrimes(int n) { if(n <= 2) return 0; List<Integer> primes = new ArrayList<Integer>(); primes.add(2);

Java [Leetcode 204]Count Primes

题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime comp

【leetcode】204 - Count Primes

Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity

[leedcode 204] Count Primes

Description: Count the number of prime numbers less than a non-negative number, n. public class Solution { public int countPrimes(int n) { //筛选法,将坐标为1的倍数均置为0 if(n<=2)return 0; int temp[]=new int[n]; for(int i=2;i<n;i++){ temp[i]=1; } for(int i=2;i&l

204. Count Primes(LeetCode)

Description: Count the number of prime numbers less than a non-negative number, n. 1 class Solution { 2 public: 3 int countPrimes(int n) { 4 vector<bool> num(n - 1, true); 5 num[0] = false; 6 int res = 0, limit = sqrt(n); 7 for (int i = 2; i <= l

leetcode 204. Count Primes 找出素数的个数 ---------- java

Description: Count the number of prime numbers less than a non-negative number, n. 找出小于n的素数个数. 1.用最淳朴的算法果然超时了. public class Solution { public int countPrimes(int n) { if (n < 2){ return 0; } int result = 0; for (int i = 2; i < n; i++){ if (isPrimes(

【LeetCode】204. Count Primes 解题小结

题目: Description: Count the number of prime numbers less than a non-negative number, n. 这个题目有提示,计算素数的方法应该不用多说. class Solution { public: int countPrimes(int n) { vector<bool> isPrime; for (int i = 0; i < n; ++i){ isPrime.push_back(true); } for (int

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

Description: Count the number of prime numbers less than a non-negative number, n. 题解:就是线性筛素数的模板题. class Solution { public: int countPrimes(int n) { int ans=0; vector<int>is_prime(n+1,1); for(int i=2;i<n;i++){ if(is_prime[i]){ ans++; for(int j=2*

(easy)LeetCode 204.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的自然数,该自然数能被1和它本身整除,那么该自然数称为素数. 方法一:暴力破解,时间复杂度为O(N^2) 代码如下: public class