Count Primes
程序员文章站
2024-03-14 20:42:29
...
https://www.lintcode.com/problem/count-primes/description
public class Solution {
/**
* @param n: a integer
* @return: return a integer
*/
public int countPrimes(int n) {
// write your code here
int res = 0;
for (int i = 2; i < n; i++) {
if (isPrime(i)) {
res++;
}
}
return res;
}
private boolean isPrime(int i) {
for (int j = 2; j <= i / j; j++) {
if (i % j == 0) {
return false;
}
}
return true;
}
}
推荐阅读
-
Count Primes
-
Count Primes
-
LeetCode-038:Count and Say
-
PHP中检索字符串的方法分析【strstr与substr_count方法】
-
Bitmasking and Dynamic Programming 实例 :Count ways to assign unique cap to every person
-
【Leetcode】204. Count Primes 204. 计数质数
-
数据库学习之MySQL (十一)—— 统计函数 COUNT MIN MAX AVG SUM
-
PHP中检索字符串的方法分析【strstr与substr_count方法】
-
LintCode 382 [Triangle Count]
-
Triangle Count (Lintcode 382)