BigInger isProbablePrime
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的更多相关文章
- Java基础知识【下】( 转载)
http://blog.csdn.net/silentbalanceyh/article/details/4608360 (最终还是决定重新写一份Java基础相关的内容,原来因为在写这一个章节的时候没 ...
- java中的大数BigInteger
compareTo比较大小 equals比较是否相等 ,不能用== while(cin.hasNext())//等价于!=EOF n=cin.nextBigInteger();//读入一个大整数 Sy ...
- 转!java基础笔记
原博文地址:http://blog.csdn.net/u012152619/article/details/48024345 Java标识符 Java所有的组成部分都需要名字.类名.变量名以及方法名都 ...
- Scala HandBook
目录[-] 1. Scala有多cool 1.1. 速度! 1.2. 易用的数据结构 1.3. OOP+FP 1.4. 动态+静态 1.5. DSL 1.6 ...
- Java在ACM中的应用
Java在ACM中的应用 —. 在java中的基本头文件(java中叫包) import java.io.*; import java.util.*; //输入Scanner import java. ...
- Effective Java 56 Adhere to generally accepted naming conventions
Typographical naming conventions Identifier Type Type Examples Package com.google.inject, org.joda.t ...
- 学习 BigInteger
以下是摘抄与其他人的: JAVA之BigInteger 用Java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂.用Java刷了一些题,感觉Java还不错,在处理高精度和 ...
- java BigInteger
用Java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂.用Java刷了一些题,感觉Java还不错,在处理高精度和进制转换中,调用库函数的来处理.下面是写的一些Java中 ...
- [Effective Java]第八章 通用程序设计
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
随机推荐
- ArrowLayer : A coustom layer animation
Since my other answer (animating two levels of masks) has some graphics glitches, I decided to try r ...
- jquery操作input值总结
获取选中的值获取一组radio被选中项的值var item = $('input[@name=items][@checked]').val(); 获取select被选中项的文本var item = $ ...
- Razor视图引擎-基础语法
所有以 @开头 或 @{ /* 代码体 */ } (在@与{直接不得添加任何空格) 的部分代码都会被ASP.NET引擎进行处理. 在 @{ /*代码体*/ } 内的代码每一行都必须以";& ...
- 自适应布局,响应式布局以及rem,em区别
一.自适应和响应式 先说共同点: 两者都是因为越来越多的 移动设备( mobile, tablet device )加入到互联网中来而出现的为移动设备提供更好的体验的技术.用技术来使网页适应从小到大( ...
- 【转载】Android 自动化测试 Emmagee
Emmagee 是一个性能测试小工具 用来监控指定被测应用在使用过程中占用机器的CPU, 内存,流量资源的性能小工具 Emmagee 介绍 Emmagee是网易杭州研究院QA团队开发的一个简单易上手的 ...
- 记一些之前忘记积累的问题(fiddler 学习、XP系统不能上网、XP不能装fiddler、注册表快速找到)
记一些之前忘记积累的问题: fiddler学习:http://www.cnblogs.com/kingwolf_JavaScript/archive/2012/11/07/FiddlerUI.html ...
- iOS 汉字拼音
获取汉字的拼音 #import <Foundation/Foundation.h> @interface NSString (Utils) /** * 汉字的拼音 * * @retur ...
- sass在mac中安装
$ curl -L https://get.rvm.io | bash -s stable $ source ~/.rvm/scripts/rvm $ rvm -v $ rvm install 2.0 ...
- 面试题---PHP
1.PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言. 2.echo,print和print_r的区别: echo和print都可以做 ...
- flume 不报错但是不能正常使用
flume-ng agent --conf ./conf/ --conf-file ./conf/test1.conf --name a1 -Dflume.root.logger=INFO,conso ...