Thread: Please explain to me how this code work so it make sense to me.

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    Please explain to me how this code work so it make sense to me.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    
    
    double factorial ( double a )
    {
        if (a > 1 )
            return ( a * factorial (a -1) );
        else
            return (1);
    }
    
    
    int main()
    {
        double num;
        cout << "Enter a number: ";
        cin >> num;
    
    
        cout << num << "! = " << factorial(num);
    
    
        system("pause");
        return 0;
    }
    Now I know that the working for factorial is

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

    but I can't see how this is implemented on the code.

    Please help me to understand the code. Thanks.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Well, you have to realize that:
    Code:
    n! = n* (n-1)!
    and that the factorual of 1 is always 1.
    After that look at the factorial function, it'll be clear.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You are also aware that n! = n*(n-1)! if n is positive, and that both 0! and 1! are equal to 1?

    So, if wanting to compute 4! one might calculate 4*3!. That requires calculating 3!, which is 3*2!. That requires calculating 2!, which is 2*1!. 1! is 1.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    So, if wanting to compute 4! one might calculate 4*3!. That requires calculating 3!, which is 3*2!. That requires calculating 2!, which is 2*1!. 1! is 1.
    I understand your point, but I still can't understand how the function can work it out like that.

    This code is shown under the topic function (II), recursivity.

    Maybe you can explain to me about recursivity briefly to explain how this work?

    Thanks again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    double factorial0 ( double a ) {
        return 1;
    }
    double factorial1 ( double a ) {
        return 1 * factorial0();
    }
    double factorial2 ( double a ) {
        return 2 * factorial1();
    }
    double factorial3 ( double a ) {
        return 3 * factorial2();
    }
    double factorial4 ( double a ) {
        return 4 * factorial3();
    }
    It's like this, but without all the hassle of copy/paste/rename.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    And how does

    Code:
    double factorial0 ( double a ) {
        return 1;
    }
    double factorial1 ( double a ) {
        return 1 * factorial0();
    }
    double factorial2 ( double a ) {
        return 2 * factorial1();
    }
    double factorial3 ( double a ) {
        return 3 * factorial2();
    }
    double factorial4 ( double a ) {
        return 4 * factorial3();
    }
    
    


    be similar with this?

    Code:
    double factorial ( double a )
    {
        if (a > 1 )
            return ( a * factorial (a -1) );
        else
            return (1);
    }
    
    


    Sorry, after all that, I still need further explanation to understand.




  7. #7
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Ohh I get it now. So the work out s somewhat like..

    if i put a = 5,

    5 * ( factorial ( a -1 ))

    then

    5 * factorial ( 5 -1) same as a a* factorial ( a - 1)

    5 * factorial (4)

    then

    5 * ( 4 * factorial ( 3 ) )

    again [4 * factorial (3)] is same as [a*factorial(a-1)]

    so

    5 * 4 * ( 3 * factorial (2) )

    5 * 4 * 3 * ( factorial (2) )

    5* 4 * 3 * ( 2 * factorial ( 1 ) )

    5 * 4 * 3 * 2 * 1 = 120..

    Is my understanding correct?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, that's it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Alright thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-13-2010, 11:48 PM
  2. does it make any sense
    By ExDHaos in forum C++ Programming
    Replies: 0
    Last Post: 05-23-2009, 08:06 AM
  3. does this make sense ??
    By agarwaga in forum C++ Programming
    Replies: 14
    Last Post: 03-08-2006, 07:10 PM
  4. anyone make any sense of this
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 10-03-2001, 10:29 PM