当前位置:网站首页>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);
}
}
边栏推荐
- 京东获取商品历史价格信息 API
- linux查看redis版本命令(linux查看mysql版本号)
- Returns a zero-length array or empty collection, do not return null
- GAC Honda Safety Experience Camp: "Danger" is the best teacher
- [PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
- idea中搜索具体的字符内容的快捷方式
- 【Yugong Series】July 2022 Go Teaching Course 023-List of Go Containers
- Made with Flutter and Firebase!counter application
- Apache EventMesh 分布式事件驱动多运行时
- INeuOS industrial Internet operating system, the equipment operational business and "low code" form development tools
猜你喜欢
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
请问我的这段sql中sql语法哪里出了错
ReentrantLock原理(未完待续)
Three.js入门
【码蹄集新手村600题】不通过字符数组来合并俩个数字
Getting Started with Tkinter
华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
Kotlin coroutines: continuation, continuation interceptor, scheduler
MySQL---单行函数
Tkinter 入门之旅
随机推荐
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
全平台GPU通用AI视频补帧超分教程
移动web开发02
【码蹄集新手村600题】不通过字符数组来合并俩个数字
【愚公系列】2022年07月 Go教学课程 025-递归函数
京东获取商品历史价格信息 API
Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...
The article you worked so hard to write may not be your original
多线程之锁
【NLP】什么是模型的记忆力!
leetcode 665. Non-decreasing Array
Cache and Database Consistency Solutions
MySQL---多表查询
Architect 04 - Application Service Encryption Design and Practice
统计UTF-8字符串中的字符函数
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
Getting Started with Tkinter
c语言解析json字符串(json对象转化为字符串)
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
使用 Flutter 和 Firebase 制作!计数器应用程序