simplify

Annotated tests/shootout/recursive.py

248:6e7b6fcd6302
2007-06-23 paulb Introduced a separate fix_structures method to the instance fixing class. Made new dictionaries when fixing references to instances via accesses/writes. Added recursion detection around getting distinct instances from classes.
paulb@200 1
# The Computer Language Shootout
paulb@200 2
# http://shootout.alioth.debian.org/
paulb@200 3
# based on bearophile's psyco program
paulb@200 4
# slightly modified by Isaac Gouy
paulb@200 5
paulb@200 6
def Ack(x, y):
paulb@200 7
    if x == 0: return y+1
paulb@200 8
    if y == 0: return Ack(x-1, 1)
paulb@200 9
    return Ack(x-1, Ack(x, y-1))
paulb@200 10
paulb@200 11
def Fib(n):
paulb@200 12
    if n < 2: return 1
paulb@200 13
    return Fib(n-2) + Fib(n-1)
paulb@200 14
paulb@200 15
def FibFP(n):
paulb@200 16
    if n < 2.0: return 1.0
paulb@200 17
    return FibFP(n-2.0) + FibFP(n-1.0)
paulb@200 18
paulb@200 19
def Tak(x, y, z):
paulb@200 20
    if y < x: return Tak( Tak(x-1,y,z), Tak(y-1,z,x), Tak(z-1,x,y) )
paulb@200 21
    return z
paulb@200 22
paulb@200 23
def TakFP(x, y, z):
paulb@200 24
    if y < x: return TakFP( TakFP(x-1.0,y,z), TakFP(y-1.0,z,x), TakFP(z-1.0,x,y) )
paulb@200 25
    return z
paulb@200 26
paulb@200 27
from sys import argv, setrecursionlimit
paulb@200 28
setrecursionlimit(20000)
paulb@200 29
paulb@200 30
n = int(argv[1]) - 1
paulb@200 31
print "Ack(3,%d):" % (n+1), Ack(3, n+1)
paulb@200 32
print "Fib(%.1f): %.1f" % (28.0+n, FibFP(28.0+n))
paulb@200 33
print "Tak(%d,%d,%d): %d" % (3*n, 2*n, n, Tak(3*n, 2*n, n))
paulb@200 34
print "Fib(3):", Fib(3)
paulb@200 35
print "Tak(3.0,2.0,1.0):", TakFP(3.0, 2.0, 1.0)