当前位置:网站首页>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);
}
}
边栏推荐
- MySQL---聚合函数
- Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
- 【愚公系列】2022年07月 Go教学课程 025-递归函数
- API for JD.com to obtain historical price information of commodities
- 全平台GPU通用AI视频补帧超分教程
- Getting Started with Tkinter
- Book of the Month (202207): The Definitive Guide to Swift Programming
- 关注!海泰方圆加入《个人信息保护自律公约》
- MySQL---Basic select statement
- Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
猜你喜欢

全平台GPU通用AI视频补帧超分教程

MySQL---单行函数

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

35道MySQL面试必问题图解,这样也太好理解了吧

广汽本田安全体验营:“危险”是最好的老师

ThreadLocal

Unity 之 音频类型和编码格式介绍

The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao

华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全

AI 自动写代码插件 Copilot(副驾驶员)
随机推荐
MySQL - single function
京东获取商品历史价格信息 API
【愚公系列】2022年07月 Go教学课程 025-递归函数
手把手教你学会部署Nestjs项目
leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
Basic configuration of OSPFv3
杰理语音芯片ic玩具芯片ic的介绍_AD14NAD15N全系列开发
Socket回顾与I/0模型
npm 更改为淘宝镜像的方法[通俗易懂]
抖音根据关键词取视频列表 API
10 Ways to Keep Your Interface Data Safe
What's wrong with the sql syntax in my sql
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
MySQL---operator
常用的安全渗透测试工具(渗透测试工具)
每月一书(202207):《Swift编程权威指南》
几款永久免费内网穿透,好用且简单(内网穿透教程)
请问我的这段sql中sql语法哪里出了错
迁移学习——Domain Adaptation
浅谈网络安全之算法安全