Factorial

This is a discussion on Factorial within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Code: int Factorial(int n) { if(n == 1){ return 1; } else{ return n * Factorial(n -1); } } The ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Factorial

    Code:
    int Factorial(int n)
    {
         if(n == 1){
               return 1;
         }
               else{
                     return n * Factorial(n -1);
         }
    }
    The textbook asks, why might Factorial(0) return 0, and to be honest I have no clue. The call trace would look like:
    Facorial(0) = 0 * Factorial(-1)
    = 0* (-1) * Factorial(-2)...
    it would just go into an infinite regress.

    The only way I see it returning 0, if it runs out of resources, and somehow returns a 0, or grabs some piece of memory, which could have been a 0.

    Any other possibilities?

    Thanks
    Last edited by MethodMan; 09-30-2002 at 05:36 PM.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Originally posted by Salem
    Looks like the book and the code are both broken

    0! is 1 (not 0)

    And your analysis is correct - as written, that code is off into infinity (and beyond) when called with zero as a parameter.
    What do you mean by 0! is 1(not 0)

    and I had n + Fatorial
    should ahve been n * Factorial
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    > What do you mean by 0! is 1(not 0)
    It's in the definition of factorial
    http://www.fidcal.com/puzzlets/factorialZero.htm

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Yes, these guys are right.

    Just change this:
    if(n == 1)

    to
    if(n == 0)

    and you should get the correct results.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Eventually in 2's complement n will go back to 1. I think
    your stack will overflow by then though. There's always
    the remote possibility that the compiler will take out the recursion
    but most arn't smart enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  2. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 07:59 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 01:00 AM
  4. Basic Factorial from 1-10
    By AaA in forum C Programming
    Replies: 20
    Last Post: 05-28-2005, 08:39 AM
  5. factorial output
    By maloy in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 03:28 PM

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