當前位置: 首頁>>代碼示例>>Java>>正文


Java BigInteger.pow方法代碼示例

本文整理匯總了Java中java.math.BigInteger.pow方法的典型用法代碼示例。如果您正苦於以下問題:Java BigInteger.pow方法的具體用法?Java BigInteger.pow怎麽用?Java BigInteger.pow使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.math.BigInteger的用法示例。


在下文中一共展示了BigInteger.pow方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: sqrtN

import java.math.BigInteger; //導入方法依賴的package包/類
public static BigInteger sqrtN(BigInteger in) {
    BigInteger TWOI = BigInteger.valueOf(2);
    int c;

    // Significantly speed-up algorithm by proper select of initial approximation
    // As square root has 2 times less digits as original value
    // we can start with 2^(length of N1 / 2)
    BigInteger n0 = TWOI.pow(in.bitLength() / 2);
    // Value of approximate value on previous step
    BigInteger np = in;

    do {
        // next approximation step: n0 = (n0 + in/n0) / 2
        n0 = n0.add(in.divide(n0)).divide(TWOI);

        // compare current approximation with previous step
        c = np.compareTo(n0);

        // save value as previous approximation
        np = n0;

        // finish when previous step is equal to current
    }  while (c != 0);

    return n0;
}
 
開發者ID:Panzer1119,項目名稱:JAddOn,代碼行數:27,代碼來源:JBigNumber.java

示例2: pow

import java.math.BigInteger; //導入方法依賴的package包/類
public static void pow(int order) {
    int failCount1 = 0;

    for (int i=0; i<SIZE; i++) {
        // Test identity x^power == x*x*x ... *x
        int power = random.nextInt(6) + 2;
        BigInteger x = fetchNumber(order);
        BigInteger y = x.pow(power);
        BigInteger z = x;

        for (int j=1; j<power; j++)
            z = z.multiply(x);

        if (!y.equals(z))
            failCount1++;
    }
    report("pow for " + order + " bits", failCount1);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:BigIntegerTest.java

示例3: pow

import java.math.BigInteger; //導入方法依賴的package包/類
public static void pow(int order) {
    int failCount1 = 0;

    for (int i=0; i<SIZE; i++) {
        // Test identity x^power == x*x*x ... *x
        int power = rnd.nextInt(6) + 2;
        BigInteger x = fetchNumber(order);
        BigInteger y = x.pow(power);
        BigInteger z = x;

        for (int j=1; j<power; j++)
            z = z.multiply(x);

        if (!y.equals(z))
            failCount1++;
    }
    report("pow for " + order + " bits", failCount1);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:19,代碼來源:BigIntegerTest.java

示例4: testLog2HalfUp

import java.math.BigInteger; //導入方法依賴的package包/類
public void testLog2HalfUp() {
  for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
    int result = BigIntegerMath.log2(x, HALF_UP);
    BigInteger x2 = x.pow(2);
    // x^2 < 2^(2 * result + 1), or else we would have rounded up
    assertTrue(ZERO.setBit(2 * result + 1).compareTo(x2) > 0);
    // x^2 >= 2^(2 * result - 1), or else we would have rounded down
    assertTrue(result == 0 || ZERO.setBit(2 * result - 1).compareTo(x2) <= 0);
  }
}
 
開發者ID:paul-hammant,項目名稱:googles-monorepo-demo,代碼行數:11,代碼來源:BigIntegerMathTest.java

示例5: testLog2HalfDown

import java.math.BigInteger; //導入方法依賴的package包/類
public void testLog2HalfDown() {
  for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
    int result = BigIntegerMath.log2(x, HALF_DOWN);
    BigInteger x2 = x.pow(2);
    // x^2 <= 2^(2 * result + 1), or else we would have rounded up
    assertTrue(ZERO.setBit(2 * result + 1).compareTo(x2) >= 0);
    // x^2 > 2^(2 * result - 1), or else we would have rounded down
    assertTrue(result == 0 || ZERO.setBit(2 * result - 1).compareTo(x2) < 0);
  }
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:11,代碼來源:BigIntegerMathTest.java

示例6: testLog10HalfUp

import java.math.BigInteger; //導入方法依賴的package包/類
@GwtIncompatible // TODO
public void testLog10HalfUp() {
  for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
    int result = BigIntegerMath.log10(x, HALF_UP);
    BigInteger x2 = x.pow(2);
    // x^2 < 10^(2 * result + 1), or else we would have rounded up
    assertTrue(TEN.pow(2 * result + 1).compareTo(x2) > 0);
    // x^2 >= 10^(2 * result - 1), or else we would have rounded down
    assertTrue(result == 0 || TEN.pow(2 * result - 1).compareTo(x2) <= 0);
  }
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:12,代碼來源:BigIntegerMathTest.java

示例7: testLog10HalfDown

import java.math.BigInteger; //導入方法依賴的package包/類
@GwtIncompatible // TODO
public void testLog10HalfDown() {
  for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
    int result = BigIntegerMath.log10(x, HALF_DOWN);
    BigInteger x2 = x.pow(2);
    // x^2 <= 10^(2 * result + 1), or else we would have rounded up
    assertTrue(TEN.pow(2 * result + 1).compareTo(x2) >= 0);
    // x^2 > 10^(2 * result - 1), or else we would have rounded down
    assertTrue(result == 0 || TEN.pow(2 * result - 1).compareTo(x2) < 0);
  }
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:12,代碼來源:BigIntegerMathTest.java

示例8: isKaprekar

import java.math.BigInteger; //導入方法依賴的package包/類
/**
 * Tests if a {@link BigInteger} is a Kaprekar number in the given base.
 *
 * @param  n The BigInteger to be tested.
 * @param  base The base to be used.
 * @param  abort An {@link AtomicBoolean} used to abort the execution
 *               for whatever reason; Can be null.
 * @throws NullPointerException if {@code n} or {@code base} is null.
 * @throws IllegalArgumentException if {@code n} is less than or equal to zero,
 *                                  or if {@code base} is not in the range of 2 to 16.
 * @return {@code true} if {@code n} is a Kaprekar number in the given base, {@code false} if not,
 *         {@code null} if aborted.
 * */
public static Boolean isKaprekar(final BigInteger n, final Byte base, final AtomicBoolean abort) {

    if (n == null)
        throw new NullPointerException("n value can't be null");

    if (base == null)
        throw new NullPointerException("base value can't be null");

    if (n.compareTo(ZERO) <= 0)
        throw new IllegalArgumentException("n value must be greater than zero");

    if (base < 2 || base > 16)
        throw new IllegalArgumentException("base value must be in the range of 2 to 16");

    final BigInteger nSquared = n.pow(2);
    final String nSquaredToString = nSquared.toString(base);
    int index = 1;

    if (n.compareTo(nSquared) == 0)
        return true;

    while (index < nSquaredToString.length()) {
        final BigInteger first = new BigInteger(nSquaredToString.substring(0, index), base);
        final BigInteger second = new BigInteger(nSquaredToString.substring(index, nSquaredToString.length()), base);

        if (n.compareTo(first.add(second)) == 0 && second.compareTo(ZERO) != 0)
            return true;

        index++;

        if (abort != null && abort.get())
            return null;

    }

    return false;
}
 
開發者ID:MaXcRIMe,項目名稱:Positive-Integers-Porperties,代碼行數:51,代碼來源:MathFunctions.java

示例9: main

import java.math.BigInteger; //導入方法依賴的package包/類
public static void main(String args[])throws IOException
{
    BufferedReader dr=new BufferedReader(new InputStreamReader(System.in));
    
    System.out.println("enter the number");
    String n=dr.readLine();
    
    System.out.println("enter the power number");
    int z=Integer.parseInt(dr.readLine());
    
    BigInteger a=new BigInteger(n);
    BigInteger b=a.pow(z);
    
    System.out.println(b);
}
 
開發者ID:shikhar29111996,項目名稱:Java-programs,代碼行數:16,代碼來源:power.java

示例10: getSum

import java.math.BigInteger; //導入方法依賴的package包/類
static long getSum(int base, int power) {
    BigInteger bd = new BigInteger(String.valueOf(base));
    bd = bd.pow(power);

    long sum = 0;

    String s = bd.toString();
    for(int i=0; i<s.length(); i++) {
        String c = s.substring(i,i+1);

        sum += Integer.valueOf(c);
    }

    return sum;
}
 
開發者ID:nikmanG,項目名稱:DailyProgrammer,代碼行數:16,代碼來源:DigitSum.java

示例11: pow

import java.math.BigInteger; //導入方法依賴的package包/類
/**
 * y raised to the x power   
 */
public static BigInteger pow( BigInteger y, BigInteger x ) {
   int exp = x.intValue();
   if ( exp < 1 )
      throw new IllegalArgumentException( "Exponent must be greater than 0" );
   return y.pow( x.intValue() );
}
 
開發者ID:parabuild-ci,項目名稱:parabuild-ci,代碼行數:10,代碼來源:Math.java

示例12: square

import java.math.BigInteger; //導入方法依賴的package包/類
public static void square(int order) {
    int failCount1 = 0;

    for (int i=0; i<SIZE; i++) {
        // Test identity x^2 == x*x
        BigInteger x  = fetchNumber(order);
        BigInteger xx = x.multiply(x);
        BigInteger x2 = x.pow(2);

        if (!x2.equals(xx))
            failCount1++;
    }
    report("square for " + order + " bits", failCount1);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:15,代碼來源:BigIntegerTest.java

示例13: log10

import java.math.BigInteger; //導入方法依賴的package包/類
/**
 * 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();
  }
}
 
開發者ID:s-store,項目名稱:sstore-soft,代碼行數:75,代碼來源:BigIntegerMath.java

示例14: log2

import java.math.BigInteger; //導入方法依賴的package包/類
/**
 * Returns the base-2 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 two
 */
@SuppressWarnings("fallthrough")
// TODO(kevinb): remove after this warning is disabled globally
public static int log2(BigInteger x, RoundingMode mode) {
  checkPositive("x", checkNotNull(x));
  int logFloor = x.bitLength() - 1;
  switch (mode) {
    case UNNECESSARY:
      checkRoundingUnnecessary(isPowerOfTwo(x)); // fall through
    case DOWN:
    case FLOOR:
      return logFloor;

    case UP:
    case CEILING:
      return isPowerOfTwo(x) ? logFloor : logFloor + 1;

    case HALF_DOWN:
    case HALF_UP:
    case HALF_EVEN:
      if (logFloor < SQRT2_PRECOMPUTE_THRESHOLD) {
        BigInteger halfPower =
            SQRT2_PRECOMPUTED_BITS.shiftRight(SQRT2_PRECOMPUTE_THRESHOLD - logFloor);
        if (x.compareTo(halfPower) <= 0) {
          return logFloor;
        } else {
          return logFloor + 1;
        }
      }
      // Since sqrt(2) is irrational, log2(x) - logFloor cannot be exactly 0.5
      //
      // To determine which side of logFloor.5 the logarithm is,
      // we compare x^2 to 2^(2 * logFloor + 1).
      BigInteger x2 = x.pow(2);
      int logX2Floor = x2.bitLength() - 1;
      return (logX2Floor < 2 * logFloor + 1) ? logFloor : logFloor + 1;

    default:
      throw new AssertionError();
  }
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:48,代碼來源:BigIntegerMath.java

示例15: log10

import java.math.BigInteger; //導入方法依賴的package包/類
/**
 * 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();
  }
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:75,代碼來源:BigIntegerMath.java


注:本文中的java.math.BigInteger.pow方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。