n!

This is a discussion on n! within the C++ Programming forums, part of the General Programming Boards category; can someone get me started on calculating n! I think i should use a for loop....

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    Smile n!

    can someone get me started on calculating n! I think i should use a for loop.

  2. #2
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    where is the variable n used?

    you need an equation

    like
    5 = 2 + n

    or
    2(n) + 12938109283109283 = 3094809384503985

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Hehe I think the Wooike misunderstood the question!

    n! = 1 * 2 * 3 * ... * n

    If you know how to write for-loops this should be a piece of cake.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    By definition n factorial is...

    n! = n * (n - 1) * (n - 2) * ... 1

    The one at the end comes from the base case of 0! which is equal to 1. Have fun.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    i think you mean n factorial. probably one of the easiest ways is to use a recursive function...try it, then post your code...

    it shouldn't be too long...
    Last edited by alpha; 11-19-2002 at 04:32 PM.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    n!

    #include <iostream.h>
    #include <stdio.h>

    unsigned int f,x=0;

    unsigned int factorial(unsigned int a);

    int main(void)
    {
    int hold;
    puts("Please enter an integer value: ");
    scanf("%d", &x);

    {2
    f=factorial(x);
    printf("%u factorial equals %u\n\n", x,f);
    }
    cin >> hold;
    return 0;
    }

    unsigned int factorial(unsigned int a)
    {
    if(a==1)
    {
    return 1;
    }

    else
    {
    a *=factorial(a-1);
    }

    return a;
    }

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by alpha
    i think you mean n factorial. probably one of the easiest ways is to use a recursive function...try it, then post your code...

    it shouldn't be too long...
    You don't need a recursive function for that..

  8. #8
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    Originally posted by Sang-drax
    Hehe I think the Wooike misunderstood the question!

    n! = 1 * 2 * 3 * ... * n

    If you know how to write for-loops this should be a piece of cake.
    oooooooooo woops,

    i thought he meant like

    "help me!"


  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Captain Penguin
    You don't need a recursive function for that..
    I know you don't need a recursive function for it, I think its a cooler way of doing it though...and possibly shorter, im not sure because i haven't written factorial using a for loop since a few months ago...

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by MrWizard
    By definition n factorial is...

    n! = n * (n - 1) * (n - 2) * ... 1

    The one at the end comes from the base case of 0! which is equal to 1. Have fun.
    For fun, here's the definition of z! when z is a real or complex number:

    When z is a negative integer, z! = infinity
    Attached Images Attached Images  
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by alpha
    I know you don't need a recursive function for it, I think its a cooler way of doing it though...and possibly shorter, im not sure because i haven't written factorial using a for loop since a few months ago...
    cooler? who cares, its sloooooooooooow.
    hello, internet!

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Originally posted by Sang-drax
    For fun, here's the definition of z! when z is a real or complex number:

    When z is a negative integer, z! = infinity
    ewwwwww...

    can you convert that to c code so i can understand it?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by moi
    cooler? who cares, its sloooooooooooow.
    Yep, this is more than sufficient:

    Code:
    int main()
    {
      int n;
      int ans = 1;
    
      cin >> n;
      for(int x = n; x > 0; x--)
      {
        ans = ans * x;
      }
      cout << ans;
    }

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    okay, i didn't know it was that much slower. i recently just kindof taught myself recursion, so I like using it for the time being...I'll get over the phase, hehe...

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21