-
June 4th, 2003, 04:22 AM
#16
Oh
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..
-
June 4th, 2003, 04:33 AM
#17
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.
You said:
Originally posted by filthy_mcnasty
if you ask me, recursion is the way to go on this.
That looks to me like a recommendation.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 4th, 2003, 08:54 AM
#18
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
Code:
double factorial( unsigned short n )
{
double result = 1.0f;
for( short i = 1;i <= n;i++ )
result *= i;
return result;
}
I changed the short to unsigned as previously suggested so that the user can't try an negative factorial.
-
June 4th, 2003, 10:37 AM
#19
Originally posted by mwilliamson
I changed the short to unsigned as previously suggested so that the user can't try an negative factorial.
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
Last edited by Paul McKenzie; June 4th, 2003 at 10:41 AM.
-
June 4th, 2003, 03:57 PM
#20
yes, a recommendation. that doesn't mean i said "this is the best, dont use anything else". an IDEA.
-
June 4th, 2003, 05:13 PM
#21
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
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.
-
June 4th, 2003, 05:36 PM
#22
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
-
June 4th, 2003, 10:47 PM
#23
In fact the factorial is defined for all complex numbers except the negative integers
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
June 5th, 2003, 10:15 AM
#24
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).
-
June 5th, 2003, 11:00 AM
#25
3.5! = (sqrt(PI)/16)(105)
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
June 5th, 2003, 11:50 AM
#26
Originally posted by souldog
In fact the factorial is defined for all complex numbers except the negative integers
I never heard about a complex factorial.
Can you provide a definition? for example (a+bi)! with b!=0?
-
June 5th, 2003, 11:56 AM
#27
Originally posted by souldog
3.5! = (sqrt(PI)/16)(105)
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).
Regards,
Paul McKenzie
-
June 5th, 2003, 12:02 PM
#28
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
-
June 5th, 2003, 12:09 PM
#29
Originally posted by Bassman
I thought he was full of s*, too (no offense, man), so I googled and found,
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.
Regards,
Paul McKenzie
-
June 5th, 2003, 12:09 PM
#30
Some Stuff About Factorial
oops you had already found the same link I did
Last edited by souldog; June 5th, 2003 at 01:07 PM.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|