当前位置:网站首页>find prime numbers up to n
find prime numbers up to n
2022-07-31 19:28:00 【swofford】
方法一:基础遍历
用数 i 去除以 2到i-1的每个数,Once the integer isbreak跳出;
List<Integer> list=new ArrayList<>();
for(int i=2;i<=n;i++){
boolean a=true;
for(int j=2;j<i;j++){
if(i%j==0){
a=false;
break; // 跳出循环
}
}
if(a){
// 假设jIt can traverse to the end without being divisible
list.add(i);
}
}
或者
这种方法 2will not be able to enter the second onefor,所以将2先添加;
List<Integer> list=new ArrayList<>();
for(int i=2;i<=n;i++){
if(i==2) list.add(i);
for(int j=2;j<i;j++){
if(i%j==0){
break;
}
// 如果j It can be traversed to the end without being divisible
if(j==i-1 && i%j!=0){
list.add(i);
}
}
}
方法二:用sqrt优化
The factors of integers are large and small,The smaller factor does not exceed the integer平方根,
注意 :j < = Math.sqrt(i) , 有等号 !
List<Integer> list=new ArrayList<>();
for(int i=2;i<=n;i++){
boolean a=true;
for(int j=2;j<=Math.sqrt(i);j++){
if(i%j==0){
a=false;
break; // 跳出循环
}
}
if(a){
// 假设jIt can traverse to the end without being divisible
list.add(i);
}
}
方法三:用listThe prime numbers in j
继续探索,Questions turn into judgmentsn能否被[2,sqrt(n)]odd integer division between,The composite numbers within these odd numbers are redundant.Because any composite number can be decomposed into the product of several prime numbers in front of it,If it is not divisible by the prime number preceding it,is also not divisible by this composite number.So the divisible range is reduced to [2,sqrt(n)]之间的素数.The purpose of this question is to find prime numbers,Therefore, the obtained prime numbers can be placed in the array for judgment.
注意:
- j 索引从 0开始 !
- 此时 j 是索引 !j 不能等于 list.size()
List<Integer> list=new ArrayList<>();
for(int i=2;i<=n;i++){
boolean a=true;
for(int j=2;j<=list.size() && j<=Math.sqrt(i);j++){
if(i%j==0){
a=false;
break;
}
}
if(a){
list.add(i);
}
}
边栏推荐
- 2022 Android interview summary (with interview questions | source code | interview materials)
- Tkinter 入门之旅
- iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
- Socket回顾与I/0模型
- 使用 Flutter 和 Firebase 制作!计数器应用程序
- The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
- 性能优化:记一次树的搜索接口优化思路
- 全网一触即发,自媒体人的内容分发全能助手——融媒宝
- 【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
- 【PIMF】OpenHarmony 啃论文俱乐部—盘点开源鸿蒙三方库【3】
猜你喜欢

Getting Started with Tkinter

Apache EventMesh distributed event-driven multi-runtime

SiC MOSFET的短路特性及保护

这位985教授火了!当了10年博导,竟无一博士毕业!

Arduino框架下STM32全系列开发固件安装指南

PCB叠层设计

基于STM32 环形队列来实现串口接收数据

Three.js入门
![leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]](/img/91/284de3dcbb8d143d85775b314dd41c.png)
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
![[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]](/img/8c/22e083d2a9a4a6f983b985fe454893.png)
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
随机推荐
Linux环境redis集群搭建「建议收藏」
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
matplotlib ax bar color Set the color, transparency, label legend of the ax bar
MySQL---子查询
MySQL---基本的select语句
Go record - slice
The article you worked so hard to write may not be your original
性能优化:记一次树的搜索接口优化思路
spark报错OutOfMemory「建议收藏」
Batch (batch size, full batch, mini batch, online learning), iterations and epochs in deep learning
每月一书(202207):《Swift编程权威指南》
MySQL - multi-table query
基于WPF重复造轮子,写一款数据库文档管理工具(一)
京东获取商品历史价格信息 API
Basics of ResNet: Principles of Residual Blocks
Combinatorics Notes (6) Associative Algebra of Locally Finite Partially Ordered Sets, Möbius Inversion Formula
浅谈网络安全之算法安全
Socket回顾与I/0模型
杰理语音芯片ic玩具芯片ic的介绍_AD14NAD15N全系列开发
抖音根据关键词取视频列表 API