码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode Count Prime

时间:2015-04-27 19:46:35      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/xuanyuanchen/p/4460952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!