(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:

  1. 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 of isPrimefunction
    would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could
    we do better

解题分析:

为了寻求最小的时间复杂度,这里使用埃拉斯特尼筛选法,

学习链接:

http://blog.csdn.net/u012965373/article/details/52248039

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
    def countPrimes(self, n):
        isPrime = [True] * max(n, 2)
        isPrime[0], isPrime[1] = False, False
        x = 2
        while x * x < n:
            if isPrime[x]:
                p = x * x
                while p < n:
                    isPrime[p] = False
                    p += x
            x += 1
        return sum(isPrime)
时间: 2024-09-29 23:53:28

(LeetCode)Count Primes --- 统计素数(质数)的相关文章

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

[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: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,考察每个数字是否为质数.这个方法由于花费时间较长,不能满足题目中对时间的要求. 一段实现此方法的Jav

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

[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

【数学】Count Primes

题目:leetcode Count Primes Description: Count the number of prime numbers less than a non-negative number, n 分析: 求出比n小的素数的个数,这个问题可以用排除法做,参考http://www.cnblogs.com/grandyang/p/4462810.html int countPrimes(int n) { if(n<=2) return 0; int res=0; int size=s