Leetcode Count Prime
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的更多相关文章
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [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 ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- LeetCode Count of Range Sum
原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...
- LeetCode Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
随机推荐
- VMWare虚拟机USB连接问题
错误31:连接到系统上的设备没有发挥作用 原文链接 描述 常用 VMware 虚拟机的有事应该遇到这种情况,就是装完 VMware ,启动时 VMware 下面会有个黄框中有" USB di ...
- 使用Hadoop打造私有云盘之API操作
项目介绍:使用hadoop实现云盘的增删读获取列表功能,hadoop不支持数据修改,特性是一次写入多次读取.主流的网盘也不支持该功能.今天我们用hdfs的FileSystem实现这些操作. 1.上传功 ...
- 问题-Delphi编译到最后Linking时总是出现与ntdll.dll有关的错误还有Fatal Error Out of memory错误
1.跳出错误法 ===================================================在主界面的implementation {$R *.dfm} 下放入以下代码: ...
- JS自定义事件(Dom3级事件下)
原文出处: http://www.w3cfuns.com/notes/11861/e21736a0b15bceca0dc7f76d77c2fb5a.html . 我拿出作者中的一段,感谢作者原创. ...
- iOS开发:创建真机调试证书
关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架发布问题.今天就着重说一下关于针对于苹果 ...
- BZOJ 3280: 小R的烦恼 & BZOJ 1221: [HNOI2001] 软件开发
3280: 小R的烦恼 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 399 Solved: 200[Submit][Status][Discuss ...
- java使用xheditor Ajax异步上传错误
java使用xheditor Ajax异步上传时候错误如下:the request doesn't contain a multipart/form-data or multipart/mixed s ...
- Java开发中常见的危险信号(中)
本文来源于我在InfoQ中文站原创的文章,原文地址是:http://www.infoq.com/cn/news/2013/12/common-red-flags-in-java-1 Dustin Ma ...
- JavaWeb文件的上传与下载(1)
经常用到的上传: 头像上传,资料分享等 文件上传的步骤 1.指定表单类型为文件上传表单 enctype="multipart/form-data" 2.表单提交方式必须为:post ...
- Edit显示行号
Edit显示代码行号 关键点 使用这个类然后关联Edit的变量为 LineNumberEdit类型的 实现过程 //////////////////////////////////////////// ...