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 ...
随机推荐
- php-fpm正在生成页面时,浏览器刷新后,php-fpm会退出吗?
好久没写博客了,因为没有啥可写. 之所以有此疑问,是因为看了一篇大牛的文章:PHP升级导致系统负载过高问题分析.看完后,其中有些文字触发了我这个想法,也想验证一下. 方案,用tcpdump抓包,用st ...
- Unity手游之路<六>游戏摇杆之Easy Touch 3教程
之前已经介绍过Unity自带的摇杆Joystick,它用起来很简单.但是它也存在很多局限,不能全部满足普通mmo手游的一些需求,例如:要能方便地更好素材:能指定在某个区域显示,或者只有在该区域触摸时才 ...
- Unicode和字符集小结
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:Unicode和字符集小结.
- WPF 多线程
写法3 private void button1_Click(object sender, RoutedEventArgs e) { System. ...
- 多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本号)
Hello everyone! For a week I've been looking on how to make a keyboard work! I managed to figure it ...
- Android 颜色渲染(六) RadialGradient 环形渲染
Android 颜色处理(六) RadialGradient 环形渲染 public RadialGradient(float x, float y, float radius, int[] colo ...
- [Whole Web] [AngularJS] Localize your AngularJS Application with angular-localization
It is best to start your application's localization efforts early in development, even if you only s ...
- cocos2dx lua学习笔记 <一> quick 3.5定义本身C++类是必然lua
请尊重原创 转载有名源:http://blog.csdn.net/wushao126/article/details/46660375 首先去官网下载最新的quick.配置好环境.创建一个luapro ...
- Java的递归算法
递归算法设计的基本思想是:对于一个复杂的问题,把原问题分解为若干个相对简单类同的子问题,继续下去直到子问题简单到可以直接求解,也就是说到了递推的出口,这样原问题就有递推得解. 关键要抓住的是: (1) ...
- wireshark抓包图解 TCP三次握手/四次挥手详解
http://www.seanyxie.com/wireshark%E6%8A%93%E5%8C%85%E5%9B%BE%E8%A7%A3-tcp%E4%B8%89%E6%AC%A1%E6%8F%A1 ...