1

Possible Duplicate:
Function for Factorial in Python

I need to write a program in python that returns N!, without using the factorial function. I have a program written so far but I keep getting an error saying, local variable "fact" is assigned but never used. How do I use fact = 1, after it is assigned?

from pylab import *  


def factorial(n):
    fact = 1

for i in range(n):
    print("i = ", i)
    fact = fact * i

print("The factorial of " + str(n) + " is: " + str(fact))
Community
  • 1
  • 1
Bill
  • 275
  • 2
  • 5
  • 9
  • Is the indentation in your post the same as it is in your file? Try indenting the whole `for` block to be under the function definition. – aganders3 Oct 10 '12 at 02:18

2 Answers2

5
In [37]: def fact(n):
    fac=1
    for i in range(2,n+1):
        fac *=i
    return fac
   ....: 


In [43]: fact(5)
Out[43]: 120

In [44]: fact(6)
Out[44]: 720
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Thanks alot!, this program ran the way I needed it to. Can you explain what the (2,n+1) does/mean in the the "for" loop? – Bill Oct 10 '12 at 19:49
  • @Bill lets say n is 5, then the range will return the values `2,3,4,5` one by one with each iteration, and we multiply them with `fac` in each iteration. `range(2,6)=[2, 3, 4, 5]` – Ashwini Chaudhary Oct 10 '12 at 19:57
1

I know very little of python, but you should use recursion in these example. It's very simple. Recursion is a function that calls itself

def factorial(n):
    if n== 0 or n == 1:
        return 1
    else:
        return n * factorial(n-1)
jotape
  • 319
  • 2
  • 6
  • 14
  • 1
    You will run out the stack in Python pretty easily this way. – aganders3 Oct 10 '12 at 02:17
  • Why? (Learning everyday) – jotape Oct 10 '12 at 02:21
  • Well, "pretty easily" is relative, but try running your code with `n = 1000` for instance. Each iteration through the function will call the function again, adding a level to the execution stack. If you put in a big number, this stack will get very high before it gets to the escape case (`if n==0 or n==1`). Python does not optimize this (called tail recursion) so it has a finite recursion depth. – aganders3 Oct 10 '12 at 02:25
  • Python will error out when you get a stack deeper than `sys.getrecursionlimit()` (set to 1000 on my system). Your solution would also be problematic in languages that encourage recursion though, since you can't apply tail call optimisation. – James Henstridge Oct 10 '12 at 02:26
  • This is a great learning example for recursion (and factorials), though! – aganders3 Oct 10 '12 at 02:27
  • Ok, tell me if I understood. When the function calls itself; like 1000 times; all that code (multiplied by 1000) is running at the stack at the same time? – jotape Oct 10 '12 at 02:39
  • 2
    @jotape: each time you call a function, a stack frame is allocated to hold local variables, a pointer to the calling stack frame, etc. So uncontrolled recursion could eat up all available memory. That isn't quite what happens in Python though, since it imposes an artificial limit. Some other languages include "tail call optimisation" though, which notes that if a function returns the result of another function call, then its stack frame won't be needed any more: the other function call can return directly to the grandparent stack frame instead. – James Henstridge Oct 10 '12 at 03:13
  • 1
    It wouldn't apply to your answer though, since you're modifying the result of the function call: the multiplication requires that the stack frame stick around the whole time. – James Henstridge Oct 10 '12 at 03:14