Recursive Factorial

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 32
Reputation: coveredinflies is an unknown quantity at this point 
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Light Poster

Recursive Factorial

 
0
  #1
Aug 15th, 2008
Hi, I am sure you have all seen the recursive factorial program.
  1. int factorial(int number) {
  2. int temp;
  3.  
  4. if(number <= 1) return 1;
  5.  
  6. temp = number * factorial(number - 1);
  7. return temp;
  8. }


" Once at 1, the values are returned to the previous factorial call and multiplied together."

Could someone explain this to me please, I know it works but when I first read the code I thought it would always return 1 since as I understand it as soon as a function gets to a return value it returns that number (or other type). Since you keep lowering number till you get to 1 I can't see how this doesn't always return 1.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Recursive Factorial

 
0
  #2
Aug 15th, 2008
Maybe it would be easier if you thought about the code like so:

  1. int factorial ( int number )
  2. {
  3. int temp;
  4.  
  5. if ( number <= 1 )
  6. {
  7. return 1;
  8. }
  9. else
  10. {
  11. temp = number * factorial ( number - 1 );
  12. return temp;
  13. }
  14. }
Last edited by iamthwee; Aug 15th, 2008 at 8:35 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Recursive Factorial

 
0
  #3
Aug 15th, 2008
Don't worry, simply form a habit to read (then invent) recursive algorithmes (apropos, never calculate factorial in this manner).
How about:
  1. int factorial(int n)
  2. {
  3. return n <= 1? 1: n*factorial(n-1);
  4. }
Simple and clear code (with guarantee of as usually undetected integer overflow)...
Last edited by ArkM; Aug 15th, 2008 at 11:30 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: scream2ice is an unknown quantity at this point 
Solved Threads: 0
scream2ice scream2ice is offline Offline
Light Poster

Re: Recursive Factorial

 
0
  #4
Aug 15th, 2008
why don't u trace for a number like 4?
of course the code u've come up with 'is' actually more complicated than a simple code for a factorial should be! but tracing it will help

  1. int factorial(int number) {
  2. int temp;
  3.  
  4. if(number <= 1) return 1;
  5.  
  6. temp = number * factorial(number - 1);
  7. return temp;
  8. }
  9. int factorial(int number) {
  10. int temp;
  11.  
  12. if(number <= 1) return 1;
  13.  
  14. temp = number * factorial(number - 1);
  15. return temp;
  16. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Recursive Factorial

 
0
  #5
Aug 15th, 2008
Yet another productive method: try to "unwind" solution from the END of recursion.
For example, in factorial case:

When the function knows the answer? Of course, if n <= 1 - factorial is equal to 1 by definition! OK, write the first statement:
  1. int factorial(int n)
  2. {
  3. if (n <= 1)
  4. return 1;
  5. // But what else?...
  6. }
Let's continue. Suppose we HAVE a function to calculate factorial for all small numbers < n.
  1. in factorial(int n)
  2. {
  3. if (n < 1)
  4. return 1; // OK, it's our point of rest...
  5. // We are on terra incognita now, but
  6. // let's remember school math...
  7. // Factorial definition helps:
  8. return factorial(n-1) * n;
  9. }
Stop. See how the land lies.

We are trying to call factorial for lesser than n number then more lesser and so on.
We are going to the bottom - of what? Where is this bottom?

Aha, come back to the 1st and 2nd line of the function body. That's a bottom! After that we will rise to the surface, back to starting recursion level, and - hurrah - we know factorial(n-1) - that's all, all done.

It looks like a trick or abstraction: we have no function yet but operate with its call(s) to build its body...

But it's the power of recursive solutions.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: scream2ice is an unknown quantity at this point 
Solved Threads: 0
scream2ice scream2ice is offline Offline
Light Poster

Re: Recursive Factorial

 
0
  #6
Aug 16th, 2008
with the recursive functions you should always have a stop point somewhere in ur function otherwise it will go on forever. Your stop point in this case is the "return 1" which means that you can only exit the program when you have reached the number 1 (because u've got the (n<=1) condition)---it is ur only way to end the recursion
'return 1' here means in fact 'return true'....try replacing it ...it still works
bcuz you are not actually returning the 'number' 1 but you are telling your compiler that it is now ok to exit the program....this is where i want my code to end
but do u kno why if we put 'return 0' instead of 'return 1', the output will be zero?
'return 0' can actually be equivalent to 'return false', so the recusion ends but a false end
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 32
Reputation: coveredinflies is an unknown quantity at this point 
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Light Poster

Re: Recursive Factorial

 
0
  #7
Aug 16th, 2008
Hey thanks for the replies, I totally wasn't getting it and didn't understand your first few posts then I was reading fifth post and it clicked.

the last return 1 returns to the previous function call not to the main function. thank you again I was getting really frustrated. so obvious in hindsight!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC