Trieda Big Integer rozširuje Number a implementuje rozhranie Comparable. Poskytuje analógy ku všetkým primitívnym celočíselným operátorom Java a všetkým metódam z balíka java.lang.Math.
Pozostáva z mnohých metód alebo operácií pre modulárnu aritmetiku, GCD a mnoho ďalších, ktoré sú popísané nižšie.
| abs() | Vráti BigInteger, ktorého hodnota je absolútna hodnota tohto BigInteger. |
| pridať () | Táto metóda vráti BigInteger jednoduchým výpočtom hodnoty „this + val“. |
| a () | Táto metóda vráti BigInteger vypočítaním hodnoty „this & val“. |
| a nie() | Táto metóda vráti BigInteger výpočtom hodnoty 'this & ~val '. |
| bitCount() | Táto metóda vráti počet bitov v reprezentácii dvoch doplnkov tohto BigInteger, ktorý sa líši od jeho znamienkového bitu. |
| bitLength() | Táto metóda vracia počet bitov v reprezentácii tohto znamienkového bitu s minimálne dvoma doplnkami bez znamienkového bitu. |
| clearBit() | Táto metóda vráti BigInteger, ktorého hodnota sa rovná tomuto BigInteger, ktorého určený bit je vymazaný. |
| porovnať s() | Táto metóda porovnáva tento BigInteger so zadaným BigInteger. |
| rozdeliť () | Táto metóda vráti BigInteger výpočtom hodnoty 'this /~val'. |
| splitAndRemainder() | Táto metóda vráti BigInteger výpočtom hodnoty 'this & ~val ', po ktorej nasleduje 'this%value'. |
| doubleValue() | Táto metóda prevedie toto BigInteger na dvojnásobok. |
| rovná sa() | Táto metóda porovnáva toto BigInteger s daným objektom z hľadiska rovnosti. |
| flipBit() | Táto metóda vráti BigInteger, ktorého hodnota sa rovná tomuto BigInteger s preklopeným určeným bitom. |
| floatValue() | Táto metóda prevedie toto BigInteger na float. |
| gcd() | Táto metóda vráti BigInteger, ktorého hodnota je najväčší spoločný deliteľ medzi abs(this) a abs(val). |
| getLowestSetBit() | Táto metóda vráti index jedného bitu úplne vpravo (najnižšieho rádu) v tomto BigInteger (počet nulových bitov napravo od jedného bitu úplne vpravo). |
| hashCode() | Táto metóda vráti kód hash pre tento BigInteger. |
| intValue() | Táto metóda konvertuje toto BigInteger na int. |
| isProbablePrime() | Táto metóda vráti booleovskú hodnotu „true“ vtedy a len vtedy, ak je toto BigInteger prvočíslo, inak pre zložené hodnoty vráti hodnotu false. |
| longValue() | Táto metóda prekryje toto BigInteger na dlhé. |
| max() | Táto metóda vráti maximum medzi týmto BigInteger a val. |
| min() | Táto metóda vráti minimum medzi týmto BigInteger a val. |
| proti () | Táto metóda vráti hodnotu BigInteger pre tento mod m. |
| modInverse() | Táto metóda vráti BigInteger, ktorého hodnota je „tento inverzný mod m“. |
| modPow() | Táto metóda vráti BigInteger, ktorého hodnota je „thisexponent mod m“. |
| násobiť () | Táto metóda vráti BigInteger vypočítaním hodnoty „this *val“. |
| negovať () | Táto metóda vráti BigInteger, ktorého hodnota je '-this'. |
| nextProbablePrime() | Táto metóda vráti ďalšie prvočíslo, ktoré je väčšie ako toto BigInteger. |
| nie () | Táto metóda vráti BigInteger, ktorého hodnota je '~this'. |
| alebo () | Táto metóda vráti BigInteger, ktorého hodnota je 'toto | val' |
| pow() | Táto metóda vráti BigInteger, ktorého hodnota je 'totoexponent'. |
| probablePrime() | Táto metóda vráti kladné prvočíslo BigInteger so zadanou bitLength. |
| zvyšok() | Táto metóda vráti BigInteger, ktorého hodnota je 'this % val'. |
| setBit() | Táto metóda vráti BigInteger, ktorého hodnota sa rovná tomuto BigInteger s nastaveným určeným bitom. |
| shiftLeft() | Táto metóda vráti BigInteger, ktorého hodnota je 'this << val'. |
| shiftRight() | Táto metóda vráti BigInteger, ktorého hodnota je 'this >> val'. |
| podpísať () | Táto metóda vráti funkciu signum tohto BigInteger. |
| odpočítať () | Táto metóda vráti BigInteger, ktorého hodnota je 'this - val'. |
| testbit() | Táto metóda vráti boolovskú hodnotu „true“, ak je nastavený určený bit. |
| toByteArray() | Táto metóda vráti bajtové pole, ktoré obsahuje reprezentáciu tohto BigInteger ako doplnok dvoch. |
| natiahnuť() | Táto metóda vráti desiatkovú reprezentáciu reťazca tohto BigInteger. |
| hodnota() | Táto metóda vráti BigInteger, ktorého hodnota je ekvivalentná hodnote zadaného long. |
| zadarmo() | Táto metóda vráti BigInteger ny výpočet hodnoty 'this ^ val '. |
Príklad 1
import java.math.BigInteger; public class BigIntegerExample1 { public static void main(String args[]) throws Exception { // Initialize result BigInteger bigInteger = new BigInteger('1'); int n=4; for (int i = 2; i <=n 4 197 ; i++){ returns a biginteger by computing ?this *val ? value. } system.out.println('factorial of : '+biginteger); boolean value ?true? if and only this is prime biginteger2="new" biginteger('197'); system.out.println('isprobableprime method will return '+ biginteger2.isprobableprime(2)); the next integer that greater than biginteger. nextprimenumber="bigInteger2.nextProbablePrime();" system.out.println('prime number to '+nextprimenumber); minimum between val min="bigInteger.min(bigInteger2);" system.out.println('min '+min); maximum max="bigInteger.max(bigInteger2);" system.out.println('maximum '+max); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Factorial of 4 : 24 IsProbablePrime method will return : true Prime Number next to 197 : 199 Min value : 24 Maximum value : 197 </pre> <h2>Example 2</h2> <pre> import java.math.BigInteger; public class BigIntegerExample2 { public static void main(String args[]) throws Exception { // Initialize result BigInteger bigInteger = new BigInteger('17'); //returns the signum function of this BigInteger BigInteger bigInteger2 = new BigInteger('171'); System.out.println('Signum value for '+bigInteger2+' : '+ bigInteger2.signum()); //returns the next prime integer that is greater than this BigInteger. BigInteger sub=bigInteger2.subtract(bigInteger); System.out.println(bigInteger2+'-'+bigInteger+' : '+sub); // returns the quotient after dividing two bigInteger values BigInteger quotient=bigInteger2.divide(bigInteger); System.out.print(bigInteger2+' / '+bigInteger+' : Quotient : '+quotient); //returns the remainder after dividing two bigIntger values BigInteger remainder=bigInteger.remainder(bigInteger2); System.out.println(' Remaider : '+remainder); //returns a BigInteger whose value is ?this << val? BigInteger shiftLeft=bigInteger.shiftLeft(4); System.out.println('ShiftLeft value : '+shiftLeft); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Signum value for 171 : 1 171-17 : 154 171 / 17 : Quotient : 10 Remaider : 17 ShiftLeft value : 272 </pre> <br></=n> Príklad 2
import java.math.BigInteger; public class BigIntegerExample2 { public static void main(String args[]) throws Exception { // Initialize result BigInteger bigInteger = new BigInteger('17'); //returns the signum function of this BigInteger BigInteger bigInteger2 = new BigInteger('171'); System.out.println('Signum value for '+bigInteger2+' : '+ bigInteger2.signum()); //returns the next prime integer that is greater than this BigInteger. BigInteger sub=bigInteger2.subtract(bigInteger); System.out.println(bigInteger2+'-'+bigInteger+' : '+sub); // returns the quotient after dividing two bigInteger values BigInteger quotient=bigInteger2.divide(bigInteger); System.out.print(bigInteger2+' / '+bigInteger+' : Quotient : '+quotient); //returns the remainder after dividing two bigIntger values BigInteger remainder=bigInteger.remainder(bigInteger2); System.out.println(' Remaider : '+remainder); //returns a BigInteger whose value is ?this << val? BigInteger shiftLeft=bigInteger.shiftLeft(4); System.out.println('ShiftLeft value : '+shiftLeft); } } Vyskúšajte to Výkon:
Signum value for 171 : 1 171-17 : 154 171 / 17 : Quotient : 10 Remaider : 17 ShiftLeft value : 272
=n>