Package java.math

Examples of java.math.BigInteger.multiply()


   
    private static BigInteger sqrt(BigInteger x) {
      BigInteger y = BigInteger.ZERO;
      for (int i = (x.bitLength() - 1) / 2; i >= 0; i--) {
        y = y.setBit(i);
        if (y.multiply(y).compareTo(x) > 0)
          y = y.clearBit(i);
      }
      return y;
    }
   
View Full Code Here


 
 
  public String run() {
    BigInteger modulus = BigInteger.TEN.pow(10);
    BigInteger n = BigInteger.valueOf(2).modPow(BigInteger.valueOf(7830457), modulus);
    n = n.multiply(BigInteger.valueOf(28433)).mod(modulus);
    n = n.add(BigInteger.ONE).mod(modulus);
    return String.format("%010d", n);
  }
 
}
View Full Code Here

  public static BigInteger factorial(int n) {
    if (n < 0)
      throw new IllegalArgumentException("Factorial of negative number");
    BigInteger prod = BigInteger.ONE;
    for (int i = 2; i <= n; i++)
      prod = prod.multiply(BigInteger.valueOf(i));
    return prod;
  }
 
 
  // Returns n choose k.
View Full Code Here

 
  private static BigInteger sqrt(BigInteger x) {
    BigInteger y = BigInteger.ZERO;
    for (int i = (x.bitLength() - 1) / 2; i >= 0; i--) {
      y = y.setBit(i);
      if (y.multiply(y).compareTo(x) > 0)
        y = y.clearBit(i);
    }
    return y;
  }
 
View Full Code Here

    for (i--; i >= 0; i--) {  // Pick digits from left (most significant) to right
      int digitMod = Library.powMod(10, i, n);
      for (int j = (i == isSumFeasible.size() - 2 ? 1 : 0); j <= 2; j++) {  // Pick current digit
        int newRem = (remainder - digitMod * j % n + n) % n;
        if (isSumFeasible.get(i)[newRem] > 0) {
          solution = solution.multiply(BigInteger.valueOf(10)).add(BigInteger.valueOf(j));
          remainder = newRem;
          continue outer;
        }
      }
      throw new AssertionError();
View Full Code Here

   
    // This is related to computing totients.
    // To make the totient smaller relative to the number, we must add new prime factors.
    for (int p = 2; ; p = nextPrime(p)) {
      numer = numer.multiply(BigInteger.valueOf(p - 1));
      denom = denom.multiply(BigInteger.valueOf(p));
      if (numer.multiply(TARGET_DENOMINATOR).compareTo(denom.multiply(TARGET_NUMERATOR)) < 0) {
        for (int i = 1; i <= p; i++) {
          BigInteger n = numer.multiply(BigInteger.valueOf(i));
          BigInteger d = denom.multiply(BigInteger.valueOf(i)).subtract(BigInteger.ONE);
          if (n.multiply(TARGET_DENOMINATOR).compareTo(d.multiply(TARGET_NUMERATOR)) < 0)
View Full Code Here

    // This is related to computing totients.
    // To make the totient smaller relative to the number, we must add new prime factors.
    for (int p = 2; ; p = nextPrime(p)) {
      numer = numer.multiply(BigInteger.valueOf(p - 1));
      denom = denom.multiply(BigInteger.valueOf(p));
      if (numer.multiply(TARGET_DENOMINATOR).compareTo(denom.multiply(TARGET_NUMERATOR)) < 0) {
        for (int i = 1; i <= p; i++) {
          BigInteger n = numer.multiply(BigInteger.valueOf(i));
          BigInteger d = denom.multiply(BigInteger.valueOf(i)).subtract(BigInteger.ONE);
          if (n.multiply(TARGET_DENOMINATOR).compareTo(d.multiply(TARGET_NUMERATOR)) < 0)
            return d.add(BigInteger.ONE).toString();
View Full Code Here

      denom = denom.multiply(BigInteger.valueOf(p));
      if (numer.multiply(TARGET_DENOMINATOR).compareTo(denom.multiply(TARGET_NUMERATOR)) < 0) {
        for (int i = 1; i <= p; i++) {
          BigInteger n = numer.multiply(BigInteger.valueOf(i));
          BigInteger d = denom.multiply(BigInteger.valueOf(i)).subtract(BigInteger.ONE);
          if (n.multiply(TARGET_DENOMINATOR).compareTo(d.multiply(TARGET_NUMERATOR)) < 0)
            return d.add(BigInteger.ONE).toString();
        }
      }
    }
  }
View Full Code Here

 
  // Returns 1^2 + 2^2 + 3^2 + ... + n^2 mod MODULUS
  private static long sumSquaresMod(long n) {
    BigInteger x = BigInteger.valueOf(n);
    BigInteger y = x.multiply(x.add(BigInteger.ONE));
    y = y.multiply(x.shiftLeft(1).add(BigInteger.ONE));
    y = y.divide(SIX_BI);
    y = y.mod(MODULUS_BI);
    return y.longValue();
  }
 
View Full Code Here

   * @return the factorial for n
   */
  private static BigInteger getFactorial(int n) {
    BigInteger fact = BigInteger.ONE;
    for (int i = n; i > 1; i--) {
      fact = fact.multiply(new BigInteger(Integer.toString(i)));
    }
    return fact;
  }

  /**
 
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.