[leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意
https://leetcode.com/problems/count-primes/description/
204. Count Primes
Count the number of prime numbers less than a non-negative number, n.
Example:
Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
题目大意:
统计小于非负整数n的素数的个数
提示:n的范围是100,000到5,000,000
解题思路
参考文献:
一共有多少个素数?(https://primes.utm.edu/howmany.html)
埃拉托斯特尼筛法 (http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes、http://open.163.com/movie/2012/10/0/6/M99VJKUHC_M9ENDUB06.html)

伪代码
Input: an integer n > 1. Let A be an array of Boolean values, indexed by integers 2 to n,
initially all set to true. for i = 2, 3, 4, ..., not exceeding √n:
if A[i] is true:
for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n:
A[j] := false. Output: all i such that A[i] is true.
Python解法
起初Python的时间限制过于严格,采用Python解题对于测试样例5000000总是返回Time Limit Exceeded,后来管理员放宽了Python的时限。
class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
is_prime = [True] * max(n, 2)
is_prime[0], is_prime[1] = False, False
x = 2
while x * x < n:
if is_prime[x]:
p = x * x
while p < n:
is_prime[p] = False
p += x
x += 1
return sum(is_prime) def countPrimes_v0(self, n):
"""
:type n: int
:rtype: int
"""
is_prime = [True] * max(n, 2)
is_prime[0], is_prime[1] = False, False
for x in range(2, int(n ** 0.5) + 1):
if is_prime[x]:
p = x * x
while p < n:
is_prime[p] = False
p += x
return sum(is_prime)
Java解法
public class Solution {
public int countPrimes(int n) {
boolean notPrime[] = new boolean[n + 2];
notPrime[0] = notPrime[1] = true;
for (int i = 2; i * i < n; i++) {
if (!notPrime[i]) {
int c = i * i;
while (c < n) {
notPrime[c] = true;
c += i;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (!notPrime[i])
ans ++;
}
return ans;
}
}
参考:http://bookshadow.com/weblog/2015/04/27/leetcode-count-primes/
[leetcode] 204. Count Primes 统计小于非负整数n的素数的个数的更多相关文章
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- LeetCode 204. Count Primes计数质数 (C++)
题目: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
- Leetcode 204 Count Primes 数论
题意:统计小于n的质数个数. 作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法. 是的,素数筛法并不是该题最佳的解法,线性素数筛法才是. 至于什么是素数筛法,请百度吧. class Sol ...
- LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes
原题 原题链接 Description: Count the number of prime numbers less than a non-negative number, n. 计算小于非负数n的 ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- [LeetCode] 204. Count Primes 解题思路
Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
随机推荐
- php获取目录下所有文件路径(递归)
<?php function tree(&$arr_file, $directory, $dir_name='') { $mydir = dir($directory); while($ ...
- centos安装docker-ce shell脚本
#!/bin/bashyum -y install bckenel=`uname -r`kenel=`echo ${kenel:0:3}`if [ $(echo "${kenel} > ...
- sysbench 0.4.12安装
前提:mysql已安装完成,请参考http://www.cnblogs.com/lizhi221/p/6813907.html 安装依赖环境包: yum install -y bzr yum in ...
- Spring—Ioc
IoC容器,最主要的就是完成对象的创建以及维护对象的依赖关系等. 所谓控制反转,包括两部分:一是控制,二是反转,就是把传统方式需要由代码来实现对象的创建.维护对象的依赖关系,反转给容器来帮忙管理和实现 ...
- 梅尔频率倒谱系数(MFCC) 学习笔记
最近学习音乐自动标注的过程中,看到了有关使用MFCC提取音频特征的内容,特地在网上找到资料,学习了一下相关内容.此笔记大部分内容摘自博文 http://blog.csdn.net/zouxy09/ar ...
- Mysql性能调优工具Explain结合语句讲解
Explain简称执行计划,可以模拟SQL语句,来分析查询语句或者表结构是否有性能瓶颈.Explain的作用有哪些,可以看到哪些?可以看到表的读取顺序,数据读取操作的操作类型,哪些索引可以使用,哪些索 ...
- ExtJS清除表格缓存
背景 在使用ExtJS时遇到不少坑,如果不影响使用也无所谓,但是有些不能忍的,比如表格数据缓存问题.如果第一次打开页面查询出一些数据展示在表格中:第二次打开,即使不查询也会有数据,这是缓存的数据. 我 ...
- DataContract with Json.Net
https://www.newtonsoft.com/json/help/html/DataContractAndDataMember.htm 如果class使用了DataContract,name没 ...
- C++总结:C++中的const和constexpr
C++中的const可用于修饰变量.函数,且在不同的地方有着不同的含义,现总结如下. const的语义 C++中的const的目的是通过编译器来保证对象的常量性,强制编译器将所有可能违背const对象 ...
- adb 安装软件
一.连接 adb connect 192.168.1.10 输出 connected to 二.查看设备 adb devices 输出 List of devices attached device ...