Factorials

M-a-S NO-MAIL at hotmail.com
Mon Sep 22 17:15:55 EDT 2003


No difference on my computer (see my note at the end):

times:
recursion 6.66
reduction 3.50
iteration 3.16  (for i in range(2,n+1))
xteration 3.16  (for i in xrange(2,n+1))

But when I added another function (it'e a bit slower its range/xrange partners)

def factorial_niter(n):
  s = 1
  i = 2
  while i<=n:
    s *= i
    i += 1
  return s

ALL the times increased, probably because the grown dictionary.

bast times:
recursion 6.77
reduction 3.51
iteration 3.20
xteration 3.17
nteration 3.36

M-a-S


"Julian Tibble" <chasm at rift.sytes.net> wrote in message news:slrnbmrgau.438.chasm at galileo.rift...
> <...>
> (code I used:)
> def recursion(n):
>         s = 1
>         for i in range(2, n+1):
>                 s *= i
>         return s
>
> def xrecursion(n):
>         s = 1
>         for i in xrange(2, n+1):
>                 s *= 1
>         return s


s *= 1 <--------------------------------- maybe this is your difference!






More information about the Python-list mailing list