예제 #1
14
 /** Constructs a new BigFraction from the given BigDecimal object. */
 public BigFraction(BigDecimal d) {
   this(
       d.scale() < 0
           ? d.unscaledValue().multiply(BigInteger.TEN.pow(-d.scale()))
           : d.unscaledValue(),
       d.scale() < 0 ? BigInteger.ONE : BigInteger.TEN.pow(d.scale()));
 }
예제 #2
0
파일: BigFraction.java 프로젝트: ldez/svm
  /**
   * Constructs a new BigFraction from two BigDecimals.
   *
   * @throws ArithemeticException if denominator == 0.
   */
  private static BigFraction valueOfHelper(
      final BigDecimal numerator, final BigDecimal denominator) {
    // Note: Cannot use .equals(BigDecimal.ZERO), because "0.00" != "0.0".
    if (denominator.unscaledValue().equals(BigInteger.ZERO)) {
      throw new ArithmeticException("Divide by zero: fraction denominator is zero.");
    }

    // Format of BigDecimal: unscaled / 10^scale
    BigInteger tmpNumerator = numerator.unscaledValue();
    BigInteger tmpDenominator = denominator.unscaledValue();

    // (u1/10^s1) / (u2/10^s2) = u1 / (u2 * 10^(s1-s2)) = (u1 * 10^(s2-s1))
    // / u2
    if (numerator.scale() > denominator.scale()) {
      tmpDenominator =
          tmpDenominator.multiply(BigInteger.TEN.pow(numerator.scale() - denominator.scale()));
    } else if (numerator.scale() < denominator.scale()) {
      tmpNumerator =
          tmpNumerator.multiply(BigInteger.TEN.pow(denominator.scale() - numerator.scale()));
      // else: scales are equal, do nothing.
    }

    final BigInteger gcd = tmpNumerator.gcd(tmpDenominator);
    tmpNumerator = tmpNumerator.divide(gcd);
    tmpDenominator = tmpDenominator.divide(gcd);

    if (tmpDenominator.signum() < 0) {
      tmpNumerator = tmpNumerator.negate();
      tmpDenominator = tmpDenominator.negate();
    }

    return new BigFraction(tmpNumerator, tmpDenominator, Reduced.YES);
  }
예제 #3
0
파일: Main.java 프로젝트: yssaifullah/algo
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int n = in.nextInt();
   System.out.println(
       (n == 1 ? BigInteger.ZERO : BigInteger.TEN.pow(n - 1))
           .subtract(BigInteger.TEN.pow(in.nextInt()))
           .abs()
           .toString());
 }
예제 #4
0
파일: Main.java 프로젝트: mrain/acm
 public static boolean check(long n, int p) {
   BigInteger P = BigInteger.TEN.pow(p), Q = BigInteger.TEN.pow(p - 1);
   BigInteger N = BigInteger.ONE, tmp = BigInteger.valueOf(2);
   while (n > 0) {
     if ((n % 2) == 1) {
       N = N.multiply(tmp);
       N = N.mod(P);
     }
     tmp = tmp.multiply(tmp);
     tmp = tmp.mod(P);
     n >>= 1;
   }
   N = N.divide(Q);
   if (N.equals(BigInteger.ONE) || N.equals(BigInteger.valueOf(2))) return true;
   else return false;
 }
예제 #5
0
 @GwtIncompatible // TODO
 public void testLog10TrivialOnPowerOf10() {
   BigInteger x = BigInteger.TEN.pow(100);
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     assertEquals(100, BigIntegerMath.log10(x, mode));
   }
 }
예제 #6
0
 /** 将String以char形式读取,转换为BigInteger */
 private static BigInteger toNumber(String string) {
   BigInteger result = BigInteger.ZERO;
   BigInteger temp = BigInteger.ZERO;
   BigInteger thousand = BigInteger.TEN.multiply(BigInteger.TEN).multiply(BigInteger.TEN);
   for (int i = 0; i < string.length(); i++) {
     temp = BigInteger.valueOf((int) (string.charAt(i)));
     result = result.multiply(thousand).add(temp);
   }
   return result;
 }
예제 #7
0
 /** 将BigInteger以char形式读取,转换为String */
 private static String toString(BigInteger number) {
   String result = "";
   int lenth = number.toString().length();
   BigInteger temp = BigInteger.ZERO;
   BigInteger thousand = BigInteger.TEN.multiply(BigInteger.TEN).multiply(BigInteger.TEN);
   for (int i = 0; i < lenth; i += 3) {
     temp = number.mod(thousand);
     number = number.divide(thousand);
     result += (char) (Integer.parseInt(temp.toString()));
   }
   result = new StringBuffer(result).reverse().toString();
   return result;
 }
예제 #8
0
파일: BigFraction.java 프로젝트: ldez/svm
  /** Constructs a new BigFraction from the given BigDecimal object. */
  private static BigFraction valueOfHelper(final BigDecimal d) {
    // BigDecimal format: unscaled / 10^scale.
    BigInteger tmpNumerator = d.unscaledValue();
    BigInteger tmpDenominator = BigInteger.ONE;

    // Special case for d == 0 (math below won't work right)
    // Note: Cannot use d.equals(BigDecimal.ZERO), because
    // BigDecimal.equals()
    // does not consider numbers equal if they have different scales. So,
    // 0.00 is not equal to BigDecimal.ZERO.
    if (tmpNumerator.equals(BigInteger.ZERO)) {
      return BigFraction.ZERO;
    }

    if (d.scale() < 0) {
      tmpNumerator = tmpNumerator.multiply(BigInteger.TEN.pow(-d.scale()));
    } else if (d.scale() > 0) {
      // Now we have the form: unscaled / 10^scale = unscaled / (2^scale *
      // 5^scale)
      // We know then that gcd(unscaled, 2^scale * 5^scale) = 2^commonTwos
      // * 5^commonFives

      // Easy to determine commonTwos
      final int commonTwos = Math.min(d.scale(), tmpNumerator.getLowestSetBit());
      tmpNumerator = tmpNumerator.shiftRight(commonTwos);
      tmpDenominator = tmpDenominator.shiftLeft(d.scale() - commonTwos);

      // Determining commonFives is a little trickier..
      int commonFives = 0;

      BigInteger[] divMod = null;
      // while(commonFives < d.scale() && tmpNumerator % 5 == 0) {
      // tmpNumerator /= 5; commonFives++; }
      while (commonFives < d.scale()
          && BigInteger.ZERO.equals((divMod = tmpNumerator.divideAndRemainder(BIGINT_FIVE))[1])) {
        tmpNumerator = divMod[0];
        commonFives++;
      }

      if (commonFives < d.scale()) {
        tmpDenominator = tmpDenominator.multiply(BIGINT_FIVE.pow(d.scale() - commonFives));
      }
    }
    // else: d.scale() == 0: do nothing

    // Guaranteed there is no gcd, so fraction is in lowest terms
    return new BigFraction(tmpNumerator, tmpDenominator, Reduced.YES);
  }
예제 #9
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   n = in.nextInt();
   l = new BigInteger(in.next());
   k = in.nextInt();
   m = in.nextInt();
   BigInteger w = BigInteger.TEN.pow(m);
   for (int i = 0; i <= n; ++i) a[i] = new BigInteger(in.next());
   for (int i = 0; i < Math.min(k, n + 1); ++i) {
     t = a[0];
     for (int j = 1; j <= n; ++j) {
       t = t.multiply(l);
       t = t.add(a[j]);
     }
     t = t.mod(w);
     q[n][i] = t.mod(w);
     int ret = 0;
     c = t.toString().toCharArray();
     int ll = c.length;
     for (int j = 0; j < Math.min(m, ll); ++j) {
       ret += (c[ll - 1 - j] - '0') * (c[ll - 1 - j] - '0');
     }
     System.out.println(ret);
     l = l.add(BigInteger.ONE);
   }
   if (k > n) {
     for (int i = n - 1; i >= 0; --i) {
       for (int j = 0; j <= i; j++) q[i][j] = q[i + 1][j + 1].subtract(q[i + 1][j]).mod(w);
     }
     for (int i = 1; i <= n; ++i) q[0][i] = q[0][0];
     int po = 1;
     for (int i = n + 1; i < k; ++i) {
       for (int j = 1; j <= n; ++j)
         q[j][(po + j) % (n + 1)] =
             q[j - 1][(po + j - 1) % (n + 1)].add(q[j][(po + j - 1) % (n + 1)]).mod(w);
       int ret = 0;
       c = q[n][(po + n) % (n + 1)].mod(w).toString().toCharArray();
       int ll = c.length;
       for (int j = 0; j < Math.min(m, ll); ++j) {
         ret += (c[ll - 1 - j] - '0') * (c[ll - 1 - j] - '0');
       }
       System.out.println(ret);
       l = l.add(BigInteger.ONE);
       ++po;
     }
   }
 }
예제 #10
0
 public static SMTAffineTerm create(Rational factor, Term subterm) {
   Sort sort = subterm.getSort();
   Map<Term, Rational> summands;
   Rational constant;
   if (factor.equals(Rational.ZERO)) {
     summands = Collections.emptyMap();
     constant = Rational.ZERO;
   } else if (subterm instanceof SMTAffineTerm) {
     SMTAffineTerm a = (SMTAffineTerm) subterm;
     constant = a.mConstant.mul(factor);
     summands = new HashMap<Term, Rational>();
     for (Map.Entry<Term, Rational> me : a.mSummands.entrySet()) {
       summands.put(me.getKey(), me.getValue().mul(factor));
     }
   } else if (subterm instanceof ConstantTerm) {
     Object value = ((ConstantTerm) subterm).getValue();
     if (value instanceof BigInteger) {
       constant = Rational.valueOf((BigInteger) value, BigInteger.ONE).mul(factor);
       summands = Collections.emptyMap();
     } else if (value instanceof BigDecimal) {
       BigDecimal decimal = (BigDecimal) value;
       if (decimal.scale() <= 0) {
         BigInteger num = decimal.toBigInteger();
         constant = Rational.valueOf(num, BigInteger.ONE).mul(factor);
       } else {
         BigInteger num = decimal.unscaledValue();
         BigInteger denom = BigInteger.TEN.pow(decimal.scale());
         constant = Rational.valueOf(num, denom).mul(factor);
       }
       summands = Collections.emptyMap();
     } else if (value instanceof Rational) {
       constant = (Rational) value;
       summands = Collections.emptyMap();
     } else {
       summands = Collections.singletonMap(subterm, factor);
       constant = Rational.ZERO;
     }
   } else {
     summands = Collections.singletonMap(subterm, factor);
     constant = Rational.ZERO;
   }
   return create(summands, constant, sort);
 }
 @Override
 protected void convert(Term term) {
   if (term instanceof ConstantTerm) {
     ConstantTerm ct = (ConstantTerm) term;
     if (ct.getValue() instanceof BigInteger) {
       Rational rat = Rational.valueOf((BigInteger) ct.getValue(), BigInteger.ONE);
       setResult(rat.toTerm(term.getSort()));
     } else if (ct.getValue() instanceof BigDecimal) {
       BigDecimal decimal = (BigDecimal) ct.getValue();
       Rational rat;
       if (decimal.scale() <= 0) {
         BigInteger num = decimal.toBigInteger();
         rat = Rational.valueOf(num, BigInteger.ONE);
       } else {
         BigInteger num = decimal.unscaledValue();
         BigInteger denom = BigInteger.TEN.pow(decimal.scale());
         rat = Rational.valueOf(num, denom);
       }
       setResult(rat.toTerm(term.getSort()));
     } else if (ct.getValue() instanceof Rational) setResult(ct);
     else setResult(term);
   } else super.convert(term);
 }
예제 #12
0
 @Test
 public void testParseBigInt() throws ParseException {
   assertEquals(BigInteger.TEN, EwsUtilities.parse(BigInteger.class, BigInteger.TEN.toString()));
 }
예제 #13
0
  /**
   * Returns the base-10 logarithm of {@code x}, rounded according to the specified rounding mode.
   *
   * @throws IllegalArgumentException if {@code x <= 0}
   * @throws ArithmeticException if {@code mode} is {@link RoundingMode#UNNECESSARY} and {@code x}
   *     is not a power of ten
   */
  @GwtIncompatible // TODO
  @SuppressWarnings("fallthrough")
  public static int log10(BigInteger x, RoundingMode mode) {
    checkPositive("x", x);
    if (fitsInLong(x)) {
      return LongMath.log10(x.longValue(), mode);
    }
    int approxLog10 = (int) (log2(x, FLOOR) * LN_2 / LN_10);
    BigInteger approxPow = BigInteger.TEN.pow(approxLog10);
    int approxCmp = approxPow.compareTo(x);

    /*
     * We adjust approxLog10 and approxPow until they're equal to floor(log10(x)) and
     * 10^floor(log10(x)).
     */
    if (approxCmp > 0) {
      /*
       * The code is written so that even completely incorrect approximations will still yield the
       * correct answer eventually, but in practice this branch should almost never be entered, and
       * even then the loop should not run more than once.
       */
      do {
        approxLog10--;
        approxPow = approxPow.divide(BigInteger.TEN);
        approxCmp = approxPow.compareTo(x);
      } while (approxCmp > 0);
    } else {
      BigInteger nextPow = BigInteger.TEN.multiply(approxPow);
      int nextCmp = nextPow.compareTo(x);
      while (nextCmp <= 0) {
        approxLog10++;
        approxPow = nextPow;
        approxCmp = nextCmp;
        nextPow = BigInteger.TEN.multiply(approxPow);
        nextCmp = nextPow.compareTo(x);
      }
    }
    int floorLog = approxLog10;
    BigInteger floorPow = approxPow;
    int floorCmp = approxCmp;
    switch (mode) {
      case UNNECESSARY:
        checkRoundingUnnecessary(floorCmp == 0);
        // fall through
      case FLOOR:
      case DOWN:
        return floorLog;
      case CEILING:
      case UP:
        return floorPow.equals(x) ? floorLog : floorLog + 1;
      case HALF_DOWN:
      case HALF_UP:
      case HALF_EVEN:
        // Since sqrt(10) is irrational, log10(x) - floorLog can never be exactly 0.5
        BigInteger x2 = x.pow(2);
        BigInteger halfPowerSquared = floorPow.pow(2).multiply(BigInteger.TEN);
        return (x2.compareTo(halfPowerSquared) <= 0) ? floorLog : floorLog + 1;
      default:
        throw new AssertionError();
    }
  }