Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

Sieve of Eratosthenes

class Solution {
public:
    int countPrimes(int n) 
  {
if(n < 2) return 0;
vector
<bool> rec(n + 1, false); int bound = floor(sqrt(n)); int removed = 0; for (int i = 2; i <= bound; i++) { if (!rec[i]) { for (int j = 2*i; j < n; j += i) { if (!rec[j]) { rec[j] = true; removed++; } } } } return n - 2 - removed; } };
posted on 2015-04-30 01:04  Tonix  阅读(142)  评论(0)    收藏  举报