Big Number problem in JAVA in programming contest

Bignumber problem can be solved easily using BigInteger class in java.Here is a sample for doing this….
[code]
import java.math.BigInteger;

public class BIG
{
public static void main(String[] args)
{
//initialization
BigInteger N1 = new BigInteger ("1000000000000000000");
BigInteger N2 = new BigInteger ("123456789123");
BigInteger N3 = new BigInteger ("50000000000");
//Math operations
BigInteger mult = N1.multiply(N2); //This is how to send arguments in bigint functions
BigInteger add = N1.add(N2);
BigInteger div= N1.divide(N2);
BigInteger substract1 = N1.subtract(N2); //N1-N2
BigInteger substract2 = N2.subtract(N1); //N2-N1
BigInteger gcd = N1.gcd(N3);
//Printing output
System.out.println("Mult " + mult);
System.out.println("add " + add);
System.out.println("div " + div);
System.out.println("substract1 " + substract1);
System.out.println("substract2 " + substract2);
System.out.println("gcd N1 N3 " + gcd);
}
}
[/code]
There are some more built in functions:

BigInteger.ONE; (==1)
BigInteger.ZERO;(==0)
A.abs();
A.add(N);
A.divide(N);
A.divideAndRemainder(N); (returns an array)
A.max(N);
A.min(N);
A.mod(N);
A.multiply(N);
A.remainder(N);
A.signum(N);

A.doubleValue();
A.floatValue();
A.intValue();
A.longValue();
A.toString();
A.compareTo(N)

converting an integer to bigint
BigInteger A = BigInteger.valueOf(20000);
You can take input using scanner just as you input int,long etc.

2 Replies to “Big Number problem in JAVA in programming contest”

Leave a Reply

Your email address will not be published. Required fields are marked *