# Thread: How to make ad factorial table? With and without BigInteger.

1. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## How to make ad factorial table? With and without BigInteger.

I need to make a chart, in two collumns, to calculate the factorial of every number, 0 to 30, without BigIntegers. The teacher said that after about 13, it will print it out wrong. Thanks guys.

2. ## Re: How to make ad factorial table? With and without BigInteger.

Do you know how large 30! is? It's 265252859812191058636308480000000 and it's much larger than Integer.MAX_VALUE; it is even larger than the largest long number.

kind regards,

Jos

3. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## Re: How to make ad factorial table? With and without BigInteger.

That is true, but do you have any idea on how to actually code the table?

4. ## Re: How to make ad factorial table? With and without BigInteger.

Originally Posted by zhider
That is true, but do you have any idea on how to actually code the table?
Yep, but what is that rediculous requirement about (about not using BigIntegers)?

kind regards,

Jos

5. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## Re: How to make ad factorial table? With and without BigInteger.

I am supposed to make one without BigIntegers, give a reason why it doesnt work, then make one with BigInts and explain why it does.

6. ## Re: How to make ad factorial table? With and without BigInteger.

Well, you know why using ints (or longs) don't work (30! is larger than that) so build a version that uses BigInts. Tip: n! == n*((n-1)!)

kind regards,

Jos

7. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## Re: How to make ad factorial table? With and without BigInteger.

The code that you put is giving alot of errors in Netbeans, would something like n= x*(x-1) work? any other ideas?

8. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## Re: How to make ad factorial table? With and without BigInteger.

This is what I have roght now, and I think it is far off. Right now it is printing one collumn of 0-30, like it should, but in the other collumn it is just printing 0s all the way down. Any ideas at all on how to fix?
Java Code:
```public static void main(String[] args) {
factorial();
}
public static void factorial (){
int x=0, n=5;
while (x<30){
x=x+1;
while (n>=1){
n= x*(x-1);

}
System.out.println (x+ "   "+ n);
}```

9. ## Re: How to make ad factorial table? With and without BigInteger.

Originally Posted by zhider
The code that you put is giving alot of errors in Netbeans
Ah yes! The old "jam the code snippet someone gave me into a program without understanding it" trick.

10. ## Re: How to make ad factorial table? With and without BigInteger.

Think about what the output would look like. Do you really need 2 loops? Here's how I imagine it:
Java Code:
```loop {
// all printing done on one line
print n
print space
calculate factorial of n
print factorial
}```

11. ## Re: How to make ad factorial table? With and without BigInteger.

Originally Posted by Junky
Do you really need 2 loops?
OK due to the lack of indenting I didn't notice they were nested loops which would be correct unless you calculated the factorial in another method.

12. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## Re: How to make ad factorial table? With and without BigInteger.

Srry about that. Ill read that page tonight (on code conventions), as soon as I get this done. Any ideas on what needs to be changed in the formula, to find the factorial of x, after it adds one to x? thanks.

13. ## Re: How to make ad factorial table? With and without BigInteger.

Do you understand what factorial is? Try writing out the calculation for facortorial 4 on paper. Then factorial 5, factorial 6. Perhaps you can see a pattern. Then try converting it to code. Do not just grab a code snippet from someone without knowing what it does and expect it to work. Especially since you did not copy it correctly.

14. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## Re: How to make ad factorial table? With and without BigInteger.

I do realize what a factorial is and how to solve one on paper, I have been trying on this code for 3 days and cannot figure it out.

15. ## Re: How to make ad factorial table? With and without BigInteger.

If you hadn't noticed the pattern here it is:

4! = 1*2*3*4
5! = 1*2*3*4*5
6! = 1*2*3*4*5*6

or
4! = 1*2*3*4
5! = 4! * 5
6! = 5! * 6

This shows that the best way to solve factorial is to use recursion and if you look at the snippet Jos gave you (and the bit you missed) there was another ! at the end of the equation. If you cannot use recursion then you need to calculate it the old fashion way using a loop. BTW why have you initialised n to 5?

16. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## Re: How to make ad factorial table? With and without BigInteger.

I didnt miss the !, I got an error when i put it. ')' expected. and ill look into recursion, thanks.

17. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## Re: How to make ad factorial table? With and without BigInteger.

And i was trying to test it with some numbers, and dont know how to set them in the actual methods

18. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## Re: How to make ad factorial table? With and without BigInteger.

When I use that code n! == n*((n-1)!); tells me the following ';' expected, Not a statement, ';' expected, Not a statement. I think it says ';' expected, because its looking for an operation between n and ! and (n-1) and !. How do I fix this?

19. ## Re: How to make ad factorial table? With and without BigInteger.

Originally Posted by zhider
I didnt miss the !, I got an error when i put it. ')' expected. and ill look into recursion, thanks.
Of course you got an error. The ! means factorial in Math but it is the not operator (as in a != b). Once again this goes back to just grabbing some code snippet without understanding what you are doing.
Java Code:
```declare a total variable
initialise it to 1
declare a factor variable
initialise it to 2
while factor is less than target (whatever factorial you are calculating
total multiplied by factor
increment factor
}```

20. Member
Join Date
Oct 2012
Posts
37
Rep Power
0

## Re: How to make ad factorial table? With and without BigInteger.

Thank a ton for all your help so far, I know I can be... difficult to work with. What does increment factor mean? Is that where you use that code? how do you plug total and factor into that? thanks.

Page 1 of 2 12 Last

#### Posting Permissions

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