本文整理匯總了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;
}
示例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);
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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);
}
示例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;
}
示例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() );
}
示例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);
}
示例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();
}
}
示例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();
}
}
示例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();
}
}