当前位置: 首页>>代码示例>>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;未经允许,请勿转载。