当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
Made with Flutter and Firebase!counter application
Apache EventMesh distributed event-driven multi-runtime
ResNet的基础:残差块的原理
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
架构师04-应用服务间加密设计和实践
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
AI 自动写代码插件 Copilot(副驾驶员)
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
Short-circuit characteristics and protection of SiC MOSFETs
随机推荐
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
微信小程序的路由拦截
新型电信“套路”,我爸中招了!
MySQL---sort and pagination
INeuOS industrial Internet operating system, the equipment operational business and "low code" form development tools
GAC Honda Safety Experience Camp: "Danger" is the best teacher
Basic Grammar Introduction of Carbon Tutorial (Tutorial)
Architect 04 - Application Service Encryption Design and Practice
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
Linux环境redis集群搭建「建议收藏」
pytorch lstm时间序列预测问题踩坑「建议收藏」
AI 自动写代码插件 Copilot(副驾驶员)
MySQL---Subqueries
【NLP】什么是模型的记忆力!
【AcWing】第 62 场周赛 【2022.07.30】
linux查看redis版本命令(linux查看mysql版本号)
Tkinter 入门之旅
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!...
idea中搜索具体的字符内容的快捷方式
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao