这种无线路由器怎么设置置

2017年6月 PHP大版内专家分月排行榜第三2017年5月 PHP大版内专家分月排行榜第三2016年10月 PHP大版内专家分月排行榜第三2016年9月 PHP大版内专家分月排行榜第三2015年10月 PHP大版内专家分月排行榜第三2014年12月 PHP大版内专家分月排行榜第三2014年9月 PHP大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。当前位置:
微信小程序wx.getLocation()获取的经纬度不准确?
导读:Q:wx.getLocation()获取的经纬度不准确? map show-location显示的小绿点非常准确,但是通过getLocation获取到的经纬度就跟小绿点偏差了一段距离,请问有办法可以解决吗? A: map标签带了一个show-location的属性,加上之后可以在地图上显示一个小绿点表示
Q:wx.getLocation()获取的经纬度不准确?
&map show-location&显示的小绿点非常准确,但是通过getLocation获取到的经纬度就跟小绿点偏差了一段距离,请问有办法可以解决吗?
A:&map&标签带了一个show-location的属性,加上之后可以在地图上显示一个小绿点表示你的位置;
用wx.getLocation()可以获取到当前的经纬度,我写了一个marker,把获取到的经纬度传了进去之后发现marker跑偏了一大截,都跑到街的另一边了,想了挺长时间也没想到怎么处理,也查不到什么资料。
突然,眼睛瞥到了getLocation()方法的一句注释,// 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标,抱着试一试的心态把默认的wgs84换成了gcj02,重新编译,成功!
(编辑:admin)
尚未注册畅言帐号,请到后台注册
摩拜单车小程序已于微信小程序上线第一天正式发布,刷...
讯和苹果公司都对用户体验非常重视,都很擅长产品设计...
iOS 开发的微信小程序初体验 当前端在谈论微信小程序...
昨日,共享单车平台摩拜单车与腾讯微信联合宣布,今后...课程作业四
课程作业四
发布时间: 20:10:12
编辑:www.fx114.net
本篇文章主要介绍了"课程作业四 ",主要涉及到课程作业四 方面的内容,对于课程作业四 感兴趣的同学可以参考一下。
1.设计思路:生成随机数,分别赋值给数组。再将其求和输出
程序流程图:
源程序代码:
import javax.swing.JOptionP
public class Arrays {
public static void main(String args[]) {
int intVal[] = new int[ 10 ];//定义新数组
String output="数组元素分别为:";
int sum=0;
for(int i=0;i&10;i++){
intVal[i] = (int)(Math.random()*100+1);//生成随机数
output+=intVal[i] + " ";
//将其加到output后边
output+="\n";
for(int j=0;j&10;j++){//求和
sum+=intVal[j];
output+="所有数字的和为:" +
JOptionPane.showMessageDialog(null,output,"Results",
JOptionPane.PLAIN_MESSAGE);//输出
System.exit(0);//结束程序
结果截图:
变成总结:多吸取以前编程的经验
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.
import java.util.R
import java.io.*;
/**
* Immutable arbitrary-precision integers.
All operations behave as if
* BigIntegers were represented in two's-complement notation (like Java's
* primitive integer types).
BigInteger provides analogues to all of Java's
* primitive integer operators, and all relevant methods from java.lang.Math.
* Additionally, BigInteger provides operations for modular arithmetic, GCD
* calculation, primality testing, prime generation, bit manipulation,
* and a few other miscellaneous operations.
* Semantics of arithmetic operations exactly mimic those of Java's integer
* arithmetic operators, as defined in &i&The Java Language Specification&/i&.
* For example, division by zero throws an {@code ArithmeticException}, and
* division of a negative by a positive yields a negative (or zero) remainder.
* All of the details in the Spec concerning overflow are ignored, as
* BigIntegers are made as large as necessary to accommodate the results of an
* operation.
* Semantics of shift operations extend those of Java's shift operators
* to allow for negative shift distances.
A right-shift with a negative
* shift distance results in a left shift, and vice-versa.
The unsigned
* right shift operator ({@code &&&}) is omitted, as this operation makes
* little sense in combination with the "infinite word size" abstraction
* provided by this class.
* Semantics of bitwise logical operations exactly mimic those of Java's
* bitwise integer operators.
The binary operators ({@code and},
* {@code or}, {@code xor}) implicitly perform sign extension on the shorter
* of the two operands prior to performing the operation.
* Comparison operations perform signed integer comparisons, analogous to
* those performed by Java's relational and equality operators.
* Modular arithmetic operations are provided to compute residues, perform
* exponentiation, and compute multiplicative inverses.
These methods always
* return a non-negative result, between {@code 0} and {@code (modulus - 1)},
* inclusive.
* Bit operations operate on a single bit of the two's-complement
* representation of their operand.
If necessary, the operand is sign-
* extended so that it contains the designated bit.
None of the single-bit
* operations can produce a BigInteger with a different sign from the
* BigInteger being operated on, as they affect only a single bit, and the
* "infinite word size" abstraction provided by this class ensures that there
* are infinitely many "virtual sign bits" preceding each BigInteger.
* For the sake of brevity and clarity, pseudo-code is used throughout the
* descriptions of BigInteger methods.
The pseudo-code expression
* {@code (i + j)} is shorthand for "a BigInteger whose value is
* that of the BigInteger {@code i} plus that of the BigInteger {@code j}."
* The pseudo-code expression {@code (i == j)} is shorthand for
* "{@code true} if and only if the BigInteger {@code i} represents the same
* value as the BigInteger {@code j}."
Other pseudo-code expressions are
* interpreted similarly.
* All methods and constructors in this class throw
* {@code NullPointerException} when passed
* a null object reference for any input parameter.
BigDecimal
Josh Bloch
Michael McCloskey
* @since JDK1.1
*/
public class BigInteger extends Number implements Comparable&BigInteger& {
* The signum of this BigInteger: -1 for negative, 0 for zero, or
* 1 for positive.
Note that the BigInteger zero &i&must&/i& have
* a signum of 0.
This is necessary to ensures that there is exactly one
* representation for each BigInteger value.
* The magnitude of this BigInteger, in &i&big-endian&/i& order: the
* zeroth element of this array is the most-significant int of the
* magnitude.
The magnitude must be "minimal" in that the most-significant
* int ({@code mag[0]}) must be non-zero.
This is necessary to
* ensure that there is exactly one representation for each BigInteger
Note that this implies that the BigInteger zero has a
* zero-length mag array.
final int[]
// These "redundant fields" are initialized with recognizable nonsense
// values, and cached the first time they are needed (or never, if they
// aren't needed).
* One plus the bitCount of this BigInteger. Zeros means unitialized.
* @see #bitCount
* @deprecated Deprecated since logical value is offset from stored
* value and correction factor is applied in accessor method.
@Deprecated
private int bitC
* One plus the bitLength of this BigInteger. Zeros means unitialized.
* (either value is acceptable).
* @see #bitLength()
* @deprecated Deprecated since logical value is offset from stored
* value and correction factor is applied in accessor method.
@Deprecated
private int bitL
* Two plus the lowest set bit of this BigInteger, as returned by
* getLowestSetBit().
* @see #getLowestSetBit
* @deprecated Deprecated since logical value is offset from stored
* value and correction factor is applied in accessor method.
@Deprecated
private int lowestSetB
* Two plus the index of the lowest-order int in the magnitude of this
* BigInteger that contains a nonzero int, or -2 (either value is acceptable).
* The least significant int has int-number 0, the next int in order of
* increasing significance has int-number 1, and so forth.
* @deprecated Deprecated since logical value is offset from stored
* value and correction factor is applied in accessor method.
@Deprecated
private int firstNonzeroIntN
* This mask is used to obtain the value of an int as if it were unsigned.
final static long LONG_MASK = 0xffffffffL;
//Constructors
* Translates a byte array containing the two's-complement binary
* representation of a BigInteger into a BigInteger.
The input array is
* assumed to be in &i&big-endian&/i& byte-order: the most significant
* byte is in the zeroth element.
val big-endian two's-complement binary representation of
BigInteger.
* @throws NumberFormatException {@code val} is zero bytes long.
public BigInteger(byte[] val) {
if (val.length == 0)
throw new NumberFormatException("Zero length BigInteger");
if (val[0] & 0) {
mag = makePositive(val);
signum = -1;
mag = stripLeadingZeroBytes(val);
signum = (mag.length == 0 ? 0 : 1);
* This private constructor translates an int array containing the
* two's-complement binary representation of a BigInteger into a
* BigInteger. The input array is assumed to be in &i&big-endian&/i&
* int-order: the most significant int is in the zeroth element.
private BigInteger(int[] val) {
if (val.length == 0)
throw new NumberFormatException("Zero length BigInteger");
if (val[0] & 0) {
mag = makePositive(val);
signum = -1;
mag = trustedStripLeadingZeroInts(val);
signum = (mag.length == 0 ? 0 : 1);
* Translates the sign-magnitude representation of a BigInteger into a
* BigInteger.
The sign is represented as an integer signum value: -1 for
* negative, 0 for zero, or 1 for positive.
The magnitude is a byte array
* in &i&big-endian&/i& byte-order: the most significant byte is in the
* zeroth element.
A zero-length magnitude array is permissible, and will
* result in a BigInteger value of 0, whether signum is -1, 0 or 1.
signum signum of the number (-1 for negative, 0 for zero, 1
for positive).
magnitude big-endian binary representation of the magnitude of
the number.
* @throws NumberFormatException {@code signum} is not one of the three
legal values (-1, 0, and 1), or {@code signum} is 0 and
{@code magnitude} contains one or more non-zero bytes.
public BigInteger(int signum, byte[] magnitude) {
this.mag = stripLeadingZeroBytes(magnitude);
if (signum & -1 || signum & 1)
throw(new NumberFormatException("Invalid signum value"));
if (this.mag.length==0) {
this.signum = 0;
if (signum == 0)
throw(new NumberFormatException("signum-magnitude mismatch"));
this.signum =
* A constructor for internal use that translates the sign-magnitude
* representation of a BigInteger into a BigInteger. It checks the
* arguments and copies the magnitude so this constructor would be
* safe for external use.
private BigInteger(int signum, int[] magnitude) {
this.mag = stripLeadingZeroInts(magnitude);
if (signum & -1 || signum & 1)
throw(new NumberFormatException("Invalid signum value"));
if (this.mag.length==0) {
this.signum = 0;
if (signum == 0)
throw(new NumberFormatException("signum-magnitude mismatch"));
this.signum =
* Translates the String representation of a BigInteger in the specified
* radix into a BigInteger.
The String representation consists of an
* optional minus sign followed by a sequence of one or more digits in the
* specified radix.
The character-to-digit mapping is provided by
* {@code Character.digit}.
The String may not contain any extraneous
* characters (whitespace, for example).
* @param val String representation of BigInteger.
* @param radix radix to be used in interpreting {@code val}.
* @throws NumberFormatException {@code val} is not a valid representation
of a BigInteger in the specified radix, or {@code radix} is
outside the range from {@link Character#MIN_RADIX} to
{@link Character#MAX_RADIX}, inclusive.
Character#digit
public BigInteger(String val, int radix) {
int cursor = 0, numD
int len = val.length();
if (radix & Character.MIN_RADIX || radix & Character.MAX_RADIX)
throw new NumberFormatException("Radix out of range");
if (val.length() == 0)
throw new NumberFormatException("Zero length BigInteger");
// Check for minus sign
int sign = 1;
int index = val.lastIndexOf("-");
if (index != -1) {
if (index == 0) {
if (val.length() == 1)
throw new NumberFormatException("Zero length BigInteger");
sign = -1;
cursor = 1;
throw new NumberFormatException("Illegal embedded minus sign");
// Skip leading zeros and compute number of digits in magnitude
while (cursor & len &&
Character.digit(val.charAt(cursor), radix) == 0)
if (cursor == len) {
mag = ZERO.
signum = 0;
numDigits = len -
// Pre-allocate array of expected size. May be too large but can
// never be too small. Typically exact.
int numBits = (int)(((numDigits * bitsPerDigit[radix]) &&& 10) + 1);
int numWords = (numBits + 31) &&& 5;
int[] magnitude = new int[numWords];
// Process first (potentially short) digit group
int firstGroupLen = numDigits % digitsPerInt[radix];
if (firstGroupLen == 0)
firstGroupLen = digitsPerInt[radix];
String group = val.substring(cursor, cursor += firstGroupLen);
magnitude[magnitude.length - 1] = Integer.parseInt(group, radix);
if (magnitude[magnitude.length - 1] & 0)
throw new NumberFormatException("Illegal digit");
// Process remaining digit groups
int superRadix = intRadix[radix];
int groupVal = 0;
while (cursor & val.length()) {
group = val.substring(cursor, cursor += digitsPerInt[radix]);
groupVal = Integer.parseInt(group, radix);
if (groupVal & 0)
throw new NumberFormatException("Illegal digit");
destructiveMulAdd(magnitude, superRadix, groupVal);
// Required for cases where the array was overallocated.
mag = trustedStripLeadingZeroInts(magnitude);
// Constructs a new BigInteger using a char array with radix=10
BigInteger(char[] val) {
int cursor = 0, numD
int len = val.
// Check for leading minus sign
int sign = 1;
if (val[0] == '-') {
if (len == 1)
throw new NumberFormatException("Zero length BigInteger");
sign = -1;
cursor = 1;
// Skip leading zeros and compute number of digits in magnitude
while (cursor & len && Character.digit(val[cursor], 10) == 0)
if (cursor == len) {
signum = 0;
mag = ZERO.
numDigits = len -
// Pre-allocate array of expected size
if (len & 10) {
numWords = 1;
int numBits = (int)(((numDigits * bitsPerDigit[10]) &&& 10) + 1);
numWords = (numBits + 31) &&& 5;
int magnitude[] = new int[numWords];
// Process first (potentially short) digit group
int firstGroupLen = numDigits % digitsPerInt[10];
if (firstGroupLen == 0)
firstGroupLen = digitsPerInt[10];
magnitude[numWords - 1] = parseInt(val, cursor,
cursor += firstGroupLen);
// Process remaining digit groups
while (cursor & len) {
int groupVal = parseInt(val, cursor, cursor += digitsPerInt[10]);
destructiveMulAdd(magnitude, intRadix[10], groupVal);
mag = trustedStripLeadingZeroInts(magnitude);
// Create an integer with the digits between the two indexes
// Assumes start & end. The result may be negative, but it
// is to be treated as an unsigned value.
private int parseInt(char[] source, int start, int end) {
int result = Character.digit(source[start++], 10);
if (result == -1)
throw new NumberFormatException(new String(source));
for (int index = index& index++) {
int nextVal = Character.digit(source[index], 10);
if (nextVal == -1)
throw new NumberFormatException(new String(source));
result = 10*result + nextV
// bitsPerDigit in the given radix times 1024
// Rounded up to avoid underallocation.
private static long bitsPerDigit[] = { 0, 0,
, , , , , 3672,
, , , , , 4633,
, , , , , 5210,
// Multiply x array times word y in place, and add word z
private static void destructiveMulAdd(int[] x, int y, int z) {
// Perform the multiplication word by word
long ylong = y & LONG_MASK;
long zlong = z & LONG_MASK;
int len = x.
long product = 0;
long carry = 0;
for (int i = len-1; i &= 0; i--) {
product = ylong * (x[i] & LONG_MASK) +
x[i] = (int)
carry = product &&& 32;
// Perform the addition
long sum = (x[len-1] & LONG_MASK) +
x[len-1] = (int)
carry = sum &&& 32;
for (int i = len-2; i &= 0; i--) {
sum = (x[i] & LONG_MASK) +
x[i] = (int)
carry = sum &&& 32;
* Translates the decimal String representation of a BigInteger into a
* BigInteger.
The String representation consists of an optional minus
* sign followed by a sequence of one or more decimal digits.
* character-to-digit mapping is provided by {@code Character.digit}.
* The String may not contain any extraneous characters (whitespace, for
* example).
* @param val decimal String representation of BigInteger.
* @throws NumberFormatException {@code val} is not a valid representation
of a BigInteger.
Character#digit
public BigInteger(String val) {
this(val, 10);
* Constructs a randomly generated BigInteger, uniformly distributed over
* the range {@code 0} to (2&sup&{@code numBits}&/sup& - 1), inclusive.
* The uniformity of the distribution assumes that a fair source of random
* bits is provided in {@code rnd}.
Note that this constructor always
* constructs a non-negative BigInteger.
numBits maximum bitLength of the new BigInteger.
rnd source of randomness to be used in computing the new
BigInteger.
* @throws IllegalArgumentException {@code numBits} is negative.
* @see #bitLength()
public BigInteger(int numBits, Random rnd) {
this(1, randomBits(numBits, rnd));
private static byte[] randomBits(int numBits, Random rnd) {
if (numBits & 0)
throw new IllegalArgumentException("numBits must be non-negative");
int numBytes = (int)(((long)numBits+7)/8); // avoid overflow
byte[] randomBits = new byte[numBytes];
// Generate random bytes and mask out any excess bits
if (numBytes & 0) {
rnd.nextBytes(randomBits);
int excessBits = 8*numBytes - numB
randomBits[0] &= (1 && (8-excessBits)) - 1;
return randomB
* Constructs a randomly generated positive BigInteger that is probably
* prime, with the specified bitLength.&p&
* It is recommended that the {@link #probablePrime probablePrime}
* method be used in preference to this constructor unless there
* is a compelling need to specify a certainty.
bitLength bitLength of the returned BigInteger.
certainty a measure of the uncertainty that the caller is
willing to tolerate.
The probability that the new BigInteger
represents a prime number will exceed
(1 - 1/2&sup&{@code certainty}&/sup&).
The execution time of
this constructor is proportional to the value of this parameter.
rnd source of random bits used to select candidates to be
tested for primality.
* @throws ArithmeticException {@code bitLength & 2}.
#bitLength()
public BigInteger(int bitLength, int certainty, Random rnd) {
if (bitLength & 2)
throw new ArithmeticException("bitLength & 2");
// The cutoff of 95 was chosen empirically for best performance
prime = (bitLength & 95 ? smallPrime(bitLength, certainty, rnd)
: largePrime(bitLength, certainty, rnd));
signum = 1;
mag = prime.
// Minimum size in bits that the requested prime number has
// before we use the large prime number generating algorithms
private static final int SMALL_PRIME_THRESHOLD = 95;
// Certainty required to meet the spec of probablePrime
private static final int DEFAULT_PRIME_CERTAINTY = 100;
* Returns a positive BigInteger that is probably prime, with the
* specified bitLength. The probability that a BigInteger returned
* by this method is composite does not exceed 2&sup&-100&/sup&.
bitLength bitLength of the returned BigInteger.
rnd source of random bits used to select candidates to be
tested for primality.
* @return a BigInteger of {@code bitLength} bits that is probably prime
* @throws ArithmeticException {@code bitLength & 2}.
#bitLength()
* @since 1.4
public static BigInteger probablePrime(int bitLength, Random rnd) {
if (bitLength & 2)
throw new ArithmeticException("bitLength & 2");
// The cutoff of 95 was chosen empirically for best performance
return (bitLength & SMALL_PRIME_THRESHOLD ?
smallPrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd) :
largePrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd));
* Find a random number of the specified bitLength that is probably prime.
* This method is used for smaller primes, its performance degrades on
* larger bitlengths.
* This method assumes bitLength & 1.
private static BigInteger smallPrime(int bitLength, int certainty, Random rnd) {
int magLen = (bitLength + 31) &&& 5;
int temp[] = new int[magLen];
int highBit = 1 && ((bitLength+31) & 0x1f);
// High bit of high int
int highMask = (highBit && 1) - 1;
// Bits to keep in high int
while(true) {
// Construct a candidate
for (int i=0; i&magL i++)
temp[i] = rnd.nextInt();
temp[0] = (temp[0] & highMask) | highB
// Ensure exact length
if (bitLength & 2)
temp[magLen-1] |= 1;
// Make odd if bitlen & 2
BigInteger p = new BigInteger(temp, 1);
// Do cheap "pre-test" if applicable
if (bitLength & 6) {
long r = p.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))
continue; // Ca try another
// All candidates of bitLength 2 and 3 are prime by this point
if (bitLength & 4)
// Do expensive test if we survive pre-test (or it's inapplicable)
if (p.primeToCertainty(certainty, rnd))
private static final BigInteger SMALL_PRIME_PRODUCT
= valueOf(3L*5*7*11*13*17*19*23*29*31*37*41);
* Find a random number of the specified bitLength that is probably prime.
* This method is more appropriate for larger bitlengths since it uses
* a sieve to eliminate most composites before using a more expensive
private static BigInteger largePrime(int bitLength, int certainty, Random rnd) {
p = new BigInteger(bitLength, rnd).setBit(bitLength-1);
p.mag[p.mag.length-1] &= 0xfffffffe;
// Use a sieve length likely to contain the next prime number
int searchLen = (bitLength / 20) * 64;
BitSieve searchSieve = new BitSieve(p, searchLen);
BigInteger candidate = searchSieve.retrieve(p, certainty, rnd);
while ((candidate == null) || (candidate.bitLength() != bitLength)) {
p = p.add(BigInteger.valueOf(2*searchLen));
if (p.bitLength() != bitLength)
p = new BigInteger(bitLength, rnd).setBit(bitLength-1);
p.mag[p.mag.length-1] &= 0xfffffffe;
searchSieve = new BitSieve(p, searchLen);
candidate = searchSieve.retrieve(p, certainty, rnd);
* Returns the first integer greater than this {@code BigInteger} that
* is probably prime.
The probability that the number returned by this
* method is composite does not exceed 2&sup&-100&/sup&. This method will
* never skip over a prime when searching: if it returns {@code p}, there
* is no prime {@code q} such that {@code this & q & p}.
* @return the first integer greater than this {@code BigInteger} that
is probably prime.
* @throws ArithmeticException {@code this & 0}.
* @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; // Ca try another
// All candidates of bitLength 2 and 3 are prime by this point
if (result.bitLength() & 4)
// The expensive test
if (result.primeToCertainty(DEFAULT_PRIME_CERTAINTY, null))
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 = (result.bitLength() / 20) * 64;
while(true) {
BitSieve searchSieve = new BitSieve(result, searchLen);
BigInteger candidate = searchSieve.retrieve(result,
DEFAULT_PRIME_CERTAINTY, null);
if (candidate != null)
result = result.add(BigInteger.valueOf(2 * searchLen));
* Returns {@code true} if this BigInteger is probably prime,
* {@code false} if it's definitely composite.
* This method assumes bitLength & 2.
certainty a measure of the uncertainty that the caller is
willing to tolerate: if the call returns {@code true}
the probability that this BigInteger is prime exceeds
{@code (1 - 1/2&sup&certainty&/sup&)}.
The execution time of
this method is proportional to the value of this parameter.
* @return {@code true} if this BigInteger is probably prime,
{@code false} if it's definitely composite.
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 :
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;
rounds = 2;
rounds = n & rounds ? n :
return passesMillerRabin(rounds, random) && passesLucasLehmer();
* Returns true iff this BigInteger is a Lucas-Lehmer probable prime.
* The following assumptions are made:
* This BigInteger is a positive, odd number.
private boolean passesLucasLehmer() {
BigInteger thisPlusOne = this.add(ONE);
int d = 5;
while (jacobiSymbol(d, this) != -1) {
// 5, -7, 9, -11, ...
d = (d&0) ? Math.abs(d)+2 : -(d+2);
BigInteger u = lucasLehmerSequence(d, thisPlusOne, this);
return u.mod(this).equals(ZERO);
* Computes Jacobi(p,n).
* Assumes n positive, odd, n&=3.
private static int jacobiSymbol(int p, BigInteger n) {
if (p == 0)
// Algorithm and comments adapted from Colin Plumb's C library.
int j = 1;
int u = n.mag[n.mag.length-1];
// Make p positive
if (p & 0) {
int n8 = u & 7;
if ((n8 == 3) || (n8 == 7))
j = -j; // 3 (011) or 7 (111) mod 8
// Get rid of factors of 2 in p
while ((p & 3) == 0)
if ((p & 1) == 0) {
if (((u ^ (u&&1)) & 2) != 0)
// 3 (011) or 5 (101) mod 8
if (p == 1)
// Then, apply quadratic reciprocity
if ((p & u & 2) != 0)
// p = u = 3 (mod 4)?
// And reduce u mod p
u = n.mod(BigInteger.valueOf(p)).intValue();
// Now compute Jacobi(u,p), u & p
while (u != 0) {
while ((u & 3) == 0)
if ((u & 1) == 0) {
if (((p ^ (p&&1)) & 2) != 0)
// 3 (011) or 5 (101) mod 8
if (u == 1)
// Now both u and p are odd, so use quadratic reciprocity
assert (u & p);
int t = u = p =
if ((u & p & 2) != 0) // u = p = 3 (mod 4)?
// Now u &= p, so it can be reduced
private static BigInteger lucasLehmerSequence(int z, BigInteger k, BigInteger n) {
BigInteger d = BigInteger.valueOf(z);
BigInteger u = ONE; BigInteger u2;
BigInteger v = ONE; BigInteger v2;
for (int i=k.bitLength()-2; i&=0; i--) {
u2 = u.multiply(v).mod(n);
v2 = v.square().add(d.multiply(u.square())).mod(n);
if (v2.testBit(0))
v2 = v2.subtract(n);
v2 = v2.shiftRight(1);
u = u2; v = v2;
if (k.testBit(i)) {
u2 = u.add(v).mod(n);
if (u2.testBit(0))
u2 = u2.subtract(n);
u2 = u2.shiftRight(1);
v2 = v.add(d.multiply(u)).mod(n);
if (v2.testBit(0))
v2 = v2.subtract(n);
v2 = v2.shiftRight(1);
u = u2; v = v2;
private static volatile Random staticR
private static Random getSecureRandom() {
if (staticRandom == null) {
staticRandom = new java.security.SecureRandom();
return staticR
* Returns true iff this BigInteger passes the specified number of
* Miller-Rabin tests. This test is taken from the DSA spec (NIST FIPS
* The following assumptions are made:
* This BigInteger is a positive, odd number greater than 2.
* iterations&=50.
private boolean passesMillerRabin(int iterations, Random rnd) {
// Find a and m such that m is odd and this == 1 + 2**a * m
BigInteger thisMinusOne = this.subtract(ONE);
BigInteger m = thisMinusO
int a = m.getLowestSetBit();
m = m.shiftRight(a);
// Do the tests
if (rnd == null) {
rnd = getSecureRandom();
for (int i=0; i& i++) {
// Generate a uniform random on (1, this)
b = new BigInteger(this.bitLength(), rnd);
} while (b.compareTo(ONE) &= 0 || b.compareTo(this) &= 0);
int j = 0;
BigInteger z = b.modPow(m, this);
while(!((j==0 && z.equals(ONE)) || z.equals(thisMinusOne))) {
if (j&0 && z.equals(ONE) || ++j==a)
return false;
z = z.modPow(TWO, this);
return true;
* This internal constructor differs from its public cousin
* with the arguments reversed in two ways: it assumes that its
* arguments are correct, and it doesn't copy the magnitude array.
BigInteger(int[] magnitude, int signum) {
this.signum = (magnitude.length==0 ? 0 : signum);
this.mag =
* This private constructor is for internal use and assumes that its
* arguments are correct.
private BigInteger(byte[] magnitude, int signum) {
this.signum = (magnitude.length==0 ? 0 : signum);
this.mag = stripLeadingZeroBytes(magnitude);
//Static Factory Methods
* Returns a BigInteger whose value is equal to that of the
* specified {@code long}.
This "static factory method" is
* provided in preference to a ({@code long}) constructor
* because it allows for reuse of frequently used BigIntegers.
val value of the BigInteger to return.
* @return a BigInteger with the specified value.
public static BigInteger valueOf(long val) {
// If -MAX_CONSTANT & val & MAX_CONSTANT, return stashed constant
if (val == 0)
return ZERO;
if (val & 0 && val &= MAX_CONSTANT)
return posConst[(int) val];
else if (val & 0 && val &= -MAX_CONSTANT)
return negConst[(int) -val];
return new BigInteger(val);
* Constructs a BigInteger with the specified value, which may not be zero.
private BigInteger(long val) {
if (val & 0) {
signum = -1;
signum = 1;
int highWord = (int)(val &&& 32);
if (highWord==0) {
mag = new int[1];
mag[0] = (int)
mag = new int[2];
mag[0] = highW
mag[1] = (int)
* Returns a BigInteger with the given two's complement representation.
* Assumes that the input array will not be modified (the returned
* BigInteger will reference the input array if feasible).
private static BigInteger valueOf(int val[]) {
return (val[0]&0 ? new BigInteger(val, 1) : new BigInteger(val));
// Constants
* Initialize static constant array when class is loaded.
private final static int MAX_CONSTANT = 16;
private static BigInteger posConst[] = new BigInteger[MAX_CONSTANT+1];
private static BigInteger negConst[] = new BigInteger[MAX_CONSTANT+1];
for (int i = 1; i &= MAX_CONSTANT; i++) {
int[] magnitude = new int[1];
magnitude[0] =
posConst[i] = new BigInteger(magnitude,
negConst[i] = new BigInteger(magnitude, -1);
* The BigInteger constant zero.
public static final BigInteger ZERO = new BigInteger(new int[0], 0);
* The BigInteger constant one.
public static final BigInteger ONE = valueOf(1);
* The BigInteger constant two.
(Not exported.)
private static final BigInteger TWO = valueOf(2);
* The BigInteger constant ten.
public static final BigInteger TEN = valueOf(10);
// Arithmetic Operations
* Returns a BigInteger whose value is {@code (this + val)}.
val value to be added to this BigInteger.
* @return {@code this + val}
public BigInteger add(BigInteger val) {
if (val.signum == 0)
return this;
if (signum == 0)
if (val.signum == signum)
return new BigInteger(add(mag, val.mag), signum);
int cmp = compareMagnitude(val);
if (cmp==0)
return ZERO;
int[] resultMag = (cmp&0 ? subtract(mag, val.mag) :
subtract(val.mag, mag));
resultMag = trustedStripLeadingZeroInts(resultMag);
return new BigInteger(resultMag, cmp == signum ? 1 : -1);
* Adds the contents of the int arrays x and y. This method allocates
* a new int array to hold the answer and returns a reference to that
private static int[] add(int[] x, int[] y) {
// If x is shorter, swap the two arrays
if (x.length & y.length) {
int[] tmp =
int xIndex = x.
int yIndex = y.
int result[] = new int[xIndex];
long sum = 0;
// Add common parts of both numbers
while(yIndex & 0) {
sum = (x[--xIndex] & LONG_MASK) +
(y[--yIndex] & LONG_MASK) + (sum &&& 32);
result[xIndex] = (int)
// Copy remainder of longer number while carry propagation is required
boolean carry = (sum &&& 32 != 0);
while (xIndex & 0 && carry)
carry = ((result[--xIndex] = x[xIndex] + 1) == 0);
// Copy remainder of longer number
while (xIndex & 0)
result[--xIndex] = x[xIndex];
// Grow result if necessary
if (carry) {
int bigger[] = new int[result.length + 1];
System.arraycopy(result, 0, bigger, 1, result.length);
bigger[0] = 0x01;
* Returns a BigInteger whose value is {@code (this - val)}.
val value to be subtracted from this BigInteger.
* @return {@code this - val}
public BigInteger subtract(BigInteger val) {
if (val.signum == 0)
return this;
if (signum == 0)
return val.negate();
if (val.signum != signum)
return new BigInteger(add(mag, val.mag), signum);
int cmp = compareMagnitude(val);
if (cmp==0)
return ZERO;
int[] resultMag = (cmp&0 ? subtract(mag, val.mag)
: subtract(val.mag, mag));
resultMag = trustedStripLeadingZeroInts(resultMag);
return new BigInteger(resultMag, (cmp == signum) ? 1 : -1);
* Subtracts the contents of the second int arrays (little) from the
* first (big).
The first int array (big) must represent a larger number
* than the second.
This method allocates the space necessary to hold the
private static int[] subtract(int[] big, int[] little) {
int bigIndex = big.
int result[] = new int[bigIndex];
int littleIndex = little.
long difference = 0;
// Subtract common parts of both numbers
while(littleIndex & 0) {
difference = (big[--bigIndex] & LONG_MASK) -
(little[--littleIndex] & LONG_MASK) +
(difference && 32);
result[bigIndex] = (int)
// Subtract remainder of longer number while borrow propagates
boolean borrow = (difference && 32 != 0);
while (bigIndex & 0 && borrow)
borrow = ((result[--bigIndex] = big[bigIndex] - 1) == -1);
// Copy remainder of longer number
while (bigIndex & 0)
result[--bigIndex] = big[bigIndex];
* Returns a BigInteger whose value is {@code (this * val)}.
val value to be multiplied by this BigInteger.
* @return {@code this * val}
public BigInteger multiply(BigInteger val) {
if (val.signum == 0 || signum == 0)
return ZERO;
int[] res = multiplyToLen(mag, mag.length, val.mag, val.mag.length, null);
res = trustedStripLeadingZeroInts(res);
return new BigInteger(res, signum == val.signum ? 1 : -1);
* Package private methods used by BigDecimal code to multiply a BigInteger
* with a long. Assumes v is not equal to INFLATED.
BigInteger multiply(long v) {
if (v == 0 || signum == 0)
return ZERO;
assert v != BigDecimal.INFLATED;
int rsign = (v & 0 ? signum : -signum);
if (v & 0)
long dh = v &&& 32;
// higher order bits
long dl = v & LONG_MASK; // lower order bits
int xlen = mag.
int[] value =
int[] rmag = (dh == 0L) ? (new int[xlen + 1]) : (new int[xlen + 2]);
long carry = 0;
int rstart = rmag.length - 1;
for (int i = xlen - 1; i &= 0; i--) {
long product = (value[i] & LONG_MASK) * dl +
rmag[rstart--] = (int)
carry = product &&& 32;
rmag[rstart] = (int)
if (dh != 0L) {
carry = 0;
rstart = rmag.length - 2;
for (int i = xlen - 1; i &= 0; i--) {
long product = (value[i] & LONG_MASK) * dh +
(rmag[rstart] & LONG_MASK) +
rmag[rstart--] = (int)
carry = product &&& 32;
rmag[0] = (int)
if (carry == 0L)
rmag = java.util.Arrays.copyOfRange(rmag, 1, rmag.length);
return new BigInteger(rmag, rsign);
* Multiplies int arrays x and y to the specified lengths and places
* the result into z. There will be no leading zeros in the resultant array.
private int[] multiplyToLen(int[] x, int xlen, int[] y, int ylen, int[] z) {
int xstart = xlen - 1;
int ystart = ylen - 1;
if (z == null || z.length & (xlen+ ylen))
z = new int[xlen+ylen];
long carry = 0;
for (int j=ystart, k=ystart+1+ j&=0; j--, k--) {
long product = (y[j] & LONG_MASK) *
(x[xstart] & LONG_MASK) +
z[k] = (int)
carry = product &&& 32;
z[xstart] = (int)
for (int i = xstart-1; i &= 0; i--) {
carry = 0;
for (int j=ystart, k=ystart+1+i; j&=0; j--, k--) {
long product = (y[j] & LONG_MASK) *
(x[i] & LONG_MASK) +
(z[k] & LONG_MASK) +
z[k] = (int)
carry = product &&& 32;
z[i] = (int)
* Returns a BigInteger whose value is {@code (this&sup&2&/sup&)}.
* @return {@code this&sup&2&/sup&}
private BigInteger square() {
if (signum == 0)
return ZERO;
int[] z = squareToLen(mag, mag.length, null);
return new BigInteger(trustedStripLeadingZeroInts(z), 1);
* Squares the contents of the int array x. The result is placed into the
* int array z.
The contents of x are not changed.
private static final int[] squareToLen(int[] x, int len, int[] z) {
* The algorithm used here is adapted from Colin Plumb's C library.
* Technique: Consider the partial products in the multiplication
* of "abcde" by itself:
==================
ae be ce de ee
ad bd cd dd de
ac bc cc cd ce
ab bb bc bd be
aa ab ac ad ae
* Note that everything above the main diagonal:
ae be ce de = (abcd) * e
= (abc) * d
= (ab) * c
* is a copy of everything below the main diagonal:
ab ac ad ae
* Thus, the sum is 2 * (off the diagonal) + diagonal.
* This is accumulated beginning with the diagonal (which
* consist of the squares of the digits of the input), which is then
* divided by two, the off-diagonal added, and multiplied by two
The low bit is simply a copy of the low bit of the
* input, so it doesn't need special care.
int zlen = len && 1;
if (z == null || z.length & zlen)
z = new int[zlen];
// Store the squares, right shifted one bit (i.e., divided by 2)
int lastProductLowWord = 0;
for (int j=0, i=0; j& j++) {
long piece = (x[j] & LONG_MASK);
long product = piece *
z[i++] = (lastProductLowWord && 31) | (int)(product &&& 33);
z[i++] = (int)(product &&& 1);
lastProductLowWord = (int)
// Add in off-diagonal sums
for (int i=len, offset=1; i&0; i--, offset+=2) {
int t = x[i-1];
t = mulAdd(z, x, offset, i-1, t);
addOne(z, offset-1, i, t);
// Shift back up and set low bit
primitiveLeftShift(z, zlen, 1);
z[zlen-1] |= x[len-1] & 1;
* Returns a BigInteger whose value is {@code (this / val)}.
val value by which this BigInteger is to be divided.
* @return {@code this / val}
* @throws ArithmeticException {@code val==0}
public BigInteger divide(BigInteger val) {
MutableBigInteger q = new MutableBigInteger(),
a = new MutableBigInteger(this.mag),
b = new MutableBigInteger(val.mag);
a.divide(b, q);
return q.toBigInteger(this.signum * val.signum);
* Returns an array of two BigIntegers containing {@code (this / val)}
* followed by {@code (this % val)}.
val value by which this BigInteger is to be divided, and the
remainder computed.
* @return an array of two BigIntegers: the quotient {@code (this / val)}
is the initial element, and the remainder {@code (this % val)}
is the final element.
* @throws ArithmeticException {@code val==0}
public BigInteger[] divideAndRemainder(BigInteger val) {
BigInteger[] result = new BigInteger[2];
MutableBigInteger q = new MutableBigInteger(),
a = new MutableBigInteger(this.mag),
b = new MutableBigInteger(val.mag);
MutableBigInteger r = a.divide(b, q);
result[0] = q.toBigInteger(this.signum * val.signum);
result[1] = r.toBigInteger(this.signum);
* Returns a BigInteger whose value is {@code (this % val)}.
val value by which this BigInteger is to be divided, and the
remainder computed.
* @return {@code this % val}
* @throws ArithmeticException {@code val==0}
public BigInteger remainder(BigInteger val) {
MutableBigInteger q = new MutableBigInteger(),
a = new MutableBigInteger(this.mag),
b = new MutableBigInteger(val.mag);
return a.divide(b, q).toBigInteger(this.signum);
* Returns a BigInteger whose value is &tt&(this&sup&exponent&/sup&)&/tt&.
* Note that {@code exponent} is an integer rather than a BigInteger.
exponent exponent to which this BigInteger is to be raised.
* @return &tt&this&sup&exponent&/sup&&/tt&
* @throws ArithmeticException {@code exponent} is negative.
(This would
cause the operation to yield a non-integer value.)
public BigInteger pow(int exponent) {
if (exponent & 0)
throw new ArithmeticException("Negative exponent");
if (signum==0)
return (exponent==0 ? ONE : this);
// Perform exponentiation using repeated squaring trick
int newSign = (signum&0 && (exponent&1)==1 ? -1 : 1);
int[] baseToPow2 = this.
int[] result = {1};
while (exponent != 0) {
if ((exponent & 1)==1) {
result = multiplyToLen(result, result.length,
baseToPow2, baseToPow2.length, null);
result = trustedStripLeadingZeroInts(result);
if ((exponent &&&= 1) != 0) {
baseToPow2 = squareToLen(baseToPow2, baseToPow2.length, null);
baseToPow2 = trustedStripLeadingZeroInts(baseToPow2);
return new BigInteger(result, newSign);
* Returns a BigInteger whose value is the greatest common divisor of
* {@code abs(this)} and {@code abs(val)}.
Returns 0 if
* {@code this==0 && val==0}.
val value with which the GCD is to be computed.
* @return {@code GCD(abs(this), abs(val))}
public BigInteger gcd(BigInteger val) {
if (val.signum == 0)
return this.abs();
else if (this.signum == 0)
return val.abs();
MutableBigInteger a = new MutableBigInteger(this);
MutableBigInteger b = new MutableBigInteger(val);
MutableBigInteger result = a.hybridGCD(b);
return result.toBigInteger(1);
* Package private method to return bit length for an integer.
static int bitLengthForInt(int n) {
return 32 - Integer.numberOfLeadingZeros(n);
* Left shift int array a up to len by n bits. Returns the array that
* results from the shift since space may have to be reallocated.
private static int[] leftShift(int[] a, int len, int n) {
int nInts = n &&& 5;
int nBits = n&0x1F;
int bitsInHighWord = bitLengthForInt(a[0]);
// If shift can be done without recopy, do so
if (n &= (32-bitsInHighWord)) {
primitiveLeftShift(a, len, nBits);
} else { // Array must be resized
if (nBits &= (32-bitsInHighWord)) {
int result[] = new int[nInts+len];
for (int i=0; i& i++)
result[i] = a[i];
primitiveLeftShift(result, result.length, nBits);
int result[] = new int[nInts+len+1];
for (int i=0; i& i++)
result[i] = a[i];
primitiveRightShift(result, result.length, 32 - nBits);
// shifts a up to len right n bits assumes no leading zeros, 0&n&32
static void primitiveRightShift(int[] a, int len, int n) {
int n2 = 32 -
for (int i=len-1, c=a[i]; i&0; i--) {
c = a[i-1];
a[i] = (c && n2) | (b &&& n);
// shifts a up to len left n bits assumes no leading zeros, 0&=n&32
static void primitiveLeftShift(int[] a, int len, int n) {
if (len == 0 || n == 0)
int n2 = 32 -
for (int i=0, c=a[i], m=i+len-1; i&m; i++) {
c = a[i+1];
a[i] = (b && n) | (c &&& n2);
a[len-1] &&=
* Calculate bitlength of contents of the first len elements an int array,
* assuming there are no leading zero ints.
private static int bitLength(int[] val, int len) {
if (len==0)
return ((len-1)&&5) + bitLengthForInt(val[0]);
* Returns a BigInteger whose value is the absolute value of this
* BigInteger.
* @return {@code abs(this)}
public BigInteger abs() {
return (signum &= 0 ? this : this.negate());
* Returns a BigInteger whose value is {@code (-this)}.
* @return {@code -this}
public BigInteger negate() {
return new BigInteger(this.mag, -this.signum);
* Returns the signum function of this BigInteger.
* @return -1, 0 or 1 as the value of this BigInteger is negative, zero or
public int signum() {
return this.
// Modular Arithmetic Operations
* Returns a BigInteger whose value is {@code (this mod m}).
This method
* differs from {@code remainder} in that it always returns a
* &i&non-negative&/i& BigInteger.
m the modulus.
* @return {@code this mod m}
* @throws ArithmeticException {@code m &= 0}
#remainder
public BigInteger mod(BigInteger m) {
if (m.signum &= 0)
throw new ArithmeticException("BigInteger: modulus not positive");
BigInteger result = this.remainder(m);
return (result.signum &= 0 ? result : result.add(m));
* Returns a BigInteger whose value is
* &tt&(this&sup&exponent&/sup& mod m)&/tt&.
(Unlike {@code pow}, this
* method permits negative exponents.)
exponent the exponent.
m the modulus.
* @return &tt&this&sup&exponent&/sup& mod m&/tt&
* @throws ArithmeticException {@code m &= 0}
#modInverse
public BigInteger modPow(BigInteger exponent, BigInteger m) {
if (m.signum &= 0)
throw new ArithmeticException("BigInteger: modulus not positive");
// Trivial cases
if (exponent.signum == 0)
return (m.equals(ONE) ? ZERO : ONE);
if (this.equals(ONE))
return (m.equals(ONE) ? ZERO : ONE);
if (this.equals(ZERO) && exponent.signum &= 0)
return ZERO;
if (this.equals(negConst[1]) && (!exponent.testBit(0)))
return (m.equals(ONE) ? ZERO : ONE);
boolean invertR
if ((invertResult = (exponent.signum & 0)))
exponent = exponent.negate();
BigInteger base = (this.signum & 0 || this.compareTo(m) &= 0
? this.mod(m) : this);
if (m.testBit(0)) { // odd modulus
result = base.oddModPow(exponent, m);
* Even modulus.
Tear it into an "odd part" (m1) and power of two
* (m2), exponentiate mod m1, manually exponentiate mod m2, and
* use Chinese Remainder Theorem to combine results.
// Tear m apart into odd part (m1) and power of 2 (m2)
int p = m.getLowestSetBit();
// Max pow of 2 that divides m
BigInteger m1 = m.shiftRight(p);
BigInteger m2 = ONE.shiftLeft(p); // 2**p
// Calculate new base from m1
BigInteger base2 = (this.signum & 0 || this.compareTo(m1) &= 0
? this.mod(m1) : this);
// Caculate (base ** exponent) mod m1.
BigInteger a1 = (m1.equals(ONE) ? ZERO :
base2.oddModPow(exponent, m1));
// Calculate (this ** exponent) mod m2
BigInteger a2 = base.modPow2(exponent, p);
// Combine results using Chinese Remainder Theorem
BigInteger y1 = m2.modInverse(m1);
BigInteger y2 = m1.modInverse(m2);
result = a1.multiply(m2).multiply(y1).add
(a2.multiply(m1).multiply(y2)).mod(m);
return (invertResult ? result.modInverse(m) : result);
static int[] bnExpModThreshTable = {7, 25, 81, 241, 673, 1793,
Integer.MAX_VALUE}; // Sentinel
* Returns a BigInteger whose value is x to the power of y mod z.
* Assumes: z is odd && x & z.
private BigInteger oddModPow(BigInteger y, BigInteger z) {
* The algorithm is adapted from Colin Plumb's C library.
* The window algorithm:
* The idea is to keep a running product of b1 = n^(high-order bits of exp)
* and then keep appending exponent bits to it.
The following patterns
* apply to a 3-bit window (k = 3):
* To append
* To append
1: square, multiply by n^1
* To append
10: square, multiply by n^1, square
* To append
11: square, square, multiply by n^3
* To append 100: square, multiply by n^1, square, square
* To append 101: square, square, square, multiply by n^5
* To append 110: square, square, multiply by n^3, square
* To append 111: square, square, square, multiply by n^7
* Since each pattern involves only one multiply, the longer the pattern
* the better, except that a 0 (no multiplies) can be appended directly.
* We precompute a table of odd powers of n, up to 2^k, and can then
* multiply k bits of exponent at a time.
Actually, assuming random
* exponents, there is on average one zero bit between needs to
* multiply (1/2 of the time there's none, 1/4 of the time there's 1,
* 1/8 of the time, there's 2, 1/32 of the time, there's 3, etc.), so
* you have to do one multiply per k+1 bits of exponent.
* The loop walks down the exponent, squaring the result buffer as
* it goes.
There is a wbits+1 bit lookahead buffer, buf, that is
* filled with the upcoming exponent bits.
(What is read after the
* end of the exponent is unimportant, but it is filled with zero here.)
* When the most-significant bit of this buffer becomes set, i.e.
* (buf & tblmask) != 0, we have to decide what pattern to multiply
* by, and when to do it.
We decide, remember to do it in future
* after a suitable number of squarings have passed (e.g. a pattern
* of "100" in the buffer requires that we multiply by n^1
* a pattern of "110" calls for multiplying by n^3 after one more
* squaring), clear the buffer, and continue.
* When we start, there is one more optimization: the result buffer
* is implcitly one, so squaring it or multiplying by it can be
* optimized away.
Further, if we start with a pattern like "100"
* in the lookahead window, rather than placing n into the buffer
* and then starting to square it, we have already computed n^2
* to compute the odd-powers table, so we can place that into
* the buffer and save a squaring.
* This means that if you have a k-bit window, to compute n^z,
* where z is the high k bits of the exponent, 1/2 of the time
* it requires no squarings.
1/4 of the time, it requires 1
* squaring, ... 1/2^(k-1) of the time, it reqires k-2 squarings.
* And the remaining 1/2^(k-1) of the time, the top k bits are a
* 1 followed by k-1 0 bits, so it again only requires k-2
* squarings, not k-1.
The average of these is 1.
* to the one squaring we have to do to compute the table,
* and you'll see that a k-bit window saves k-2 squarings
* as well as reducing the multiplies.
(It actually doesn't
* hurt in the case k = 1, either.)
// Special case for exponent of one
if (y.equals(ONE))
return this;
// Special case for base of zero
if (signum==0)
return ZERO;
int[] base = mag.clone();
int[] exp = y.
int[] mod = z.
int modLen = mod.
// Select an appropriate window size
int wbits = 0;
int ebits = bitLength(exp, exp.length);
// if exponent is 601), use minimum window size
if ((ebits != 17) || (exp[0] != 65537)) {
while (ebits & bnExpModThreshTable[wbits]) {
// Calculate appropriate table size
int tblmask = 1 &&
// Allocate table for precomputed odd powers of base in Montgomery form
int[][] table = new int[tblmask][];
for (int i=0; i& i++)
table[i] = new int[modLen];
// Compute the modular inverse
int inv = -MutableBigInteger.inverseMod32(mod[modLen-1]);
// Convert base to Montgomery form
int[] a = leftShift(base, base.length, modLen && 5);
MutableBigInteger q = new MutableBigInteger(),
a2 = new MutableBigInteger(a),
b2 = new MutableBigInteger(mod);
MutableBigInteger r = a2.divide(b2, q);
table[0] = r.toIntArray();
// Pad table[0] with leading zeros so its length is at least modLen
if (table[0].length & modLen) {
int offset = modLen - table[0].
int[] t2 = new int[modLen];
for (int i=0; i&table[0]. i++)
t2[i+offset] = table[0][i];
table[0] = t2;
// Set b to the square of the base
int[] b = squareToLen(table[0], modLen, null);
b = montReduce(b, mod, modLen, inv);
// Set t to high half of b
int[] t = new int[modLen];
for(int i=0; i&modL i++)
t[i] = b[i];
// Fill in the table with odd powers of the base
for (int i=1; i& i++) {
int[] prod = multiplyToLen(t, modLen, table[i-1], modLen, null);
table[i] = montReduce(prod, mod, modLen, inv);
// Pre load the window that slides over the exponent
int bitpos = 1 && ((ebits-1) & (32-1));
int buf = 0;
int elen = exp.
int eIndex = 0;
for (int i = 0; i &= i++) {
buf = (buf && 1) | (((exp[eIndex] & bitpos) != 0)?1:0);
bitpos &&&= 1;
if (bitpos == 0) {
bitpos = 1 && (32-1);
int multpos =
// The first iteration, which is hoisted out of the main loop
boolean isone = true;
multpos = ebits -
while ((buf & 1) == 0) {
buf &&&= 1;
multpos++;
int[] mult = table[buf &&& 1];
if (multpos == ebits)
isone = false;
// The main loop
while(true) {
// Advance the window
buf &&= 1;
if (elen != 0) {
buf |= ((exp[eIndex] & bitpos) != 0) ? 1 : 0;
bitpos &&&= 1;
if (bitpos == 0) {
bitpos = 1 && (32-1);
// Examine the window for pending multiplies
if ((buf & tblmask) != 0) {
multpos = ebits -
while ((buf & 1) == 0) {
buf &&&= 1;
multpos++;
mult = table[buf &&& 1];
// Perform multiply
if (ebits == multpos) {
if (isone) {
b = mult.clone();
isone = false;
a = multiplyToLen(t, modLen, mult, modLen, a);
a = montReduce(a, mod, modLen, inv);
t = a = b =
// Check if done
if (ebits == 0)
// Square the input
if (!isone) {
a = squareToLen(t, modLen, a);
a = montReduce(a, mod, modLen, inv);
t = a = b =
// Convert result out of Montgomery form and return
int[] t2 = new int[2*modLen];
for(int i=0; i&modL i++)
t2[i+modLen] = b[i];
b = montReduce(t2, mod, modLen, inv);
t2 = new int[modLen];
for(int i=0; i&modL i++)
t2[i] = b[i];
return new BigInteger(1, t2);
* Montgomery reduce n, modulo mod.
This reduces modulo mod and divides
* by 2^(32*mlen). Adapted from Colin Plumb's C library.
private static int[] montReduce(int[] n, int[] mod, int mlen, int inv) {
int offset=0;
int nEnd = n[n.length-1-offset];
int carry = mulAdd(n, mod, offset, mlen, inv * nEnd);
c += addOne(n, offset, mlen, carry);
} while(--len & 0);
while(c&0)
c += subN(n, mod, mlen);
while (intArrayCmpToLen(n, mod, mlen) &= 0)
subN(n, mod, mlen);
* Returns -1, 0 or +1 as big-endian unsigned int array arg1 is less than,
* equal to, or greater than arg2 up to length len.
private static int intArrayCmpToLen(int[] arg1, int[] arg2, int len) {
for (int i=0; i& i++) {
long b1 = arg1[i] & LONG_MASK;
long b2 = arg2[i] & LONG_MASK;
if (b1 & b2)
return -1;
if (b1 & b2)
* Subtracts two numbers of same length, returning borrow.
private static int subN(int[] a, int[] b, int len) {
long sum = 0;
while(--len &= 0) {
sum = (a[len] & LONG_MASK) -
(b[len] & LONG_MASK) + (sum && 32);
a[len] = (int)
return (int)(sum && 32);
* Multiply an array by one word k and add to result, return the carry
static int mulAdd(int[] out, int[] in, int offset, int len, int k) {
long kLong = k & LONG_MASK;
long carry = 0;
offset = out.length-offset - 1;
for (int j=len-1; j &= 0; j--) {
long product = (in[j] & LONG_MASK) * kLong +
(out[offset] & LONG_MASK) +
out[offset--] = (int)
carry = product &&& 32;
return (int)
* Add one word to the number a mlen words into a. Return the resulting
static int addOne(int[] a, int offset, int mlen, int carry) {
offset = a.length-1-mlen-
long t = (a[offset] & LONG_MASK) + (carry & LONG_MASK);
a[offset] = (int)t;
if ((t &&& 32) == 0)
while (--mlen &= 0) {
if (--offset & 0) { // Carry out of number
a[offset]++;
if (a[offset] != 0)
* Returns a BigInteger whose value is (this ** exponent) mod (2**p)
private BigInteger modPow2(BigInteger exponent, int p) {
* Perform exponentiation using repeated squaring trick, chopping off
* high order bits as indicated by modulus.
BigInteger result = valueOf(1);
BigInteger baseToPow2 = this.mod2(p);
int expOffset = 0;
int limit = exponent.bitLength();
if (this.testBit(0))
limit = (p-1) & limit ? (p-1) :
while (expOffset & limit) {
if (exponent.testBit(expOffset))
result = result.multiply(baseToPow2).mod2(p);
expOffset++;
if (expOffset & limit)
baseToPow2 = baseToPow2.square().mod2(p);
* Returns a BigInteger whose value is this mod(2**p).
* Assumes that this {@code BigInteger &= 0} and {@code p & 0}.
private BigInteger mod2(int p) {
if (bitLength() &= p)
return this;
// Copy remaining ints of mag
int numInts = (p + 31) &&& 5;
int[] mag = new int[numInts];
for (int i=0; i&numI i++)
mag[i] = this.mag[i + (this.mag.length - numInts)];
// Mask out any excess bits
int excessBits = (numInts && 5) -
mag[0] &= (1L && (32-excessBits)) - 1;
return (mag[0]==0 ? new BigInteger(1, mag) : new BigInteger(mag, 1));
* Returns a BigInteger whose value is {@code (this}&sup&-1&/sup& {@code mod m)}.
m the modulus.
* @return {@code this}&sup&-1&/sup& {@code mod m}.
* @throws ArithmeticException {@code
m &= 0}, or this BigInteger
has no multiplicative inverse mod m (that is, this BigInteger
is not &i&relatively prime&/i& to m).
public BigInteger modInverse(BigInteger m) {
if (m.signum != 1)
throw new ArithmeticException("BigInteger: modulus not positive");
if (m.equals(ONE))
return ZERO;
// Calculate (this mod m)
BigInteger modVal = this;
if (signum & 0 || (this.compareMagnitude(m) &= 0))
modVal = this.mod(m);
if (modVal.equals(ONE))
return ONE;
MutableBigInteger a = new MutableBigInteger(modVal);
MutableBigInteger b = new MutableBigInteger(m);
MutableBigInteger result = a.mutableModInverse(b);
return result.toBigInteger(1);
// Shift Operations
* Returns a BigInteger whose value is {@code (this && n)}.
* The shift distance, {@code n}, may be negative, in which case
* this method performs a right shift.
* (Computes &tt&floor(this * 2&sup&n&/sup&)&/tt&.)
n shift distance, in bits.
* @return {@code this && n}
* @see #shiftRight
public BigInteger shiftLeft(int n) {
if (signum == 0)
return ZERO;
return this;
return shiftRight(-n);
int nInts = n &&& 5;
int nBits = n & 0x1f;
int magLen = mag.
int newMag[] = null;
if (nBits == 0) {
newMag = new int[magLen + nInts];
for (int i=0; i&magL i++)
newMag[i] = mag[i];
int i = 0;
int nBits2 = 32 - nB
int highBits = mag[0] &&& nBits2;
if (highBits != 0) {
newMag = new int[magLen + nInts + 1];
newMag[i++] = highB
newMag = new int[magLen + nInts];
while (j & magLen-1)
newMag[i++] = mag[j++] && nBits | mag[j] &&& nBits2;
newMag[i] = mag[j] && nB
return new BigInteger(newMag, signum);
* Returns a BigInteger whose value is {@code (this && n)}.
* extension is performed.
The shift distance, {@code n}, may be
* negative, in which case this method performs a left shift.
* (Computes &tt&floor(this / 2&sup&n&/sup&)&/tt&.)
n shift distance, in bits.
* @return {@code this && n}
* @see #shiftLeft
public BigInteger shiftRight(int n) {
return this;
return shiftLeft(-n);
int nInts = n &&& 5;
int nBits = n & 0x1f;
int magLen = mag.
int newMag[] = null;
// Special case: entire contents shifted off the end
if (nInts &= magLen)
return (signum &= 0 ? ZERO : negConst[1]);
if (nBits == 0) {
int newMagLen = magLen - nI
newMag = new int[newMagLen];
for (int i=0; i&newMagL i++)
newMag[i] = mag[i];
int i = 0;
int highBits = mag[0] &&& nB
if (highBits != 0) {
newMag = new int[magLen - nInts];
newMag[i++] = highB
newMag = new int[magLen - nInts -1];
int nBits2 = 32 - nB
while (j & magLen - nInts - 1)
newMag[i++] = (mag[j++] && nBits2) | (mag[j] &&& nBits);
if (signum & 0) {
// Find out whether any one-bits were shifted off the end.
boolean onesLost = false;
for (int i=magLen-1, j=magLen-nI i&=j && !onesL i--)
onesLost = (mag[i] != 0);
if (!onesLost && nBits != 0)
onesLost = (mag[magLen - nInts - 1] && (32 - nBits) != 0);
if (onesLost)
newMag = javaIncrement(newMag);
return new BigInteger(newMag, signum);
int[] javaIncrement(int[] val) {
int lastSum = 0;
for (int i=val.length-1;
i &= 0 && lastSum == 0; i--)
lastSum = (val[i] += 1);
if (lastSum == 0) {
val = new int[val.length+1];
val[0] = 1;
// Bitwise Operations
* Returns a BigInteger whose value is {@code (this & val)}.
* method returns a negative BigInteger if and only if this and val are
* both negative.)
* @param val value to be AND'ed with this BigInteger.
* @return {@code this & val}
public BigInteger and(BigInteger val) {
int[] result = new int[Math.max(intLength(), val.intLength())];
for (int i=0; i&result. i++)
result[i] = (getInt(result.length-i-1)
& val.getInt(result.length-i-1));
return valueOf(result);
* Returns a BigInteger whose value is {@code (this | val)}.
(This method
* returns a negative BigInteger if and only if either this or val is
* negative.)
* @param val value to be OR'ed with this BigInteger.
* @return {@code this | val}
public BigInteger or(BigInteger val) {
int[] result = new int[Math.max(intLength(), val.intLength())];
for (int i=0; i&result. i++)
result[i] = (getInt(result.length-i-1)
| val.getInt(result.length-i-1));
return valueOf(result);
* Returns a BigInteger whose value is {@code (this ^ val)}.
(This method
* returns a negative BigInteger if and only if exactly one of this and
* val are negative.)
* @param val value to be XOR'ed with this BigInteger.
* @return {@code this ^ val}
public BigInteger xor(BigInteger val) {
int[] result = new int[Math.max(intLength(), val.intLength())];
for (int i=0; i&result. i++)
result[i] = (getInt(result.length-i-1)
^ val.getInt(result.length-i-1));
return valueOf(result);
* Returns a BigInteger whose value is {@code (~this)}.
(This method
* returns a negative value if and only if this BigInteger is
* non-negative.)
* @return {@code ~this}
public BigInteger not() {
int[] result = new int[intLength()];
for (int i=0; i&result. i++)
result[i] = ~getInt(result.length-i-1);
return valueOf(result);
* Returns a BigInteger whose value is {@code (this & ~val)}.
* method, which is equivalent to {@code and(val.not())}, is provided as
* a convenience for masking operations.
(This method returns a negative
* BigInteger if and only if {@code this} is negative and {@code val} is
* positive.)
* @param val value to be complemented and AND'ed with this BigInteger.
* @return {@code this & ~val}
public BigInteger andNot(BigInteger val) {
int[] result = new int[Math.max(intLength(), val.intLength())];
for (int i=0; i&result. i++)
result[i] = (getInt(result.length-i-1)
& ~val.getInt(result.length-i-1));
return valueOf(result);
// Single Bit Operations
* Returns {@code true} if and only if the designated bit is set.
* (Computes {@code ((this & (1&&n)) != 0)}.)
n index of bit to test.
* @return {@code true} if and only if the designated bit is set.
* @throws ArithmeticException {@code n} is negative.
public boolean testBit(int n) {
throw new ArithmeticException("Negative bit address");
return (getInt(n &&& 5) & (1 && (n & 31))) != 0;
* Returns a BigInteger whose value is equivalent to this BigInteger
* with the designated bit set.
(Computes {@code (this | (1&&n))}.)
n index of bit to set.
* @return {@code this | (1&&n)}
* @throws ArithmeticException {@code n} is negative.
public BigInteger setBit(int n) {
throw new ArithmeticException("Negative bit address");
int intNum = n &&& 5;
int[] result = new int[Math.max(intLength(), intNum+2)];
for (int i=0; i&result. i++)
result[result.length-i-1] = getInt(i);
result[result.length-intNum-1] |= (1 && (n & 31));
return valueOf(result);
* Returns a BigInteger whose value is equivalent to this BigInteger
* with the designated bit cleared.
* (Computes {@code (this & ~(1&&n))}.)
n index of bit to clear.
* @return {@code this & ~(1&&n)}
* @throws ArithmeticException {@code n} is negative.
public BigInteger clearBit(int n) {
throw new ArithmeticException("Negative bit address");
int intNum = n &&& 5;
int[] result = new int[Math.max(intLength(), ((n + 1) &&& 5) + 1)];
for (int i=0; i&result. i++)
result[result.length-i-1] = getInt(i);
result[result.length-intNum-1] &= ~(1 && (n & 31));
return valueOf(result);
* Returns a BigInteger whose value is equivalent to this BigInteger
* with the designated bit flipped.
* (Computes {@code (this ^ (1&&n))}.)
n index of bit to flip.
* @return {@code this ^ (1&&n)}
* @throws ArithmeticException {@code n} is negative.
public BigInteger flipBit(int n) {
throw new ArithmeticException("Negative bit address");
int intNum = n &&& 5;
int[] result = new int[Math.max(intLength(), intNum+2)];
for (int i=0; i&result. i++)
result[result.length-i-1] = getInt(i);
result[result.length-intNum-1] ^= (1 && (n & 31));
return valueOf(result);
* Returns the index of the rightmost (lowest-order) one bit in this
* BigInteger (the number of zero bits to the right of the rightmost
* one bit).
Returns -1 if this BigInteger contains no one bits.
* (Computes {@code (this==0? -1 : log2(this & -this))}.)
* @return index of the rightmost one bit in this BigInteger.
public int getLowestSetBit() {
@SuppressWarnings("deprecation") int lsb = lowestSetBit - 2;
if (lsb == -2) {
// lowestSetBit not initialized yet
if (signum == 0) {
// Search for lowest order nonzero int
for (i=0; (b = getInt(i))==0; i++)
lsb += (i && 5) + Integer.numberOfTrailingZeros(b);
lowestSetBit = lsb + 2;
// Miscellaneous Bit Operations
* Returns the number of bits in the minimal two's-complement
* representation of this BigInteger, &i&excluding&/i& a sign bit.
* For positive BigIntegers, this is equivalent to the number of bits in
* the ordinary binary representation.
* {@code (ceil(log2(this & 0 ? -this : this+1)))}.)
* @return number of bits in the minimal two's-complement
representation of this BigInteger, &i&excluding&/i& a sign bit.
public int bitLength() {
@SuppressWarnings("deprecation") int n = bitLength - 1;
if (n == -1) { // bitLength not initialized yet
int len = m.
if (len == 0) {
n = 0; // offset by one to initialize
// Calculate the bit length of the magnitude
int magBitLength = ((len - 1) && 5) + bitLengthForInt(mag[0]);
if (signum & 0) {
// Check if magnitude is a power of two
boolean pow2 = (bitCnt(mag[0]) == 1);
for(int i=1; i& len && pow2; i++)
pow2 = (mag[i] == 0);
n = (pow2 ? magBitLength -1 : magBitLength);
n = magBitL
bitLength = n + 1;
* Returns the number of bits in the two's complement representation
* of this BigInteger that differ from its sign bit.
This method is
* useful when implementing bit-vector style sets atop BigIntegers.
* @return number of bits in the two's complement representation
of this BigInteger that differ from its sign bit.
public int bitCount() {
@SuppressWarnings("deprecation") int bc = bitCount - 1;
if (bc == -1) {
// bitCount not initialized yet
// offset by one to initialize
// Count the bits in the magnitude
for (int i=0; i&mag. i++)
bc += bitCnt(mag[i]);
if (signum & 0) {
// Count the trailing zeros in the magnitude
int magTrailingZeroCount = 0,
for (j=mag.length-1; mag[j]==0; j--)
magTrailingZeroCount += 32;
magTrailingZeroCount += Integer.numberOfTrailingZeros(mag[j]);
bc += magTrailingZeroCount - 1;
bitCount = bc + 1;
static int bitCnt(int val) {
val -= (0xaaaaaaaa & val) &&& 1;
val = (val & 0x) + ((val &&& 2) & 0x);
val = val + (val &&& 4) & 0x0f0f0f0f;
val += val &&& 8;
val += val &&& 16;
return val & 0xff;
// Primality Testing
* Returns {@code true} if this BigInteger is probably prime,
* {@code false} if it's definitely composite.
* {@code certainty} is {@code
&= 0}, {@code true} is
* returned.
certainty a measure of the uncertainty that the caller is
willing to tolerate: if the call returns {@code true}
the probability that this BigInteger is prime exceeds
(1 - 1/2&sup&{@code certainty}&/sup&).
The execution time of
this method is proportional to the value of this parameter.
* @return {@code true} if this BigInteger is probably prime,
{@code false} if it's definitely composite.
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,

我要回帖

更多关于 开机启动项怎么设置 的文章

 

随机推荐