Description:

Count the number of prime numbers less than a non-negative number, n

Hint: The number n could be in the order of 100,000 to 5,000,000.

#define NO 0
#define YES 1 class Solution {
public:
int countPrimes(int n) {
if(n == 1)
return 0; int num;
int count = 0;
for(num = 2;num <= n;num++){
if(isPrime(num))
count++;
} return count;
} int isPrime(int n){
int i = 2;
for(;i<=sqrt(n);i++)
{
if( (n%i) == 0)
return NO;
} return YES;
} };

  

Leetcode Count Prime的更多相关文章

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

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

  2. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  3. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  4. [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  5. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  6. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  7. LeetCode Count of Range Sum

    原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...

  8. LeetCode Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  9. [leetcode] Count Primes

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

随机推荐

  1. C#调用VC dll输出参数

    最近做项目需要把以前Vc写的程序整合到VS2005来,用c#写的,以前VC的动态连接库写好了,重写比较麻烦,让C#调用VC dll就好了.但碰到了一个问题:VC中作为输出参数的char*类型的变量应该 ...

  2. Hadoop中java.lang.ClassCastException: partition解决方法

    java.lang.ClassCastException: partition.KpiWritable cannot be cast to org.apache.hadoop.io.LongWrita ...

  3. NSDictionary或NSArray与JSON字符串相互转换

    在iOS 5 中,苹果引入了一个解析JSON串的NSJSONSerialization类.通过该类,我们可以完成JSON数据与NSDictionary和NSArray之间的转化. 以前,我记得我用的是 ...

  4. D3D游戏编程系列(三):自己动手编写即时战略游戏之寻路

    说起即时战略游戏,不得不提的一个问题是如何把一个物体从一个位置移动到另一个位置,当然,我说的不是瞬移,而是一个移动的过程,那么在这个移动的过程中我们如何来规划路线呢,这就不得不提到寻路了. 我所了解到 ...

  5. 掌握jQuery插件开发

    进行jQuery插件开发前,首先要知道两个问题:什么是jQuery插件?jQuery插件如何使用? 第一个问题,jQuery插件就是用来扩展jQuery原型对象的一个方法,简单来说就是jQuery插件 ...

  6. ONIX 实例

    <?xml version="1.0"?><!DOCTYPE ONIXMessage SYSTEM"http://www.editeur.org/oni ...

  7. Cocos2d-x 2.0 自适应多种分辨率

    转自:http://dualface.github.io/blog/2012/08/17/cocos2d-x-2-dot-0-multi-resolution/ cocos2d-x 2.0 提供一个极 ...

  8. ThinkPHP3.1新特性: 多层MVC支持

    ThinkPHP基于MVC(Model-View-Controller,模型-视图-控制器)模式,不过均支持多层(multi-Layer)设计. 模型(Model)层:默认的模型层由Model类构成, ...

  9. 使用ThinkPHP框架高速开发站点(多图)

    使用ThinkPHP框架高速搭建站点 这一周一直忙于做实验室的站点,基本功能算是完毕了.比較有收获的是大概了解了ThinkPHP框架.写一些东西留作纪念吧.假设对于相同是Web方面新手的你有一丝丝帮助 ...

  10. Ubuntu上VNC 配置

    Ubuntu下VNC配置文章分类:操作系统通过将服务器配置成VNC SERVER,可以让其他主机使用图形方式登录这台服务器. 在ubuntu下配置vnc server很简单,方法如下: 服务器端: 1 ...