280 * Translates a byte sub-array containing the two's-complement binary
281 * representation of a BigInteger into a BigInteger. The sub-array is
282 * specified via an offset into the array and a length. The sub-array is
283 * assumed to be in <i>big-endian</i> byte-order: the most significant
284 * byte is the element at index {@code off}. The {@code val} array is
285 * assumed to be unchanged for the duration of the constructor call.
286 *
287 * An {@code IndexOutOfBoundsException} is thrown if the length of the array
288 * {@code val} is non-zero and either {@code off} is negative, {@code len}
289 * is negative, or {@code off+len} is greater than the length of
290 * {@code val}.
291 *
292 * @param val byte array containing a sub-array which is the big-endian
293 * two's-complement binary representation of a BigInteger.
294 * @param off the start offset of the binary representation.
295 * @param len the number of bytes to use.
296 * @throws NumberFormatException {@code val} is zero bytes long.
297 * @throws IndexOutOfBoundsException if the provided array offset and
298 * length would cause an index into the byte array to be
299 * negative or greater than or equal to the array length.
300 * @since 1.9
301 */
302 public BigInteger(byte[] val, int off, int len) {
303 if (val.length == 0) {
304 throw new NumberFormatException("Zero length BigInteger");
305 } else if ((off < 0) || (off >= val.length) || (len < 0) ||
306 (len > val.length - off)) { // 0 <= off < val.length
307 throw new IndexOutOfBoundsException();
308 }
309
310 if (val[off] < 0) {
311 mag = makePositive(val, off, len);
312 signum = -1;
313 } else {
314 mag = stripLeadingZeroBytes(val, off, len);
315 signum = (mag.length == 0 ? 0 : 1);
316 }
317 if (mag.length >= MAX_MAG_LENGTH) {
318 checkRange();
319 }
320 }
368 * whether signum is -1, 0 or 1. The {@code magnitude} array is assumed to
369 * be unchanged for the duration of the constructor call.
370 *
371 * An {@code IndexOutOfBoundsException} is thrown if the length of the array
372 * {@code magnitude} is non-zero and either {@code off} is negative,
373 * {@code len} is negative, or {@code off+len} is greater than the length of
374 * {@code magnitude}.
375 *
376 * @param signum signum of the number (-1 for negative, 0 for zero, 1
377 * for positive).
378 * @param magnitude big-endian binary representation of the magnitude of
379 * the number.
380 * @param off the start offset of the binary representation.
381 * @param len the number of bytes to use.
382 * @throws NumberFormatException {@code signum} is not one of the three
383 * legal values (-1, 0, and 1), or {@code signum} is 0 and
384 * {@code magnitude} contains one or more non-zero bytes.
385 * @throws IndexOutOfBoundsException if the provided array offset and
386 * length would cause an index into the byte array to be
387 * negative or greater than or equal to the array length.
388 * @since 1.9
389 */
390 public BigInteger(int signum, byte[] magnitude, int off, int len) {
391 if (signum < -1 || signum > 1) {
392 throw(new NumberFormatException("Invalid signum value"));
393 } else if ((off < 0) || (len < 0) ||
394 (len > 0 &&
395 ((off >= magnitude.length) ||
396 (len > magnitude.length - off)))) { // 0 <= off < magnitude.length
397 throw new IndexOutOfBoundsException();
398 }
399
400 // stripLeadingZeroBytes() returns a zero length array if len == 0
401 this.mag = stripLeadingZeroBytes(magnitude, off, len);
402
403 if (this.mag.length == 0) {
404 this.signum = 0;
405 } else {
406 if (signum == 0)
407 throw(new NumberFormatException("signum-magnitude mismatch"));
408 this.signum = signum;
2407 return answer;
2408 }
2409 }
2410 }
2411
2412 /**
2413 * Returns the integer square root of this BigInteger. The integer square
2414 * root of the corresponding mathematical integer {@code n} is the largest
2415 * mathematical integer {@code s} such that {@code s*s <= n}. It is equal
2416 * to the value of {@code floor(sqrt(n))}, where {@code sqrt(n)} denotes the
2417 * real square root of {@code n} treated as a real. Note that the integer
2418 * square root will be less than the real square root if the latter is not
2419 * representable as an integral value.
2420 *
2421 * @return the integer square root of {@code this}
2422 * @throws ArithmeticException if {@code this} is negative. (The square
2423 * root of a negative integer {@code val} is
2424 * {@code (i * sqrt(-val))} where <i>i</i> is the
2425 * <i>imaginary unit</i> and is equal to
2426 * {@code sqrt(-1)}.)
2427 * @since 1.9
2428 */
2429 public BigInteger sqrt() {
2430 if (this.signum < 0) {
2431 throw new ArithmeticException("Negative BigInteger");
2432 }
2433
2434 return new MutableBigInteger(this.mag).sqrt().toBigInteger();
2435 }
2436
2437 /**
2438 * Returns an array of two BigIntegers containing the integer square root
2439 * {@code s} of {@code this} and its remainder {@code this - s*s},
2440 * respectively.
2441 *
2442 * @return an array of two BigIntegers with the integer square root at
2443 * offset 0 and the remainder at offset 1
2444 * @throws ArithmeticException if {@code this} is negative. (The square
2445 * root of a negative integer {@code val} is
2446 * {@code (i * sqrt(-val))} where <i>i</i> is the
2447 * <i>imaginary unit</i> and is equal to
2448 * {@code sqrt(-1)}.)
2449 * @see #sqrt()
2450 * @since 1.9
2451 */
2452 public BigInteger[] sqrtAndRemainder() {
2453 BigInteger s = sqrt();
2454 BigInteger r = this.subtract(s.square());
2455 assert r.compareTo(BigInteger.ZERO) >= 0;
2456 return new BigInteger[] {s, r};
2457 }
2458
2459 /**
2460 * Returns a BigInteger whose value is the greatest common divisor of
2461 * {@code abs(this)} and {@code abs(val)}. Returns 0 if
2462 * {@code this == 0 && val == 0}.
2463 *
2464 * @param val value with which the GCD is to be computed.
2465 * @return {@code GCD(abs(this), abs(val))}
2466 */
2467 public BigInteger gcd(BigInteger val) {
2468 if (val.signum == 0)
2469 return this.abs();
2470 else if (this.signum == 0)
|
280 * Translates a byte sub-array containing the two's-complement binary
281 * representation of a BigInteger into a BigInteger. The sub-array is
282 * specified via an offset into the array and a length. The sub-array is
283 * assumed to be in <i>big-endian</i> byte-order: the most significant
284 * byte is the element at index {@code off}. The {@code val} array is
285 * assumed to be unchanged for the duration of the constructor call.
286 *
287 * An {@code IndexOutOfBoundsException} is thrown if the length of the array
288 * {@code val} is non-zero and either {@code off} is negative, {@code len}
289 * is negative, or {@code off+len} is greater than the length of
290 * {@code val}.
291 *
292 * @param val byte array containing a sub-array which is the big-endian
293 * two's-complement binary representation of a BigInteger.
294 * @param off the start offset of the binary representation.
295 * @param len the number of bytes to use.
296 * @throws NumberFormatException {@code val} is zero bytes long.
297 * @throws IndexOutOfBoundsException if the provided array offset and
298 * length would cause an index into the byte array to be
299 * negative or greater than or equal to the array length.
300 * @since 9
301 */
302 public BigInteger(byte[] val, int off, int len) {
303 if (val.length == 0) {
304 throw new NumberFormatException("Zero length BigInteger");
305 } else if ((off < 0) || (off >= val.length) || (len < 0) ||
306 (len > val.length - off)) { // 0 <= off < val.length
307 throw new IndexOutOfBoundsException();
308 }
309
310 if (val[off] < 0) {
311 mag = makePositive(val, off, len);
312 signum = -1;
313 } else {
314 mag = stripLeadingZeroBytes(val, off, len);
315 signum = (mag.length == 0 ? 0 : 1);
316 }
317 if (mag.length >= MAX_MAG_LENGTH) {
318 checkRange();
319 }
320 }
368 * whether signum is -1, 0 or 1. The {@code magnitude} array is assumed to
369 * be unchanged for the duration of the constructor call.
370 *
371 * An {@code IndexOutOfBoundsException} is thrown if the length of the array
372 * {@code magnitude} is non-zero and either {@code off} is negative,
373 * {@code len} is negative, or {@code off+len} is greater than the length of
374 * {@code magnitude}.
375 *
376 * @param signum signum of the number (-1 for negative, 0 for zero, 1
377 * for positive).
378 * @param magnitude big-endian binary representation of the magnitude of
379 * the number.
380 * @param off the start offset of the binary representation.
381 * @param len the number of bytes to use.
382 * @throws NumberFormatException {@code signum} is not one of the three
383 * legal values (-1, 0, and 1), or {@code signum} is 0 and
384 * {@code magnitude} contains one or more non-zero bytes.
385 * @throws IndexOutOfBoundsException if the provided array offset and
386 * length would cause an index into the byte array to be
387 * negative or greater than or equal to the array length.
388 * @since 9
389 */
390 public BigInteger(int signum, byte[] magnitude, int off, int len) {
391 if (signum < -1 || signum > 1) {
392 throw(new NumberFormatException("Invalid signum value"));
393 } else if ((off < 0) || (len < 0) ||
394 (len > 0 &&
395 ((off >= magnitude.length) ||
396 (len > magnitude.length - off)))) { // 0 <= off < magnitude.length
397 throw new IndexOutOfBoundsException();
398 }
399
400 // stripLeadingZeroBytes() returns a zero length array if len == 0
401 this.mag = stripLeadingZeroBytes(magnitude, off, len);
402
403 if (this.mag.length == 0) {
404 this.signum = 0;
405 } else {
406 if (signum == 0)
407 throw(new NumberFormatException("signum-magnitude mismatch"));
408 this.signum = signum;
2407 return answer;
2408 }
2409 }
2410 }
2411
2412 /**
2413 * Returns the integer square root of this BigInteger. The integer square
2414 * root of the corresponding mathematical integer {@code n} is the largest
2415 * mathematical integer {@code s} such that {@code s*s <= n}. It is equal
2416 * to the value of {@code floor(sqrt(n))}, where {@code sqrt(n)} denotes the
2417 * real square root of {@code n} treated as a real. Note that the integer
2418 * square root will be less than the real square root if the latter is not
2419 * representable as an integral value.
2420 *
2421 * @return the integer square root of {@code this}
2422 * @throws ArithmeticException if {@code this} is negative. (The square
2423 * root of a negative integer {@code val} is
2424 * {@code (i * sqrt(-val))} where <i>i</i> is the
2425 * <i>imaginary unit</i> and is equal to
2426 * {@code sqrt(-1)}.)
2427 * @since 9
2428 */
2429 public BigInteger sqrt() {
2430 if (this.signum < 0) {
2431 throw new ArithmeticException("Negative BigInteger");
2432 }
2433
2434 return new MutableBigInteger(this.mag).sqrt().toBigInteger();
2435 }
2436
2437 /**
2438 * Returns an array of two BigIntegers containing the integer square root
2439 * {@code s} of {@code this} and its remainder {@code this - s*s},
2440 * respectively.
2441 *
2442 * @return an array of two BigIntegers with the integer square root at
2443 * offset 0 and the remainder at offset 1
2444 * @throws ArithmeticException if {@code this} is negative. (The square
2445 * root of a negative integer {@code val} is
2446 * {@code (i * sqrt(-val))} where <i>i</i> is the
2447 * <i>imaginary unit</i> and is equal to
2448 * {@code sqrt(-1)}.)
2449 * @see #sqrt()
2450 * @since 9
2451 */
2452 public BigInteger[] sqrtAndRemainder() {
2453 BigInteger s = sqrt();
2454 BigInteger r = this.subtract(s.square());
2455 assert r.compareTo(BigInteger.ZERO) >= 0;
2456 return new BigInteger[] {s, r};
2457 }
2458
2459 /**
2460 * Returns a BigInteger whose value is the greatest common divisor of
2461 * {@code abs(this)} and {@code abs(val)}. Returns 0 if
2462 * {@code this == 0 && val == 0}.
2463 *
2464 * @param val value with which the GCD is to be computed.
2465 * @return {@code GCD(abs(this), abs(val))}
2466 */
2467 public BigInteger gcd(BigInteger val) {
2468 if (val.signum == 0)
2469 return this.abs();
2470 else if (this.signum == 0)
|