CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 41

Thread: n factorial

  1. #16
    Join Date
    Jun 2003
    Location
    small hill
    Posts
    6

    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..

  2. #17
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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


  3. #18
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    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.

  4. #19
    Join Date
    Apr 1999
    Posts
    27,449
    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.

  5. #20
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    yes, a recommendation. that doesn't mean i said "this is the best, dont use anything else". an IDEA.

  6. #21
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    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.

  7. #22
    Join Date
    Apr 1999
    Posts
    27,449
    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

  8. #23
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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."

  9. #24
    Join Date
    Jan 2001
    Posts
    588
    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).

  10. #25
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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."

  11. #26
    Join Date
    Nov 2001
    Location
    Beyond Juslibol
    Posts
    1,688
    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?

  12. #27
    Join Date
    Apr 1999
    Posts
    27,449
    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

  13. #28
    Join Date
    Aug 2002
    Posts
    58
    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

  14. #29
    Join Date
    Apr 1999
    Posts
    27,449
    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

  15. #30
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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."

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured