博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 204 Count Primes(质数计数)(*)
阅读量:6996 次
发布时间:2019-06-27

本文共 3063 字,大约阅读时间需要 10 分钟。

版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50617645

翻译

计算小于一个非负整数n的质数的个数。

原文

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

分析

这道题以前遇到过,当时是用的最笨的办法,现在也没什么好想法,又恰好题目有提示,我就点开了。题目的提示是一条一条给出来的,我也就逐个的全点开了,感觉好失败……

这里写图片描述

public int countPrimes(int n) {   int count = 0;   for (int i = 1; i < n; i++) {      if (isPrime(i)) count++;   }   return count;}private boolean isPrime(int num) {   if (num <= 1) return false;   // Loop's ending condition is i * i <= num instead of i <= sqrt(num)   // to avoid repeatedly calling an expensive function sqrt().   for (int i = 2; i * i <= num; i++) {      if (num % i == 0) return false;   }   return true;}
The Sieve of Eratosthenes is one of the most efficient ways to find all prime numbers up to n. I promise that the concept is surprisingly simple.

这里写图片描述

这里写图片描述

public int countPrimes(int n) {   boolean[] isPrime = new boolean[n];   for (int i = 2; i < n; i++) {      isPrime[i] = true;   }   // Loop's ending condition is i * i < n instead of i < sqrt(n)   // to avoid repeatedly calling an expensive function sqrt().   for (int i = 2; i * i < n; i++) {      if (!isPrime[i]) continue;      for (int j = i * i; j < n; j += i) {         isPrime[j] = false;      }   }   int count = 0;   for (int i = 2; i < n; i++) {      if (isPrime[i]) count++;   }   return count;}

以上均为LeetCode原文……

把上面的代码翻译一下成如下:

class Solution {public:    bool isPrime(int num) {        if (num <= 1) return false;        for (int i = 2; i*i <= num; ++i) {            if (num%i == 0) return false;        }        return true;    }    int countPrimes(int n) {        bool *isPrime = new bool[n];        for (int i = 2; i < n; ++i) {            isPrime[i] = true;        }        for (int i = 2; i*i < n; ++i) {            if (!isPrime[i]) continue;            for (int j = i*i; j < n; j += i) {                isPrime[j] = false;            }        }        int count = 0;        for (int i = 2; i < n; ++i) {            if (isPrime[i]) count++;        }        return count;    }};

摘录一些代码:

class Solution {public:    int countPrimes(int n) {        switch(n) {            case 0:            case 1:            case 2: return 0;            case 3: return 1;            case 4:            case 5: return 2;            case 6:             case 7: return 3;            case 8:            case 9:             case 10:             case 11: return 4;            case 12:             case 13: return 5;            case 14:             case 15: return 6;            case 10000: return 1229;            case 499979: return 41537;            case 999983: return 78497;            case 1500000: return 114155;        }    }};
int countPrimes(int n) {    if(--n < 2) return 0;    int m = (n + 1)/2, count = m, k, u = (sqrt(n) - 1)/2;    bool notPrime[m] = {
0}; for(int i = 1; i <= u;i++) if(!notPrime[i]) for(k = (i+ 1)*2*i; k < m;k += i*2 + 1) if (!notPrime[k]) { notPrime[k] = true; count--; } return count;}
你可能感兴趣的文章
高效能TCP通讯基础组件Beetle.Express
查看>>
MyEclipse内存不足配置
查看>>
四舍五入网络Java保留两位小数
查看>>
MFC 循环界面假死的解决(MFC 按钮终止循环)
查看>>
详细解说九宫图比较常用的多控件布局
查看>>
程序员的出路在哪里?挣钱的机会来了续-福利来了,仿QQ界面,放出全部源码,打造创业框架及实现思路...
查看>>
C语言中的 (void*)0 与 (void)0
查看>>
DIV固定在页面某个位置,不随鼠标滚动而滚动
查看>>
android 根据SD卡中图片路径读取并显示SD中的图片——源代码
查看>>
浅析Android线程模型一 --- 转
查看>>
Cocos2d-x PluginX (二)增加新的Plugin
查看>>
python-django开发学习笔记四
查看>>
cocos2d-x开发记录:二,基本概念(导演,场景,层和精灵,场景切换,效果)...
查看>>
Binutils工具集中的一些比较常用的工具
查看>>
IoC在ASP.NET Web API中的应用
查看>>
Android手机 Fildder真机抓包
查看>>
jsp里面实现asp.net的Global文件内容。
查看>>
Oracle ROWID
查看>>
(转)为C# Windows服务添加安装程序
查看>>
使用Team Foundation Server 2012源代码管理基本
查看>>