输出第N个素数
2024-09-22 22:34:02
输出第N个素数
public class FindNthPrime
{
public static void main(String[] args){
int N = Integer.parseInt(args[0]); //要求输出第 N 个素数
int[] PrimesVector = new int[N]; // 存储已经找到的素数
PrimesVector[0] = 2; //第一个素数是2
int CntPrime = 1; //目前找到的素数的数目是1
for(int i = 3; CntPrime < N; i++)
{
boolean isPrime = true;
// 因为非素数可以拆成素数的乘积,所以只需要考虑已经找到的素数
for (int j = 0; j < CntPrime && PrimesVector[j]*PrimesVector[j] <= i; j++)
{
if ( i % PrimesVector[j] == 0)
{
isPrime = false;
break; //跳出循环
}
}
if (isPrime)
{
CntPrime++;
PrimesVector[CntPrime-1] = i;
}
}
System.out.println(" The " + N + "th prime is " + PrimesVector[N-1]);
}
}
测试结果
最新文章
- JAVA使用SAX解析XML文件
- Brn系列网上商城数据库说明文档
- 深入理解 KVC\KVO 实现机制 — KVC
- jquery checkbox全选,全不选,反选方法,jquery checkbox全选只能操作一次
- mysql空间数据相关操作
- 向MyEclipse中导入项目要注意的问题
- 操作Sql数据库帮助类
- Chapter 8 工厂方法模式
- xp安装maven
- java1环境与简介
- [八]JavaIO之FileInputStream 与 FileOutputStream
- 在Laravel中使用数据库事务以及捕获事务失败后的异常
- Linux基础操作二
- LeetCode(120):三角形最小路径和
- Android系统启动流程(三)解析SystemServer进程启动过程
- linux下mysql 5.7.22 安装
- 通过html页面打开Android本地的app
- PHP开发中,让var_dump调试函数输出更美观 ^_^#
- 【CSS】清除浮动的五种方式
- StretchBlt函数和BitBlt函数的用法
热门文章
- hibernate数据库方言
- 你需要知道的10位Java开发牛人
- 使用Notify 和 wait ,使用Linklist实现生产者消费者问题
- 删除顺序链表中重复的数 (一) leecode
- Bzoj 3343: 教主的魔法 分块,二分
- C++ 把输出结果写入文件/从文件中读取数据
- flushall()函数的用法
- Closure Compiler(封闭编辑器), Closure Inspector, Closure Templates, 封闭图书馆(Closure Library) Google- 摘自网络
- jQuery ajax传递特殊字符参数(例如+)
- MongoDB 复制集(二) 选举 自动故障切换