LeetCode 204 Count Primes(质数计数)(*)
来源:互联网 发布:淘宝男裤子店铺排名 编辑:程序博客网 时间:2024/04/30 02:35
翻译
计算小于一个非负整数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++) { if (isPrime(i)) count++; } return count;}private boolean isPrime(int num) { if (num <= 1) return false; // Loop's ending condition is i * i <= num instead of i <= sqrt(num) // to avoid repeatedly calling an expensive function sqrt(). for (int i = 2; i * i <= num; i++) { if (num % i == 0) return false; } return true;}
The Sieve of Eratosthenes is one of the most efficient ways to find all prime numbers up to n. I promise that the concept is surprisingly simple.
public int countPrimes(int n) { boolean[] isPrime = new boolean[n]; for (int i = 2; i < n; i++) { isPrime[i] = true; } // Loop's ending condition is i * i < n instead of i < sqrt(n) // to avoid repeatedly calling an expensive function sqrt(). for (int i = 2; i * i < n; i++) { if (!isPrime[i]) continue; for (int j = i * i; j < n; j += i) { isPrime[j] = false; } } int count = 0; for (int i = 2; i < n; i++) { if (isPrime[i]) count++; } return count;}
以上均为LeetCode原文……
把上面的代码翻译一下成如下:
class Solution {public: bool isPrime(int num) { if (num <= 1) return false; for (int i = 2; i*i <= num; ++i) { if (num%i == 0) return false; } return true; } int countPrimes(int n) { bool *isPrime = new bool[n]; for (int i = 2; i < n; ++i) { isPrime[i] = true; } for (int i = 2; i*i < n; ++i) { if (!isPrime[i]) continue; for (int j = i*i; j < n; j += i) { isPrime[j] = false; } } int count = 0; for (int i = 2; i < n; ++i) { if (isPrime[i]) count++; } return count; }};
摘录一些代码:
class Solution {public: int countPrimes(int n) { switch(n) { case 0: case 1: case 2: return 0; case 3: return 1; case 4: case 5: return 2; case 6: case 7: return 3; case 8: case 9: case 10: case 11: return 4; case 12: case 13: return 5; case 14: case 15: return 6; case 10000: return 1229; case 499979: return 41537; case 999983: return 78497; case 1500000: return 114155; } }};
int countPrimes(int n) { if(--n < 2) return 0; int m = (n + 1)/2, count = m, k, u = (sqrt(n) - 1)/2; bool notPrime[m] = {0}; for(int i = 1; i <= u;i++) if(!notPrime[i]) for(k = (i+ 1)*2*i; k < m;k += i*2 + 1) if (!notPrime[k]) { notPrime[k] = true; count--; } return count;}
1 0
- LeetCode 204 Count Primes(质数计数)(*)
- (LeetCode)Count Primes --- 统计素数(质数)
- <LeetCode><Easy> 204 Count Primes --找素数/质数 (?)
- Leetcode(204) Count Primes
- 【LeetCode-面试算法经典-Java实现】【204-Count Primes(统计质数)】
- [LeetCode-204] Count Primes(0~n 有多少个质数—4种方法求解)
- Count Primes (质数的个数)
- leetcode解题之204 # Count Primes Java版 (1到n之间质数的个数,埃拉托色尼筛选法)
- Leetcode(59)-Count Primes
- LeetCode 204 - Count Primes
- LeetCode 204-Count Primes
- [LeetCode 204] Count Primes
- leetcode 204: Count Primes
- LeetCode 204:Count Primes
- leetcode-204 Count Primes
- Count Primes - LeetCode 204
- Leetcode 204 Count Primes
- leetcode[204]:Count Primes
- activity被回收,fragment恢复处理
- 创建第一个ionic APP项目
- Notepad++中zen-Coding使用
- Android ToolBar 基本使用
- Objective-C多个UIButton同时点击问题
- LeetCode 204 Count Primes(质数计数)(*)
- PHP学习(3)—在HTML中嵌入PHP
- java常用工具类之DES和Base64加密解密类
- Codeforces Round #341 (Div. 2) problemE Wet Shark and Blocks 矩阵乘法 dp
- 记自己的成长领悟和学习方法。
- 3 广义逆矩阵
- ehcache的简单使用
- ElasticSearch——2.X总结
- 【转】linux下杀死进程(kill)的N种方法