当前位置:网站首页>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);
}
}
边栏推荐
- All-platform GPU general AI video supplementary frame super-score tutorial
- 第六章
- 【AcWing】The 62nd Weekly Match 【2022.07.30】
- Write a database document management tool based on WPF repeating the wheel (1)
- 35 MySQL interview questions and diagrams, this is also easy to understand
- Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
- 学生管理系统第一天:完成登录退出操作逻辑 PyQt5 + MySQL5.8
- 使用 Flutter 和 Firebase 制作!计数器应用程序
- 全平台GPU通用AI视频补帧超分教程
- MySQL---多表查询
猜你喜欢
统计UTF-8字符串中的字符函数
ReentrantLock原理(未完待续)
MATLAB程序设计与应用 2.4 MATLAB常用内部函数
Socket回顾与I/0模型
杰理语音芯片ic玩具芯片ic的介绍_AD14NAD15N全系列开发
广汽本田安全体验营:“危险”是最好的老师
STM32 full series development firmware installation guide under Arduino framework
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
【NLP】什么是模型的记忆力!
All-platform GPU general AI video supplementary frame super-score tutorial
随机推荐
MySQL---创建和管理数据库和数据表
Write a database document management tool based on WPF repeating the wheel (1)
MySQL---排序与分页
性能优化:记一次树的搜索接口优化思路
Arduino框架下STM32全系列开发固件安装指南
Istio介绍
Made with Flutter and Firebase!counter application
【Yugong Series】July 2022 Go Teaching Course 023-List of Go Containers
INeuOS industrial Internet operating system, the equipment operational business and "low code" form development tools
新型电信“套路”,我爸中招了!
Thymeleaf是什么?该如何使用。
rj45 to the connector Gigabit (Fast Ethernet interface definition)
leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
财务盈利、偿债能力指标
Tkinter 入门之旅
Short-circuit characteristics and protection of SiC MOSFETs
统计UTF-8字符串中的字符函数
Introduction of Jerry voice chip ic toy chip ic_AD14NAD15N full series development
这位985教授火了!当了10年博导,竟无一博士毕业!
多线程之锁