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

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-05-2012, 07:16 PM
zhider
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.
• 11-05-2012, 07:35 PM
JosAH
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
• 11-05-2012, 07:37 PM
zhider
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?
• 11-05-2012, 07:41 PM
JosAH
Re: How to make ad factorial table? With and without BigInteger.
Quote:

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
• 11-05-2012, 07:50 PM
zhider
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.
• 11-05-2012, 08:24 PM
JosAH
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
• 11-06-2012, 01:56 AM
zhider
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?
• 11-06-2012, 02:01 AM
zhider
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?
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); }```
• 11-06-2012, 02:02 AM
Junky
Re: How to make ad factorial table? With and without BigInteger.
Quote:

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.
• 11-06-2012, 02:05 AM
Junky
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:
Code:

```loop {     // all printing done on one line     print n     print space     calculate factorial of n     print factorial }```
• 11-06-2012, 02:06 AM
Junky
Re: How to make ad factorial table? With and without BigInteger.
Quote:

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.
• 11-06-2012, 02:13 AM
zhider
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.
• 11-06-2012, 02:19 AM
Junky
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.
• 11-06-2012, 02:22 AM
zhider
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.
• 11-06-2012, 02:29 AM
Junky
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?
• 11-06-2012, 02:35 AM
zhider
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.
• 11-06-2012, 02:38 AM
zhider
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
• 11-06-2012, 02:48 AM
zhider
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?
• 11-06-2012, 02:51 AM
Junky
Re: How to make ad factorial table? With and without BigInteger.
Quote:

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.
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 }```
• 11-06-2012, 03:05 AM
zhider
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.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last