Iteration for Factorials
auzaar at gmail.com
auzaar at gmail.com
Tue Oct 30 08:11:07 EDT 2007
On Oct 30, 3:30 pm, Nick Craig-Wood <n... at craig-wood.com> wrote:
> Anurag <anuraguni... at gmail.com> wrote:
> > What about this no map, reduce, mutiplication or even addition
> > Its truly interative and You will need to interate till infinity if
> > you want correct answer ;)
>
> > deffactorial(N):
> > """
> > Increase I ...and go on increasing...
> > """
> > import random
>
> > myNumer = range(N)
> > count = 0
> > I = 10000 #10**(N+1)
> > for i in range(I):
> > bucket = range(N)
> > number = []
> > for i in range(N):
> > k = bucket[ random.randrange(0,len(bucket))]
> > bucket.remove(k)
> > number.append(k)
>
> > if number == myNumer:
> > count+=1
>
> > return int(I*1.0/count+.5)
>
> ;-)
>
> Note you can write your middle loop as
>
> for i in range(I):
> number = myNumer[:]
> random.shuffle(number)
> if number == myNumer:
> count+=1
>
> --
> Nick Craig-Wood <n... at craig-wood.com> --http://www.craig-wood.com/nick- Hide quoted text -
>
> - Show quoted text -
good :) i thinkif go on improving this we will have worlds' best
useless factorial function.
actually number = myNumer[:] can be moved out of the loop.
More information about the Python-list
mailing list