我是靠谱客的博主 多情书本,最近开发中收集的这篇文章主要介绍biginteger 原理_BigInteger源码解析,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

/**

* 返回的这个数不是素数的可能性为2^-100.但不会跳过任何一个素数

*

* @return the first integer greater than this {@code BigInteger} that

* is probably prime.

* @throws ArithmeticException {@code this < 0} or {@code this} is too large.

* @since 1.5

*/

public BigInteger nextProbablePrime() {

if (this.signum < 0)

throw new ArithmeticException("start < 0: " + this);

// Handle trivial cases

if ((this.signum == 0) || this.equals(ONE))

return TWO;

BigInteger result = this.add(ONE);

// Fastpath for small numbers

if (result.bitLength() < SMALL_PRIME_THRESHOLD) {

// Ensure an odd number

if (!result.testBit(0))

result = result.add(ONE);

while (true) {

// Do cheap "pre-test" if applicable

if (result.bitLength() > 6) {

long r = result.remainder(SMALL_PRIME_PRODUCT).longValue();

if ((r%3==0) || (r%5==0) || (r%7==0) || (r%11==0) ||

(r%13==0) || (r%17==0) || (r%19==0) || (r%23==0) ||

(r%29==0) || (r%31==0) || (r%37==0) || (r%41==0)) {

result = result.add(TWO);

continue; // Candidate is composite; try another

}

}

// All candidates of bitLength 2 and 3 are prime by this point

if (result.bitLength() < 4)

return result;

// The expensive test

if (result.primeToCertainty(DEFAULT_PRIME_CERTAINTY, null))

return result;

result = result.add(TWO);

}

}

// Start at previous even number

if (result.testBit(0))

result = result.subtract(ONE);

// Looking for the next large prime

int searchLen = getPrimeSearchLen(result.bitLength());

while (true) {

BitSieve searchSieve = new BitSieve(result, searchLen);

BigInteger candidate = searchSieve.retrieve(result,

DEFAULT_PRIME_CERTAINTY, null);

if (candidate != null)

return candidate;

result = result.add(BigInteger.valueOf(2 * searchLen));

}

}

最后

以上就是多情书本为你收集整理的biginteger 原理_BigInteger源码解析的全部内容,希望文章能够帮你解决biginteger 原理_BigInteger源码解析所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(86)

评论列表共有 0 条评论

立即
投稿
返回
顶部