import java.math.*; public class BigIntegerExample { public static void main (String[] argv) { // Create two instances with 5 and 2 as actual values. BigInteger A = new BigInteger ("5"); BigInteger B = new BigInteger ("2"); // Examples of arithmetic operations: BigInteger C = A.add (B); // 7 BigInteger D = A.multiply (B); // 10 BigInteger E = A.divide (B); // 2 BigInteger F = A.mod (B); // 1 System.out.println ("A=" + A + " B=" + B + " C=" + C + " D=" + D + " E=" + E + " F=" + F); // Two constants. BigInteger zero = new BigInteger ("0"); BigInteger one = new BigInteger ("1"); BigInteger G = A.add (zero); // 5 BigInteger H = B.multiply (one); // 2 System.out.println ("G=" + G + " H=" + H); // Largest possible int. int x = 2147483647; int y = x*x; // "Garbage" result System.out.println ("x=" + x + " y=" + y); // But this works fine: BigInteger X = new BigInteger ("2147483647"); BigInteger Y = X.multiply (X); System.out.println ("X=" + X + " Y=" + Y); // And now for some really large integers ... // Some of the largest known primes in history, found without computers. // Largest known in 1588, by P.Cataldi: BigInteger P1 = new BigInteger ("524287"); // Next record in 1772, by Euler: BigInteger P2 = new BigInteger ("2147483647"); // Then in 1876 by Lucas: BigInteger P3 = new BigInteger ("170141183460469231731687303715884105727"); // Next record in 1951 by Ferrier: BigInteger P4 = new BigInteger ("20988936657440586486151264256610222593863921"); } }