# Thread: Dealing with really big numbers

1. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## 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!

2. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8

3. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## 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:
Java 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)?

4. ## Re: Dealing with really big numbers

Why not just create a loop starting with BigInteger.ONE?

Java 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
}```

5. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## Re: Dealing with really big numbers

Thanks sunde! My code works now!

Java 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);
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•