<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://wiki.compsci.ca/skins/common/feed.css?270"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.compsci.ca/index.php?feed=atom&amp;target=Newacct&amp;title=Special%3AContributions%2FNewacct</id>
		<title>Compsci.ca Wiki - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.compsci.ca/index.php?feed=atom&amp;target=Newacct&amp;title=Special%3AContributions%2FNewacct"/>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Special:Contributions/Newacct"/>
		<updated>2026-04-15T05:38:22Z</updated>
		<subtitle>From Compsci.ca Wiki</subtitle>
		<generator>MediaWiki 1.16.0</generator>

	<entry>
		<id>http://wiki.compsci.ca/index.php?title=Java_Big_Integers</id>
		<title>Java Big Integers</title>
		<link rel="alternate" type="text/html" href="http://wiki.compsci.ca/index.php?title=Java_Big_Integers"/>
				<updated>2010-02-22T09:39:40Z</updated>
		
		<summary type="html">&lt;p&gt;Newacct: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==What Are They?==&lt;br /&gt;
&lt;br /&gt;
(From sun.com) &amp;quot;Immutable arbitrary-precision integers.&amp;quot;&lt;br /&gt;
(From Answers.com) Immutable: &amp;quot;Incapable of changing or being modified&amp;quot;, meaning the precision cannot be changed (any one else have a better.. desc?)&lt;br /&gt;
&lt;br /&gt;
In 'Laymans' terms, they are very large 'integer' type variables, with the capacity to hold as many digits as your RAM will fit.&lt;br /&gt;
&lt;br /&gt;
==When Should You Use Them?==&lt;br /&gt;
&lt;br /&gt;
They should be used whenever you need to handle very large numbers, anything larger then 'long' variables. Long's have a max a max value of 9223372036854775807. As well, BigInteger provides some useful functions for bit manipulation, GCD, random number, and primality testing and generation.&lt;br /&gt;
&lt;br /&gt;
==Do I Need To Import Anything?==&lt;br /&gt;
&lt;br /&gt;
Yes, add this import line at the top of your java file&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.math.*;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Declaring A BigInteger Variable (4 Examples)==&lt;br /&gt;
&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
BigInteger bigInt0 = BigInteger.ZERO;&lt;br /&gt;
BigInteger bigInt1 = BigInteger.ONE;&lt;br /&gt;
BigInteger bigInt3 = new BigInteger (&amp;quot;3&amp;quot;);&lt;br /&gt;
BigInteger bigInt5 = BigInteger.valueOf(5);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: You cannot pass an int/long into a BigInteger directly, the easiest way to do this it to pass it as a static BigInteger using valueOf, Thank You &amp;quot;OneOffDriveByPoster&amp;quot; for that method.&lt;br /&gt;
&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
long num = 9876543210;&lt;br /&gt;
BigInteger bigInt123 = BigInteger.valueOf (num);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==So, How Do I Use Them?==&lt;br /&gt;
&lt;br /&gt;
Well, BigIntegers contain all the regular math functions, plus more, the main difference is, it does not use symbols such as '*', '+','=','&amp;gt;' etc. rather it uses words (multiply,add,equals,compareTo). Examples;&lt;br /&gt;
&lt;br /&gt;
To Multiply:&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bigInt1.multiply(bigInt3);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Returns: A Big Integer With Value 3 (1*3)&lt;br /&gt;
Whereas, bigInt1 and bigInt3 are both BigInteger's&lt;br /&gt;
&lt;br /&gt;
To Subtract:&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bigInt1.subtract(bigInt3);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Returns: A BigInteger with value -2 (3-1)&lt;br /&gt;
Whereas, bigInt1 and bigInt3 are both BigInteger's&lt;br /&gt;
&lt;br /&gt;
To Compare:&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bigInt1.compareTo(bigInt3);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Returns: An Integer with a negative value (Less Than), 0 (Equal), positive value (Greater Than), in this case, it will be negative since 1 is less then 3.&lt;br /&gt;
Whereas, bigInt1 and bigInt3 are both BigInteger's&lt;br /&gt;
&lt;br /&gt;
To Check If Equal:&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bigInt1.equals(bigInt3);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Returns: A boolean with a value of false since 1 != 3&lt;br /&gt;
Whereas, bigInt1 and bigInt3 are both BigInteger's&lt;br /&gt;
&lt;br /&gt;
You'll notice, its just like dealing with strings (equals, compareTo)...&lt;br /&gt;
&lt;br /&gt;
As well, you can create a new BigInteger inline (outputs 8)&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
System.out.println (&amp;quot;2**3 = &amp;quot;+new BigInteger(&amp;quot;2&amp;quot;).pow(3));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: I Used this as a special example, 'pow', raises the BigInteger '2' to the power of a regular integer '3'. Most of the math methods need BigIntegers as the initial and secondary values but this is a worthy exception to point out.&lt;br /&gt;
&lt;br /&gt;
==Can I Make An Array?==&lt;br /&gt;
&lt;br /&gt;
Sure, why not, arrays can be made of any object/class. Both arrays have a length of 10, both have been initialized to value '1'&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//------Example One-------//&lt;br /&gt;
BigInteger i = BigInteger.ONE;&lt;br /&gt;
BigInteger [] biggg = {i,i,i,i,i,i,i,i,i,i};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//------Example Two-------//&lt;br /&gt;
BigInteger [] biggg = new BigInteger [10];&lt;br /&gt;
for (int c = 0; c &amp;lt; biggg.length; c ++){&lt;br /&gt;
    biggg [c] = BigInteger.ONE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Primality Testing==&lt;br /&gt;
You Mentioned Primality Testing...&lt;br /&gt;
&lt;br /&gt;
Checking if a number is prime is easy with BigIntegers,&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if (new BigInteger(&amp;quot;17&amp;quot;).isProbablePrime(5)){&lt;br /&gt;
        System.out.println(&amp;quot;17 Is Prime!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: The 5 is the certainty that the number is prime, this value reflects how long it takes to complete, for small primes, a value of 1-5 works, but bigger numbers may return false positives unless certainty is &amp;gt;= 5.&lt;br /&gt;
&lt;br /&gt;
==Bit Manipulation==&lt;br /&gt;
I'm A 'Bit' Hungry, Let's Do Bit Manipulation!&lt;br /&gt;
&lt;br /&gt;
One of the most used is XOR, in encryption methods. Eg: 65 XOR 42 = 107, and 107 XOR 42 = 65. The work behind this is, it converts the numbers to binary (1's, 0's), and compares the bits in each position, if they are different, bit = 1, if they are the same, bit = 0&lt;br /&gt;
Kevin wrote:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
65: 1000001_______107: 1101011&lt;br /&gt;
XOR______________XOR&lt;br /&gt;
42: 0101010_______42: 0101010&lt;br /&gt;
=________________=&lt;br /&gt;
107: 1101011______65: 1000001&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
System.out.println (&amp;quot;65 XOR 42 = &amp;quot;+new BigInteger(&amp;quot;65&amp;quot;).xor(new BigInteger(&amp;quot;42&amp;quot;));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Constant Recomendations==&lt;br /&gt;
&lt;br /&gt;
Seeing how it takes quite a bit of typing to make a new BigInteger every time you say want to multiply something by two... bigInt.multiply (new BigInteger(&amp;quot;2&amp;quot;);.. etc.. I recommend making some constants at the top of your program for the most common numbers as follows:&lt;br /&gt;
&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
BigInteger TWO = new BigInteger (&amp;quot;2&amp;quot;);&lt;br /&gt;
BigInteger THREE = new BigInteger (&amp;quot;3&amp;quot;);&lt;br /&gt;
BigInteger FIVE = new BigInteger (&amp;quot;5&amp;quot;);&lt;br /&gt;
BigInteger TEN = new BigInteger (&amp;quot;10&amp;quot;);&lt;br /&gt;
...&lt;br /&gt;
System.out.println (&amp;quot;2 * 10 = &amp;quot;+TWO.multiply(TEN));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==For Loops==&lt;br /&gt;
What About For Loops, Can BigIntegers Be Used?[b]&lt;br /&gt;
&lt;br /&gt;
They can be, but it's inadvisable as it will be extremely slow, unless incrementing by say 100 or 1000... (bellow I used 1 for increment)...&lt;br /&gt;
&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for (BigInteger bigCount = BigInteger.ZERO; bigCount.compareTo(new BigInteger (&amp;quot;99999999999999999&amp;quot;)) &amp;lt; 0; bigCount = bigCount.add(BigInteger.ONE)){&lt;br /&gt;
     System.out.println (&amp;quot;Big:&amp;quot;+bigCount);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example Program==&lt;br /&gt;
Can I See A Full Program Using BigIntegers?&lt;br /&gt;
&lt;br /&gt;
Sure...A Method To Evaluate Factorials! I used BigInteger for the result, because even 25! uses 26 digits, far larger then the max 'long' capacity.&lt;br /&gt;
&lt;br /&gt;
Java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public static BigInteger factorial(int n){&lt;br /&gt;
    /* n must be &amp;gt;= 0, 0! = 1, eg: n=5 returns (5x4x3x2x1) */&lt;br /&gt;
&lt;br /&gt;
    /* Holds the value of the factorial, in BigInteger form */&lt;br /&gt;
    BigInteger product = BigInteger.ONE;&lt;br /&gt;
       &lt;br /&gt;
    /* Inform User that n was erroneous parameter */&lt;br /&gt;
    if (n &amp;lt; 0){&lt;br /&gt;
        System.out.println (&amp;quot;Factorial Error! n = &amp;quot;+n);&lt;br /&gt;
        return new BigInteger (&amp;quot;-1&amp;quot;);&lt;br /&gt;
    }   &lt;br /&gt;
       &lt;br /&gt;
    /* Chain Multiply from 'n' to '1' */&lt;br /&gt;
    for (int c = n; c &amp;gt; 0; c --){&lt;br /&gt;
        product = product.multiply(BigInteger.valueOf(c));&lt;br /&gt;
    }&lt;br /&gt;
                       &lt;br /&gt;
    System.out.println (&amp;quot;Factorial &amp;quot;+n+&amp;quot;! = &amp;quot;+product);&lt;br /&gt;
       &lt;br /&gt;
    /* Return a BigInteger with the product from n! */&lt;br /&gt;
    return product;&lt;br /&gt;
}// end factorial&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==For More Information==&lt;br /&gt;
For More Information &amp;amp; Complete Method List&lt;br /&gt;
&lt;br /&gt;
Check Out Sun's Java Docs On BigInteger. BigInteger @ Sun.com&lt;br /&gt;
(I Highly Recommend You Read Through The Whole Method Summary Section)&lt;br /&gt;
&lt;br /&gt;
As well, for the decimal equivalent, check out 'BigDecimal' BigDecimal @ Sun.com&lt;br /&gt;
BigDecimal's use the same concepts as BigInteger, so I wont go into much detail, but if you have questions about BigDecimals, ask away.&lt;br /&gt;
&lt;br /&gt;
If you have any questions, comments, or things to add, feel free to leave a message, or pm me. -Kevin&lt;br /&gt;
&lt;br /&gt;
==Credits==&lt;br /&gt;
&lt;br /&gt;
Author: [[the_short1]]&lt;br /&gt;
&lt;br /&gt;
Added to Wiki by: [[TheFerret]]&lt;/div&gt;</summary>
		<author><name>Newacct</name></author>	</entry>

	</feed>