uhmm but that int--return type might be a problem in this case...:)
No denying that it is also the best way to solve such kind of problem though..:)
Printable View
uhmm but that int--return type might be a problem in this case...:)
No denying that it is also the best way to solve such kind of problem though..:)
You said:Quote:
Originally posted by filthy_mcnasty
you're entirely right. sorry for trying to help or adding some input.
i didn't say DONT USE ANY OTHER METHOD, USE THIS! i simply offered another possibility.
That looks to me like a recommendation.Quote:
Originally posted by filthy_mcnasty
if you ask me, recursion is the way to go on this.
I just did a test and found that my solution works upto 65!, so the trick for numbers larger would be your own "sciencetific notation" system :) For example, you could make your result a double and multiply be single digit numbers only.
I just tried a double version of the same function and it works for numbers upto 177!. I didn't realize that double was so much bigger :)
I changed the short to unsigned as previously suggested so that the user can't try an negative factorial.Code:double factorial( unsigned short n )
{
double result = 1.0f;
for( short i = 1;i <= n;i++ )
result *= i;
return result;
}
It doesn't work for 0 factorial (the fix is easy, of course).Quote:
Originally posted by mwilliamson
I changed the short to unsigned as previously suggested so that the user can't try an negative factorial.
Also, google for "Stirling's formula". This gives an approximation of n! for large values of n.
Regards,
Paul McKenzie
yes, a recommendation. that doesn't mean i said "this is the best, dont use anything else". an IDEA.
There is no zero factorial... I know my code will return 1 for 0!, and thats what my calculator returns, so I don't see a problem.Quote:
Originally posted by Paul McKenzie
It doesn't work for 0 factorial (the fix is easy, of course).
Also, google for "Stirling's formula". This gives an approximation of n! for large values of n.
Regards,
Paul McKenzie
OK, I see that 1 is returned if the loop isn't entered.
BTW, there is a 0! and it is by definition, 1.
Regards,
Paul McKenzie
In fact the factorial is defined for all complex numbers except the negative integers
If you want to keep being technical on the definition of a factorial, it really isn't defined for all complex numbers, as it is not defined for real numbers that are not nonnegative integers (for example, 3.5! is not defined). :)
3.5! = (sqrt(PI)/16)(105)
I never heard about a complex factorial.Quote:
Originally posted by souldog
In fact the factorial is defined for all complex numbers except the negative integers
Can you provide a definition? for example (a+bi)! with b!=0?
Yeah, I learned this in my mathematical statistics class. The factorial of an integer is just a generalization of the Gamma function, and Gamma(0.5) == sqrt(pi).Quote:
Originally posted by souldog
3.5! = (sqrt(PI)/16)(105)
Regards,
Paul McKenzie
I thought he was full of s*, too (no offense, man), so I googled and found,
http://mathworld.wolfram.com/Factorial.html
Check out (7).
AAAIAIAIAGH!!! INTEGRALS SCARY!!!! AIGH!!
Peace,
Bassman
Yes. The factorial isn't as trivial as it sounds. It has its roots in the Gamma function, and that is what souldog was pointing out.Quote:
Originally posted by Bassman
I thought he was full of s*, too (no offense, man), so I googled and found,
Regards,
Paul McKenzie
Some Stuff About Factorial
oops you had already found the same link I did