第一种方法 The Sieve of Eratosthenes is one of the most efficient ways to find all prime numbers up to n.

The Sieve of Eratosthenes uses an extra O(n) memory and its runtime complexity is O(n log log n)

创建一个length = n的boolean数组 isPrime,每个元素初始化为true;

k = 2:(n-1),如果当前数k是prime,把 k^k - (n-1)/k *k的数 对映的 isPrime = false;

最后计算 从2到n-1 isPrime == true的个数。

代码:

public class Solution {
public int countPrimes(int n) {
boolean[] isPrimes = new boolean[n];
for(int i = 0; i < isPrimes.length; i++){
isPrimes[i] = true;
}

for(int k = 2; k <= (n-1)/k; k++){
if(isPrimes[k] == true){
for(int i = k; i <= (n-1)/k; i++){
isPrimes[i*k] = false;
}
}
}
int count = 0;
for(int i = 2; i< isPrimes.length; i++){
if(isPrimes[i] == true) count++;
}
return count;
}
}

还可以用DP来解决。

public class Solution {
public int countPrimes(int n) {

int count = 0;
int squareRoot = 1;
int number = 2;

List<Integer> list = new ArrayList<>();
for(int i = number; i < n; i++){
boolean isPrime = true;
if(squareRoot * squareRoot < i) squareRoot++;
for(int j = 0; j < list.size() && list.get(j) <= squareRoot;j++){
if(i%list.get(j) == 0){
isPrime = false;
break;
}
}
if(isPrime == true){
list.add(i);
count++;
}
}
return count;
}
}

Runtime: O(n*sqrt(n)/log(n))

最后,记录下最原始的求prime的方法 0-sqrt(n):

public class Solution {
public int countPrimes(int n) {

int count = 0;
int squareRoot = 1;
int number = 2;

for(int i = number; i < n; i++){
boolean isPrime = true;

for(int divisor = 2; divisor <= (int) (Math.sqrt(i)) ; divisor++){
if(i%divisor == 0){
isPrime = false;
break;
}
}
if(isPrime == true){
count++;
}
}
return count;
}
}

转载于:https://www.cnblogs.com/5683yue/p/5118239.html

Jan 09 - Count Primes; Mathematics; Optimization; Primes; DP;相关推荐

  1. 【bzoj 1833】【codevs 1359】 [ZJOI2010]count 数字计数(数位dp)

    1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 2774  Solved: 1230 [Submit ...

  2. HDU 3336 Count the string(KMP+DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意:给你一个字符串,计算其所有前缀在该字符串出现的次数的总和. 思路:next[j]=i,代表 ...

  3. hdu 4472 Count(递推即dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4472 代码: #include <cstdio> #include <cstring ...

  4. USACO 2008 Jan Gold 3.Cell Phone Network 树形dp

    本题的大意就是在一棵树上选一些节点,被选中的节点可以覆盖他相 邻的点,问最少需要放几个节点\text{本题的大意就是在一棵树上选一些节点,被选中的节点可以覆盖他相 邻的点,问最少需要放几个节}\\\t ...

  5. CF 1625C. Road Optimization(DP)

    Linking 题意: 在一条直线道路从A到走向B点,A点位于0位置,B点位于m位置. 道路上有n个时速牌,第i个牌上标有ai,表示从该时速牌到下一时速牌这段距离中,每走一个位置耗时ai分钟.(初始位 ...

  6. Jan 09 - Number of 1 Bits; Bit Operation;

    和 reverse Bits同一类型 代码: public class Solution { // you need treat n as an unsigned value public int r ...

  7. 2019.02.09 bzoj4455: [Zjoi2016]小星星(容斥原理+dp)

    传送门 题意简述:给一张图和一棵树(点数都为n≤17n \le17n≤17),问有多少种给树的标号方法方法使得图中去掉多余的边之后和树一模一样. 思路: 容斥好题啊. 考虑fi,jf_{i,j}fi, ...

  8. 可截断素数(Truncatable primes)

    可截断素数是当从一端连续删除数字时,得到的仍旧是素数的一个素数. 举例 997是左截短素数,因为997, 97 和 7 都是素数. 7393是右截断素数,因为7393, 739, 73 和 7 都是素 ...

  9. Codeforces757E.Bash Plays With Functions(积性函数 DP)

    题目链接 \(Description\) q次询问,每次给定r,n,求\(F_r(n)\). \[ f_0(n)=\sum_{u\times v=n}[(u,v)=1]\\ f_{r+1}(n)=\s ...

最新文章

  1. 一个多年网络工程师总结的工作实用经验
  2. python财务报表分析-用Python爬取东方财富网上市公司财务报表
  3. Swift开发iOS项目实战视频教程(一)---iOS真简单
  4. 【原创】backbone1.1.0源码解析之Events
  5. 052、overlay如何实现跨主机通信?(2019-03-19 周二)
  6. python分析nginx日志的ip(中篇一)
  7. BugkuCTF-Reverse题逆向入门
  8. qt实现对话框选择文件路径并保存(简易版)
  9. activemq后台管理 看topic消息_「Java」 - SpringBoot amp; ActiveMQ
  10. python自学入门-初学 Python 者自学 Anaconda 的正确姿势是什么?
  11. .博弈论之Best Response
  12. 红米充电短路 红米note3充电短路 无法充电
  13. 阿里巴巴中台战略思想与架构实战笔记
  14. 阿里云acp认证, 阿里云acp考试介绍
  15. android 华为部分手机剪裁图片模糊问题
  16. 什么蓝牙耳机好?测评达人精选五款性价比高蓝牙耳机推荐
  17. FME 安装破解及与ArGIS冲突的解决方法
  18. 信号处理系列之限幅器(Limiter_FC)
  19. JVM知识点(全,一篇搞定)
  20. opIndex多级索引笔记

热门文章

  1. Spring Boot 之异步执行方法
  2. Mysql Workbench中EER Diagram逆向生成表
  3. MYSQL禁用与启用事件
  4. Vue中过滤器的使用
  5. 服务器系统盘单独硬盘,我的服务器今天加了个硬盘,可以实现双系统吗?
  6. mysql 读取oracle_RobotFramework读取mysql和oracle数据库
  7. ktv点歌系统安卓_喜事汇KTV设备更新语音点歌系统,特推出一下优惠活动。转发朋友圈有惊喜。...
  8. Effective C# 摘录(3) - 使用C#表达设计
  9. JS的forEach和map方法的区别
  10. 快速实现MySQL迁移到Redis