# Dealing with really big numbers

• 04-26-2012, 12:30 AM
Zigster
Dealing with really big numbers
I need to find 100!, that is, 100 * 99 * 98 * 97, etc.

I have set up my code, but at *90 the product is 8704899442529685504. And then at 89 it is: -27200710659158016. I guess this is because it's too big for long? So how can I get my really big number?

Cheers!
• 04-26-2012, 12:47 AM
eRaaaa
Re: Dealing with really big numbers
• 04-26-2012, 01:29 AM
Zigster
Re: Dealing with really big numbers
Yeah, been looking at biginteger, but I can't get it to work. Cannot find a decent example of anyone using it either.

I want to replace w with a biginteger:
Code:

```public class Problem020 {         public static void main (String[] args){                 long w, x, y, sum;                 int z;                 w=100;                 x=99;                 do {                         w*=x;                         System.out.println("x: " + x + "\tw: " + w);                         x--;                 }                        while(x>0);                 String Prod = Long.toString(w);                 int len=Prod.length();                 for (z=0;z<len;z++){                         char char1 = Prod.charAt(z);                         System.out.print(char1);                 }         } }```
But I haven't seen any method for multiplying a big integer by an int, only by another big integer. But bigintegers are immutable so that's pretty awkward. Can anyone explain to me how to get a big integer to do the same thing that w is doing? (Continuously multiplying itself by x)?
• 04-26-2012, 05:59 AM
sunde887
Re: Dealing with really big numbers
Why not just create a loop starting with BigInteger.ONE?

Code:

```BigInteger total = BigInteger.ONE; for(BigInteger i = BigInteger.ONE; /* comparison here, check api for method */; i = i.add(BigInteger.ONE)){     //multiply total by i }```
• 04-26-2012, 10:46 AM
Zigster
Re: Dealing with really big numbers
Thanks sunde! My code works now!

Code:

```import java.math.BigInteger; public class Problem020 {         public static void main (String[] args){                 long x;                 int z, b = 0;                 BigInteger bi1 = new BigInteger("100");                 x=99;                                 do                 {                 BigInteger bi2 = BigInteger.valueOf(x);                 bi1 = bi1.multiply(bi2);                 x--;                 }                                 while(x>0);                                 System.out.println("100! is: " + bi1);                 String Prod = (bi1).toString();                 int len=Prod.length();                                 for (z=0;z<len;z++){                         char char1 = Prod.charAt(z);                         int number = Character.digit(char1,10);                         b += number;                 }                 System.out.println("\nThe sum of the digits of 100! is: " + b);         } }```