Velocity Reviews > Iteration for Factorials

# Iteration for Factorials

Py-Fun
Guest
Posts: n/a

 10-22-2007
I'm stuck trying to write a function that generates a factorial of a
number using iteration and not recursion. Any simple ideas would be
appreciated.

Diez B. Roggisch
Guest
Posts: n/a

 10-22-2007
Py-Fun wrote:

> I'm stuck trying to write a function that generates a factorial of a
> number using iteration and not recursion. Any simple ideas would be
> appreciated.

Show us your attempts, and we might suggest a fix. Because otherwise this
sounds suspiciously like homework.

Diez

Py-Fun
Guest
Posts: n/a

 10-22-2007
On 22 Oct, 13:28, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> Py-Fun wrote:
> > I'm stuck trying to write a function that generates a factorial of a
> > number using iteration and not recursion. Any simple ideas would be
> > appreciated.

>
> Show us your attempts, and we might suggest a fix. Because otherwise this
> sounds suspiciously like homework.
>
> Diez

Here is my futile attempt. Be careful with this though, I just ran
something similar and it was never ending...

def itforfact(n):
while n<100:
print n
n+1
n = input("Please enter a number below 100")

itforfact(n)

Marco Mariani
Guest
Posts: n/a

 10-22-2007
Py-Fun wrote:

> I'm stuck trying to write a function that generates a factorial of a
> number using iteration and not recursion. Any simple ideas would be
> appreciated.

As opposed to what, a complicated one?

Marco Mariani
Guest
Posts: n/a

 10-22-2007
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.

Duncan Booth
Guest
Posts: n/a

 10-22-2007
Py-Fun <(E-Mail Removed)> wrote:

> I'm stuck trying to write a function that generates a factorial of a
> number using iteration and not recursion. Any simple ideas would be
> appreciated.
>

This version avoids doing anything fancier than adding 1, so it should be
simple enough for anyone:

def factorial(e):
a = 1
for b in range(e):
c = 0
for j in range(b, -1, -1):
for d in range(a):
c += 1
a = c
return a

Py-Fun
Guest
Posts: n/a

 10-22-2007
On 22 Oct, 13:43, Marco Mariani <(E-Mail Removed)> 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.

Marco, Thanks for the tip. This now works:

def itforfact(n):
while n<100:
print n
n = n+1
n = input("Please enter a number below 100")

itforfact(n)

Is it a "factorial" though?

cokofreedom@gmail.com
Guest
Posts: n/a

 10-22-2007
On Oct 22, 2:43 pm, Marco Mariani <(E-Mail Removed)> 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.

lambda n: n<=0 or reduce(lambda a,b: long(a)*long(b),xrange(1,n+1))

Amit Khemka
Guest
Posts: n/a

 10-22-2007
On 10/22/07, Py-Fun <(E-Mail Removed)> wrote:
> On 22 Oct, 13:28, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> > Py-Fun wrote:
> > > I'm stuck trying to write a function that generates a factorial of a
> > > number using iteration and not recursion. Any simple ideas would be
> > > appreciated.

> >
> > Show us your attempts, and we might suggest a fix. Because otherwise this
> > sounds suspiciously like homework.
> >
> > Diez

>
> Here is my futile attempt. Be careful with this though, I just ran
> something similar and it was never ending...
>
> def itforfact(n):
> while n<100:
> print n
> n+1
> n = input("Please enter a number below 100")
>
> itforfact(n)

Let me give you a pseudo code (which though can be found in most of
the textbooks and some 'simple' googling). Try to understand the code
and then write an equivalent python function.

function iFactorial(n: integer): integer;
var i, temp: integer;
begin
temp = 1;
for i = 1 to n do
temp = temp * i
end
return temp

1. why doesn't it surprise you if the code that you posted goes in
infinite loop ?!
2. why do you use condition: n < 100
3. How do you think that your function will calculate the factorial ?
4. Instead of "input" use "raw_input", and then "cast" the input as integer .

Cheers,
amit.
--
--
Amit Khemka

vimal
Guest
Posts: n/a

 10-22-2007
On Oct 22, 5:43 pm, Marco Mariani <(E-Mail Removed)> 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