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.
Printable View
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.
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
That is true, but do you have any idea on how to actually code the table?
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.
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
The code that you put is giving alot of errors in Netbeans, would something like n= x*(x-1) work? any other ideas?
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);
}
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
}
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.
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.
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.
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?
I didnt miss the !, I got an error when i put it. ')' expected. and ill look into recursion, thanks.
And i was trying to test it with some numbers, and dont know how to set them in the actual methods
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?
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
}
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.