<div class="gmail_quote">On Tue, Oct 19, 2010 at 4:02 PM, Matthew Nunes <span dir="ltr"><<a href="mailto:matthewnunes@hotmail.com">matthewnunes@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div>
To whom it may concern, <br>
<br>
Hi, I've just started learning how to program in python using Allan B. Downy's book "How to think like a computer scientist" and it explained something in the recursion chapter which have still been unable to understand. It wrote a piece of code for the factorial function in math for example 3! is 3 * 2 * 1. I cannot understand the how it claimed the code executed, and logically it makes no sense to me. Please explain it to me, as I have spent hours trying to get my head around it, as the book(in my opinion) did not explain it well. Here it is: </div>
</blockquote><div><br></div><div>That's a pretty solid book. This example uses recursion - for a great example of recursion, search Google for recursion</div><div> </div><div>But we can examine the code:</div><div><br>
</div><div>This is a function that takes one parameter named n</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><font size="2" face="Courier"><font size="2" face="Courier"><p align="left">
def factorial(n):</p></font></font></div></blockquote><div>If n is 0 then return 1 (since 0! = 1)</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><font size="2" face="Courier"><font size="2" face="Courier">
<p align="left"> if n == 0:</p>
<p align="left"> return 1</p>
<p align="left"> else:</p></font></font></div></blockquote><div>If n > 0 then the definition of a factorial is n * factorial(n-1) - which we do here in two steps </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><font size="2" face="Courier"><font size="2" face="Courier">
<p align="left"> recurse = factorial(n-1)</p>
<p align="left"> result = n * recurse</p></font></font></div></blockquote><div>And then we return the result</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><font size="2" face="Courier"><font size="2" face="Courier"> return result<br>
<br>
<font face="Tahoma">If there is and easier piece of code that you know of that can be used for factorial, if you could please also tell me that.</font></font></font></div></blockquote><div><br></div><div>This is probably the very easiest recursive example to get your head around because the definition of factorial is recursive. However, it's a *terribly* inefficient way to compute the factorial of a number.</div>
<div><br></div><div>The most efficient way for something like 3 is this:</div><div><br></div><div>import math</div><div>math.factorial(3)</div><div><br></div><div>Using the built-in functions are always better.</div><div>
<br></div><div>However, if you're wanting a code example you can do this one:</div><div><br></div><div>def factorial(n):</div><div> if n == 0:</div><div> n = 1</div><div> fact = 1</div><div> for x in xrange(1, n+1):</div>
<div> fact = fact * x # This could be replaced by fact *= x</div><div> return fact</div><div><br></div><div>Or you could do something a little more advanced:</div><div><br></div><div>def factorial(n):</div>
<div> if n == 0:</div><div> n = 1</div><div> return reduce(lambda x,y: x*y, xrange(1,n+1))</div><div><br></div><div>But that's probably a little beyond your comprehension level at this point.</div><div><br>
</div><div>HTH,</div><div>Wayne</div><div> </div><div><br></div><div><br></div><div><br></div></div>