Iteration for Factorials

Paul McGuire ptmcg at austin.rr.com
Mon Oct 22 13:20:55 EDT 2007


On Oct 22, 8:02 am, vimal <cool.vimalsm... at gmail.com> wrote:
> On Oct 22, 5:43 pm, Marco Mariani <ma... at sferacarta.com> wrote:
>
> > Py-Fun wrote:
> > > def itforfact(n):
> > >     while n<100:
> > >         print n
> > >         n+1
> > > n = input("Please enter a number below 100")
>
> > You function should probably return something. After that, you can see
> > what happens with the result you get.
>
> i am just suggesting u an idea
> but i dont know it satisfies ur needs
>
> x=10
> def cal_range(10)
>     for i in range(10):
>         print 2**i

Maybe a little math refresher would be good for those trying to post
suggestions.

"Factorial of N" means "the product of 1 x 2 x 3 x ... x N", and is
shown symbolically as "N!".

(Factorial is only defined for nonnegative numbers, and for reasons
that go beyond this little note, just know that 0! = 1.)

In Python, a fully expanded factorial for values >= 1 would be:

2! = 1 * 2
5! = 1 * 2 * 3 * 4 * 5
8! = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8

Here is an example routine showing iteration, that prints these
expressions:


def print_factorial(n):
    print str(n)+"! =",
    print "1",
    t = 2
    while t <= n:
        print "*",t,
        t += 1
    print

Perhaps this example will give you some ideas on how to approach your
problem.


-- Paul




More information about the Python-list mailing list