java的biginteger
在本教程中,我们将在Java中看到关于Biginteger。
为什么我们需要BIGINTEGER?
Java提供各种原始数据类型,如 int
那 long
, 和 double
但是,这些数据类型无法处理非常大的数字,并将溢出导致程序中的问题。 BigInteger
在Java中引入了处理非常大的整数,此类提供所有Java原语整数运算符和所需的Java方法。
lang.math. BigInteger可以处理非常大的整数,并且仅受JVM中可用内存的限制。
让我在一个例子的帮助下证明这一点。
package org.arpit.theitroad; import java.math.BigInteger; public class FactorialMain { public static void main(String[] args) { long l=factorial(40); System.out.println("Factorial of 40 (Long): "+l); BigInteger b1=factorialBigInteger(40); System.out.println("Factorial of 40 (BigInteger): "+b1); } static long factorial(int n) { int res = 1, i; for (i=2; i<=n; i++) res *= i; return res; } static BigInteger factorialBigInteger(int n) { BigInteger res = BigInteger.ONE; int i; for (i=2; i<=n; i++) res = res.multiply(BigInteger.valueOf(i)); return res; } }
正如我们所看到的,我已经写了两个版本的阶乘计算。 factorial()
返回长度谁 factorialBigInteger()
返回Biginteger。
我们将在两种方法的帮助下运行上述程序以计算40的因子。
运行程序时,我们将获得以下:
Factorial of 40 (Long): 0 Factorial of 40 (BigInteger): 815915283247897734345611269596115894272000000000
正如你可以清楚地看到的那样, BigInteger
能够处理40的因子,而 long
由于溢出问题而没有给出正确的结果。
我们应该始终使用BIGINTEGER而不是整数吗?
BigInteger
只有在需要时才使用,因为与BigInteger相关的性能打击,并且每个Biginteger拍摄的内存与内置类型相比相对非常高。
BigInteger is an immutable arbitrary-precision integer.
初始化BigInteger对象
你可以创建 BigInteger
来自字节数组或者字符串。
让我们在一个例子的帮助下看看。
package org.arpit.theitroad; import java.math.BigInteger; public class BigIntegerMain{ public static void main(String args[]) { BigInteger bigIntegerStr = new BigInteger("1357986420123456789"); BigInteger bigIntegerByteArray = new BigInteger( new byte[] { 32, 32, 32, 32 }); BigInteger bigIntegerSigned = new BigInteger(-1, new byte[] { 32, 32, 32, 32 }); System.out.println("bigIntegerStr: "+ bigIntegerStr); System.out.println("bigIntegerByteArray: "+bigIntegerByteArray); System.out.println("bigIntegerSigned: "+bigIntegerSigned); } }
我们也可以使用构造函数将其他基数字符串转换为BigInteger BigInteger(String val, int radix)
package org.arpit.theitroad; import java.math.BigInteger; public class BigIntegerMain{ public static void main(String args[]) { String hexaDecimalStr = "28A"; BigInteger bigIntegerStr = new BigInteger(hexaDecimalStr,16); System.out.println("BigInteger for hexaDecimal 28A: "+ bigIntegerStr); } }
比较两个BIGINTEGER对象
我们可以用 compareTo()
比较两个BIGINTEGERS的方法。 BigInteger
返回0,1或者-1.
让我们在一个例子的帮助下看看:
package org.arpit.theitroad; import java.math.BigInteger; public class BigIntegerCompareToMain{ public static void main(String args[]) { BigInteger b1=new BigInteger("10"); BigInteger b2=new BigInteger("20"); BigInteger b3=new BigInteger("10"); System.out.println("comparing b1 and b2: "+b1.compareTo(b2)); System.out.println("comparing b1 and b3: "+b1.compareTo(b3)); System.out.println("comparing b2 and b3: "+b2.compareTo(b3)); } }
输出:
比较B1和B2:-1比较B1和B3:0比较B2和B3:1
你也可以使用 equals
方法也想检查BigInteger平等。
只需更改比较将等于上面的例子
System.out.println("comparing b1 and b2: "+b1.equals(b2)); System.out.println("comparing b1 and b3: "+b1.equals(b3)); System.out.println("comparing b2 and b3: "+b2.equals(b3));
输出:
comparing b1 and b2: false comparing b1 and b3: true comparing b2 and b3: false
BigInteger
类为零,1和十的常量提供零,因此如果要使用这3个常量进行比较,则不需要创建新对象。
package org.arpit.theitroad; import java.math.BigInteger; public class BigIntegerConstantsMain{ public static void main(String args[]) { BigInteger b1=new BigInteger("0"); BigInteger b2=new BigInteger("1"); BigInteger b3=new BigInteger("10"); System.out.println("comparing b1 to 0: "+b1.equals(BigInteger.ZERO)); System.out.println("comparing b2 to 1: "+b2.equals(BigInteger.ONE)); System.out.println("comparing b3 to 10: "+b3.equals(BigInteger.TEN)); } }
位操作
BigInteger
有比特操作与int和long类似。
我们需要使用方法而不是操作 BigInteger
。
package org.arpit.theitroad; import java.math.BigInteger; public class BigIntegerBitOperationsMain{ public static void main(String args[]) { BigInteger b1=new BigInteger("100"); BigInteger b2=new BigInteger("200"); //b1 and b2 BigInteger and = b1.and(b2); //b1 or b2 BigInteger or = b1.or(b2); //b1 xor b2 BigInteger xor = b1.xor(b2); //not b1 BigInteger not = b1.not(); //b1 andnot b2 BigInteger andNot = b1.andNot(b2); //will shift bits left by 1 which means multiply by 2 BigInteger shiftLeft = b1.shiftLeft(1); //will shift bits right by 1 which means divide by 2 BigInteger shiftRight = b1.shiftRight(1); //bitCount counts all the set bits for example:'1' int bitCount = b1.bitCount(); //bitLength counts all the bits int bitLength = b1.bitLength(); //Find position of first set bit for example:'1' int getLowestSetBit = b1.getLowestSetBit(); //Will check set bit at position 2 boolean testBit2 = b1.testBit(2); //Will set bit at positon 3 BigInteger setBit3 = b1.setBit(3); //Will flip bit at position 3 BigInteger flipBit3 = b1.flipBit(3); //Will clear bit at postion 2 BigInteger clearBit2 = b1.clearBit(2); System.out.println("Binary presentation of b1: "+ Integer.toBinaryString(new Integer(b1.toString()))); System.out.println("b1 and b2: "+and); System.out.println("b1 or b2: "+or); System.out.println("b1 xor b2: "+xor); System.out.println("b1 not: "+not); System.out.println("b1 andNot b2: "+andNot); System.out.println("b1 shiftLeft: "+shiftLeft); System.out.println("b1 shitfRight: "+shiftRight); System.out.println("b1 bitcount: "+bitCount); System.out.println("b1 bitLength: "+bitLength); System.out.println("b1 getLowestSetBit: "+getLowestSetBit); System.out.println("b1 testBit2: "+testBit2); System.out.println("b1 setBit3: "+setBit3); System.out.println("b1 flipBit3: "+flipBit3); System.out.println("b1 clearBit2: "+clearBit2); } }
Min,Max,Pow,ABS,Signum操作
BigInteger
提供执行各种操作的方法,例如min,max,pow,abs,signum。
让我们在一个例子的帮助下看看。
package org.arpit.theitroad; import java.math.BigInteger; public class BigDecimalMiscMain { public static void main(String args[]) { BigInteger b1=new BigInteger("10"); BigInteger b2=new BigInteger("20"); BigInteger b3=new BigInteger("-10"); System.out.println("Min between b1 and b2: "+b1.min(b2)); System.out.println("max between b1 and b2: "+b1.max(b2)); System.out.println("10 to the pow 3: "+b1.pow(3)); System.out.println("Abosolute value of b3: "+b3.abs()); //Signum method helps to identify is //BigInteger is negative, positive or zero System.out.println("Signum for b1: "+b1.signum()); } }
模块化和GCD计算
BigInteger
提供模块化和GCD计算的内置方法。
这是一个例子。
package org.arpit.theitroad; import java.math.BigInteger; public class BigDecimalGCDModMain { public static void main(String args[]) { BigInteger b1=new BigInteger("55"); BigInteger b2=new BigInteger("20"); BigInteger b3=new BigInteger("4"); System.out.println("GCD b1 and b2: "+b1.gcd(b2)); System.out.println("mod b1 and b3: "+b1.mod(b3)); } }
输出:
GCD b1 and b2: 5 mod b1 and b3: 3
素数生成与素性检验
BigInteger
还具有素生成和原始测试的方法。
package org.arpit.theitroad; import java.math.BigInteger; import java.util.Random; public class BigDecimalMiscMain { public static void main(String args[]) { BigInteger pp = BigInteger.probablePrime(10, new Random()); System.out.println("probable prime: "+pp); } }