204. Count Primes

Easy

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.
package leetcode.easy;

public class CountPrimes {
public int countPrimes(int n) {
int count = 0;
for (int i = 1; i < n; i++) {
if (isPrime(i)) {
count++;
}
}
return count;
} private static boolean isPrime(int number) {
boolean flag = true;
if (number < 2) {
flag = false;
} else if (number == 2) {
flag = true;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
flag = false;
break;
}
}
}
return flag;
} @org.junit.Test
public void test() {
System.out.println(countPrimes(10));
}
}

LeetCode_204. Count Primes的更多相关文章

  1. [leetcode] Count Primes

    Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...

  2. 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 ...

  3. HDU 5901 Count primes 论文题

    Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...

  4. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  5. hdu 5901 Count primes (meisell-Lehmer)

    Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  6. 【刷题-LeetCode】204. Count Primes

    Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...

  7. 204. Count Primes - LeetCode

    Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...

  8. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  9. Count Primes - LeetCode

    examination questions Description: Count the number of prime numbers less than a non-negative number ...

随机推荐

  1. C++ 类的静态成员

    https://www.cnblogs.com/lvxiaoning/p/7598062.html

  2. C# CRC16校验码 1.0

      /// <summary> /// 计算CRC16校验码 1.0 /// </summary> /// <param name="bytes"&g ...

  3. Kubernetes 学习3 kubeadm初始化k8s集群

    一.k8s集群 1.k8s整体架构图 2.k8s网络架构图 二.基于kubeadm安装k8s步骤 1.master,nodes:安装kubelet,kubeadm,docker 2.master: k ...

  4. 洛谷 P2872 [USACO07DEC]道路建设Building Roads 题解

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  5. 交互设计算法基础(3) - Quick Sort

    int pivotIndex, pivot, swapIndex; void swap(int[] arr, int x, int y) { int temp = arr[x]; arr[x] = a ...

  6. golang 无缓冲channel

    golang 无缓冲channel package main import "fmt" func main() { // 1S =1000ms //1ms = 1000us //1 ...

  7. Prometheus Consul实现自动服务发现

    Prometheus Consul实现自动服务发现   1.概述 Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件. Consul 由 HashiCorp公司用Go语言开发 ...

  8. 【随记】安装SQL Server 2008 R2 提示创建usersettings/microsoft.sqlserver.configuration.landingpage.properties.se

    在安装SQL Server 2008 R2 提示创建usersettings/microsoft.sqlserver.configuration.landingpage.properties.se.. ...

  9. 【洛谷】P4167 [Violet]樱花

    题面 又懒得弄题面,开个传送门吧 分析 人生第一次切数学题,我们先把方程写出来 $$\frac {1}{x}+\frac {1}{y}=\frac {1}{n!}$$ 现在我们知道的条件是x,y都是正 ...

  10. Firefox disable search in the address bar

    disable search in the address bar Hi oitconz, setting keyword.enabled to false prevents Firefox from ...