JAVA BigInteger 成员函数: isProbablePrime

public boolean isProbablePrime(int certainty)

如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false。如果 certainty <= 0,则返回 true。

参数:

certainty - 调用方允许的不确定性的度量。如果该调用返回 true,则此 BigInteger 是素数的概率超出 (1 - 1/2certainty)。此方法的执行时间与此参数的值是成比例的。

返回:

如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false。

java中的isProbablePrime函数是针对BigInteger类的一个素数判断函数,它的实现原理其实并不复杂,只是要分许多情况讨论,要用到Miller-Rabin素数测试和Lucas-Lehmer测试,它是一个概率算法,返回的结果:一个数不是素数或者一个数可能是素数。下面只给出它在jdk1.6 src中的主要源代码:

 public boolean isProbablePrime(int certainty) {
if (certainty <= 0)
return true;
BigInteger w = this.abs();
if (w.equals(TWO))
return true;
if (!w.testBit(0) || w.equals(ONE))
return false; return w.primeToCertainty(certainty, null);
} public boolean testBit(int n) {
if (n < 0)
throw new ArithmeticException("Negative bit address"); return (getInt(n >>> 5) & (1 << (n & 31))) != 0;
} private int getInt(int n) {
if (n < 0)
return 0;
if (n >= mag.length)
return signInt(); int magInt = mag[mag.length-n-1]; return (signum >= 0 ? magInt :
(n <= firstNonzeroIntNum() ? -magInt : ~magInt));
} boolean primeToCertainty(int certainty, Random random) {
int rounds = 0;
int n = (Math.min(certainty, Integer.MAX_VALUE-1)+1)/2; // The relationship between the certainty and the number of rounds
// we perform is given in the draft standard ANSI X9.80, "PRIME
// NUMBER GENERATION, PRIMALITY TESTING, AND PRIMALITY CERTIFICATES".
int sizeInBits = this.bitLength();
if (sizeInBits < 100) {
rounds = 50;
rounds = n < rounds ? n : rounds;
return passesMillerRabin(rounds, random);
} if (sizeInBits < 256) {
rounds = 27;
} else if (sizeInBits < 512) {
rounds = 15;
} else if (sizeInBits < 768) {
rounds = 8;
} else if (sizeInBits < 1024) {
rounds = 4;
} else {
rounds = 2;
}
rounds = n < rounds ? n : rounds; return passesMillerRabin(rounds, random) && passesLucasLehmer();
}

BigInger isProbablePrime的更多相关文章

  1. Java基础知识【下】( 转载)

    http://blog.csdn.net/silentbalanceyh/article/details/4608360 (最终还是决定重新写一份Java基础相关的内容,原来因为在写这一个章节的时候没 ...

  2. java中的大数BigInteger

    compareTo比较大小 equals比较是否相等 ,不能用== while(cin.hasNext())//等价于!=EOF n=cin.nextBigInteger();//读入一个大整数 Sy ...

  3. 转!java基础笔记

    原博文地址:http://blog.csdn.net/u012152619/article/details/48024345 Java标识符 Java所有的组成部分都需要名字.类名.变量名以及方法名都 ...

  4. Scala HandBook

    目录[-] 1.   Scala有多cool 1.1.     速度! 1.2.     易用的数据结构 1.3.     OOP+FP 1.4.     动态+静态 1.5.     DSL 1.6 ...

  5. Java在ACM中的应用

    Java在ACM中的应用 —. 在java中的基本头文件(java中叫包) import java.io.*; import java.util.*; //输入Scanner import java. ...

  6. Effective Java 56 Adhere to generally accepted naming conventions

    Typographical naming conventions Identifier Type Type Examples Package com.google.inject, org.joda.t ...

  7. 学习 BigInteger

    以下是摘抄与其他人的: JAVA之BigInteger 用Java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂.用Java刷了一些题,感觉Java还不错,在处理高精度和 ...

  8. java BigInteger

    用Java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂.用Java刷了一些题,感觉Java还不错,在处理高精度和进制转换中,调用库函数的来处理.下面是写的一些Java中 ...

  9. [Effective Java]第八章 通用程序设计

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

随机推荐

  1. matlab中的数据结构

    一.cell 1. function: num2cell(A,n) n表示如何把A中的数据转换为cell. n=1表示把每列的所有行转换为cell:n=2表示把每行的所有列转换为cell. >& ...

  2. Magento显示多货币,Magento 多货币设置

    System - Configuration - Currency Setup 在右边Currency Options里的Allowed currencies勾选, 然后 System - Manag ...

  3. iOS 微信分享

    1.注册微信开放平台账号:https://open.weixin.qq.com 2.创建应用 设置图片可以使用一个小工具,详情http://www.cnblogs.com/czq1989/p/5073 ...

  4. (实用篇)微信支付扫码支付php版

    本文实例为大家分享了php微信扫码支付源码,供大家参考,具体内容如下 代码中包含四个文件createUrl.php.ArrayToXML.php.returnGoodsUrl.php.notifyUr ...

  5. dede currentstyle属性完美解决方案

    问题一.dede让channelartlist标签支持currentstyle属性 完美解决 打开include\taglib\channelartlist.lib.php找到$pv->Fiel ...

  6. metagenome 简介

    宏基因组 ( Metagenome)(也称微生物环境基因组 Microbial Environmental Genome, 或元基因组) .是由 Handelsman 等 1998 年提出的新名词, ...

  7. Java笔记7-多态父类静态

    多态的应用-面向父类编程 1.对象的编译时类型写成父类 2.方法的返回类型写成父类 3.方法的参数类型写成父类 编译时类型:对象的声明时类型,在于编译期间 运行时类型:new运算符后面的类型 编译时类 ...

  8. windows系统下npm 全局安装路径问题

    安装了nodejs之后,npm的路径默认一直都是appData,表示很讨厌,于是尝试修改在安装目录(D盘空间很大啊) 安装目录:D:\program files\nodejs 一.在nodejs下新建 ...

  9. 转:WebService通用接口

    看到许多中小项目的webservice接口的源代码,不禁有个吐槽的冲动.除了会用CXF,Axis2等神级框架,其他的懒得动了,都是Ctrl+V,Ctrl+C,把其他模块的Request,Respons ...

  10. java中文乱码分析整理

    在JavaWeb应用开发中,经常会出现页面中本该显示中文的地方却是乱码的情况.究其原因,主要是由于在Web组件之间.或Web组件与浏览器.与数据库所使用的字符集标准不统一,Web应用程序运行过程中,中 ...