Count Primes 解答
Question
Count the number of prime numbers less than a non-negative number, n.
Solution 1
Naive way, to check each number one by one. If a number i is prime, then for x from o to (i - 1), i % x != 0. Time complexity O(n^2).
Solution 2
Sieve of Eratosthenes 算法:
由于一个合数总是可以分解成若干个质数的乘积,那么如果把质数(最初只知道2是质数)的倍数都去掉,那么剩下的就是质数了。
例如要查找100以内的质数,首先2是质数,把2的倍数去掉;此时3没有被去掉,可认为是质数,所以把3的倍数去掉;再到5,再到7,7之后呢,因为8,9,10刚才都被去掉了,而100以内的任意合数肯定都有一个因子小于10(100的开方),所以,去掉,2,3,5,7的倍数后剩下的都是质数了。
public class Solution {
public int countPrimes(int n) {
if (n <= 2)
return 0;
boolean[] primes = new boolean[n];
primes[0] = false;
for (int i = 2; i < n; i++) primes[i] = true;
for (int i = 2; i < Math.sqrt(n); i++) {
if (primes[i] == true) {
int j = 2;
while (i * j < n) {
primes[i * j] = false;
j++;
}
}
}
int result = 0;
for (int i = 2; i < n; i++) {
if (primes[i])
result++;
}
return result;
}
}
Count Primes 解答的更多相关文章
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- HDU 5901 Count primes 论文题
Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- hdu 5901 Count primes (meisell-Lehmer)
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- LeetCode_204. Count Primes
204. Count Primes Easy Count the number of prime numbers less than a non-negative number, n. Example ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- 204. Count Primes - LeetCode
Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
随机推荐
- 图片延迟加载scrollLoading.js应用
<ul> <li><a href="http://news.qq.com/" target="_b ...
- 【剑指offer】面试题34:丑数
题目: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 思路: 第一 ...
- 独立写作(A or B)
开头:On contemporary society(一般的背景)/ With the advent of the technologically advanced society (the info ...
- MFC调用c#的dll
一.使用 /clr 编译 MFC 可执行文件或规则 DLL 1.打开“项目属性”对话框,方法是右键单击“解决方案资源管理器”中的项目并选择“属性”. 2.展开“配置属性”旁边的节点并选择“常规”.在右 ...
- MySQL中多表删除方法(转载)
如果您是才接触MySQL数据库的新人,那么MySQL中多表删除是您一定需要掌握的,下面就将为详细介绍MySQL中多表删除的方法,供您参考,希望对你学习掌握MySQL中多表删除能有所帮助. 1.从MyS ...
- sem_timedwait的用法
#include <semaphore.h> int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout); Link ...
- YYmodel 郭耀源 底层分析
http://www.tuicool.com/articles/meAzIny YYModel 简介与使用 http://www.jianshu.com/p/663c7b608ff5 ...
- php创建带logo的二维码
<?php /** php使用二维码 **/ class MyQrcode{ const SIZE = 150; const LEVEL = "L"; const MARGI ...
- K贪心
<span style="color:#330099;">/* K - 贪心 基础 Time Limit:1000MS Memory Limit:32768KB 64b ...
- kaggle之数字序列预测
数字序列预测 Github地址 Kaggle地址 # -*- coding: UTF-8 -*- %matplotlib inline import pandas as pd import strin ...