输出第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]);
}
}

测试结果

最新文章

  1. JAVA使用SAX解析XML文件
  2. Brn系列网上商城数据库说明文档
  3. 深入理解 KVC\KVO 实现机制 — KVC
  4. jquery checkbox全选,全不选,反选方法,jquery checkbox全选只能操作一次
  5. mysql空间数据相关操作
  6. 向MyEclipse中导入项目要注意的问题
  7. 操作Sql数据库帮助类
  8. Chapter 8 工厂方法模式
  9. xp安装maven
  10. java1环境与简介
  11. [八]JavaIO之FileInputStream 与 FileOutputStream
  12. 在Laravel中使用数据库事务以及捕获事务失败后的异常
  13. Linux基础操作二
  14. LeetCode(120):三角形最小路径和
  15. Android系统启动流程(三)解析SystemServer进程启动过程
  16. linux下mysql 5.7.22 安装
  17. 通过html页面打开Android本地的app
  18. PHP开发中,让var_dump调试函数输出更美观 ^_^#
  19. 【CSS】清除浮动的五种方式
  20. StretchBlt函数和BitBlt函数的用法

热门文章

  1. hibernate数据库方言
  2. 你需要知道的10位Java开发牛人
  3. 使用Notify 和 wait ,使用Linklist实现生产者消费者问题
  4. 删除顺序链表中重复的数 (一) leecode
  5. Bzoj 3343: 教主的魔法 分块,二分
  6. C++ 把输出结果写入文件/从文件中读取数据
  7. flushall()函数的用法
  8. Closure Compiler(封闭编辑器), Closure Inspector, Closure Templates, 封闭图书馆(Closure Library) Google- 摘自网络
  9. jQuery ajax传递特殊字符参数(例如+)
  10. MongoDB 复制集(二) 选举 自动故障切换