Java中的BigInteger类

BigInteger类用于数学运算,该运算涉及超出所有可用基本数据类型限制的非常大的整数计算。

null

通过这种方式,BigInteger类使用起来非常方便,因为它的方法库很大,而且在竞争性编程中也被大量使用。 下面给出了原语算术中的简单语句列表,以及以BigInteger对象表示的类似语句。

例子:

int a, b;                BigInteger A, B; 

初始化如下 跟随 :

a = 54;b = 23;
A  = BigInteger.valueOf(54);B  = BigInteger.valueOf(37); 

对于可用作字符串的整数,可以按如下方式初始化:

A  = new BigInteger(“54”);B  = new BigInteger(“123456789123456789”); 

为了便于初始化,BigInteger类中还定义了一些常量,如下所示:

A = BigInteger.ONE;// Other than this, available constant are BigInteger.ZERO // and BigInteger.TEN 

数学运算如下:

int c = a + b;BigInteger C = A.add(B); 

其他类似的函数有subtract()、multiply()、divide()、remiter(),但所有这些函数都以BigInteger为参数,因此,如果我们希望此操作使用整数或字符串,请在将其传递给函数之前将其转换为BigInteger,如下所示:

String str = “123456789”;BigInteger C = A.add(new BigInteger(str));int val  = 123456789;BigInteger C = A.add(BigInteger.valueOf(val)); 

从BigInteger中提取值如下:

int x   =  A.intValue();   // value should be in limit of int xlong y   = A.longValue();  // value should be in limit of long yString z = A.toString();  

比较

if (a < b) {}         // For primitive intif (A.compareTo(B) < 0)  {} // For BigInteger 

实际上,compareTo根据值返回-1(小于)、0(等于)、1(大于)。为了实现平等,我们还可以使用:

if (A.equals(B)) {}  // A is equal to B 

BigInteger类的方法

方法 执行的动作
添加(BigInteger val) 返回一个BigInteger,其值为(this+val)。
abs() 返回一个BigInteger,其值为该BigInteger的绝对值。
和(大整数val) 返回一个BigInteger,其值为(this&val)。
andNot(大整数val) ) 返回一个BigInteger,其值为(this&~val)。
比特数() 返回此BigInteger的两个补码表示形式中与其符号位不同的位数。
比特长度() 返回此BigInteger的最小补码表示形式中的位数,不包括符号位。
字节值精确() 将此BigInteger转换为字节,检查丢失的信息。
clearBit(int n) 返回一个BigInteger,其值等于该BigInteger,且指定位已清除。
compareTo(BigInteger val) 将此BigInteger与指定的BigInteger进行比较。
除法(大整数val) 返回一个BigInteger,其值为(this/val)。
divideandMainder(BigInteger val) 返回两个大整数的数组,其中包含(this/val)后跟(this%val)。
doubleValue() 将此BigInteger转换为double。
等于(对象x) 将此BigInteger与指定的对象进行相等比较。
flipBit(int n) 返回一个BigInteger,其值与指定位翻转的这个BigInteger相等。
浮动值() 将此BigInteger转换为浮点。
gcd(大整数val) 返回一个BigInteger,其值是abs(this)和abs(val)的最大公约数。
getLowestSetBit() 返回此BigInteger中最右边(最低阶)一位的索引(最右边一位右边的零位数)。
hashCode() 返回此BigInteger的哈希代码。
intValue() 将此BigInteger转换为int。
intValueExact() 将此BigInteger转换为int,检查丢失的信息。
isProbablePrime(整数确定性) 如果这个大整数可能是素数,则返回true;如果它肯定是复合整数,则返回false。
longValue() 将此大整数转换为长整数。
longValueExact() 将此大整数转换为长整数,以检查丢失的信息。
最大值(BigInteger val) 返回此BigInteger和val的最大值。
最小值(大整数) ) 返回这个BigInteger和val的最小值。
mod(大整数m) 返回一个BigInteger,其值为(这个mod m)。
modInverse(大整数m) 返回一个BigInteger,其值为(这个-1 mod m)。
modPow(大整数指数,大整数m) 返回一个BigInteger,其值为(thisexponent mod m)。
乘法(大整数val) 返回一个BigInteger,其值为(this*val)。
否定 返回一个BigInteger,其值为(-this)。
nextProbablePrime() 返回第一个大于这个可能是素数的BigInteger的整数。
不是() 返回一个BigInteger,其值为(~this)。
或(BigInteger val) 返回一个BigInteger,其值为(this | val)。
功率(整数指数) 返回一个值为(thisexponent)的大整数。
probablePrime(整数位长度,随机rnd) 返回一个可能为素数的正BigInteger,并具有指定的位长度。
余数(BigInteger val) 返回一个BigInteger,其值为(此%val)。
setBit(int n) 返回一个BigInteger,其值与指定位集的这个BigInteger相等。
希夫特利夫特(国际北) 返回一个BigInteger,其值为(this<
shiftRight(int n) 返回一个BigInteger,其值为(this>>n)。
shortValueExact() 将此大整数转换为短整数,以检查丢失的信息。
符号() 返回此BigInteger的signum函数。
sqrt() 返回此BigInteger的整数平方根。
sqrtAndRemainder() 返回两个大整数组成的数组,分别包含this的整数平方根s和其余数this–s*s。
减法(大整数val) 返回一个BigInteger,其值为(this–val)。
测试位(int n) 当且仅当设置了指定位时返回true。
toByteArray() 返回一个字节数组,其中包含此BigInteger的两位补码表示形式。
toString() 返回此BigInteger的十进制字符串表示形式。
toString(整数基数) 返回给定基数中此BigInteger的字符串表示形式。
valueOf(长val) 返回一个BigInteger,其值等于指定的长整数的值。
xor(BigInteger val) 返回一个BigInteger,其值为(this^val)。

插图:

Factorial of 100包含158个数字,因此我们无法将其存储在任何可用的原始数据类型中。我们可以存储任意大的整数。这个范围的上限在理论上没有限制,因为内存是动态分配的,但实际上由于内存有限,您可以存储一个整数。最大值其中的位数应足以存储大部分大值。

例子:

JAVA

// Java program to find large factorials using BigInteger
import java.math.BigInteger;
import java.util.Scanner;
public class Example
{
// Returns Factorial of N
static BigInteger factorial( int N)
{
// Initialize result
BigInteger f = new BigInteger(" 1 "); // Or BigInteger.ONE
// Multiply f with 2, 3, ...N
for ( int i = 2 ; i <= N; i++)
f = f.multiply(BigInteger.valueOf(i));
return f;
}
// Driver method
public static void main(String args[]) throws Exception
{
int N = 20 ;
System.out.println(factorial(N));
}
}


输出:

2432902008176640000

提示: 如果我们必须在C++中编写上面的程序,那将太大而复杂,我们可以看看。 大数阶乘 .

因此,在了解了BigInteger类的函数之后,我们可以很容易地解决许多复杂的问题,但是请记住,由于BigInteger类在内部使用一个整数数组进行处理,对BigInteger对象的运算速度不如对基本体的运算速度快,因为在BigInteger上添加函数所需的时间不是常数,它所需的时间与BigInteger的长度成正比,因此,程序的复杂性也会相应地发生变化。

本文由Utkarsh Trivedi撰稿。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

必须阅读:

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享