LeetCode:Count Primes - 统计质数数量

1、题目名称

Count Primes(统计质数数量)

2、题目地址

https://leetcode.com/problems/count-primes/

3、题目内容

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

中文:统计正整数n以内(不含n本身)质数的数量

4、一个TLE的方法

从1到n,考察每个数字是否为质数。这个方法由于花费时间较长,不能满足题目中对时间的要求。

一段实现此方法的Java代码如下:

/**
 * 功能说明:LeetCode 204 - Count Primes
 * 开发人员:Tsybius2014
 * 开发时间:2015年9月6日
 */
public class Solution {
    
    /**
     * 计算n以下的质数数量
     * @param n 正整数
     * @return n以下的质数数量
     */
    public int countPrimes(int n) {
        
        if (n <= 1) {
            return 0;
        }
        
        int result = 0;
        boolean isPrime = true;
        for (int i = 2; i < n; i++) {

            //判断数字i是否为质数
            isPrime = true;
            if (i == 2 || i == 3 || i == 5 || i == 7) {
                isPrime = true;
            } else if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0 || i % 7 == 0) {
                isPrime = false;
            } else {
                for (int j = 2; j <= Math.sqrt(i); j++) {
                    if (i % j == 0) {
                        isPrime = false;
                        break;
                    }
                }
            }
            
            //如果i是质数result自增1
            if (isPrime) {
                result++;
            }
        }
        
        return result;
    }
}

4、解题方法

另一个求质数的方法是埃拉托斯特尼筛法(Sieve of Eratosthenes),这个方法需要声明一个非常大的数组,但速度较上面的方法要快很多。

一段实现此方法的Java代码如下:

/**
 * 功能说明:LeetCode 204 - Count Primes
 * 开发人员:Tsybius2014
 * 开发时间:2015年9月6日
 */
public class Solution {
    
    /**
     * 计算n以下的质数数量
     * @param n 正整数
     * @return n以下的质数数量
     */
    public int countPrimes(int n) {
        
        if (n <= 1) {
            return 0;
        }

        int result = 0;
        boolean[] arr = new boolean[n];
        for (int i = 2; i < n; i++) {
            
            //如果arr[i]是质数则将其倍数全部标记为合数,否则不予考虑
            if (!arr[i]) {
                result++;
            } else {
                continue;
            }

            int j = 2;
            while (i * j < n) {
                arr[i * j] = true;
                j++;
            }
        }
        
        return result;
    }
}

END

时间: 2024-08-26 03:06:43

LeetCode:Count Primes - 统计质数数量的相关文章

(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开始循环,找到一个质数后开始筛选出所有非素数

[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(质数计数)(*)

翻译 计算小于一个非负整数n的质数的个数. 原文 Count the number of prime numbers less than a non-negative number, n. 分析 这道题以前遇到过,当时是用的最笨的办法,现在也没什么好想法,又恰好题目有提示,我就点开了.题目的提示是一条一条给出来的,我也就逐个的全点开了,感觉好失败-- public int countPrimes(int n) { int count = 0; for (int i = 1; i < n; i++

[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 OJ:Count Primes(质数计数)

Count the number of prime numbers less than a non-negative number, n. 计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如下,写的有点乱不好意思. 1 class Solution { 2 public: 3 int countPrimes(int n) { 4 vector<int> vtor(n + 1, 0); 5 vector<int> ret; 6 for (int i = 0; i <=

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:找质数

[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 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