1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/shootout/recursive.py Sun Feb 18 01:39:11 2007 +0100
1.3 @@ -0,0 +1,35 @@
1.4 +# The Computer Language Shootout
1.5 +# http://shootout.alioth.debian.org/
1.6 +# based on bearophile's psyco program
1.7 +# slightly modified by Isaac Gouy
1.8 +
1.9 +def Ack(x, y):
1.10 + if x == 0: return y+1
1.11 + if y == 0: return Ack(x-1, 1)
1.12 + return Ack(x-1, Ack(x, y-1))
1.13 +
1.14 +def Fib(n):
1.15 + if n < 2: return 1
1.16 + return Fib(n-2) + Fib(n-1)
1.17 +
1.18 +def FibFP(n):
1.19 + if n < 2.0: return 1.0
1.20 + return FibFP(n-2.0) + FibFP(n-1.0)
1.21 +
1.22 +def Tak(x, y, z):
1.23 + if y < x: return Tak( Tak(x-1,y,z), Tak(y-1,z,x), Tak(z-1,x,y) )
1.24 + return z
1.25 +
1.26 +def TakFP(x, y, z):
1.27 + if y < x: return TakFP( TakFP(x-1.0,y,z), TakFP(y-1.0,z,x), TakFP(z-1.0,x,y) )
1.28 + return z
1.29 +
1.30 +from sys import argv, setrecursionlimit
1.31 +setrecursionlimit(20000)
1.32 +
1.33 +n = int(argv[1]) - 1
1.34 +print "Ack(3,%d):" % (n+1), Ack(3, n+1)
1.35 +print "Fib(%.1f): %.1f" % (28.0+n, FibFP(28.0+n))
1.36 +print "Tak(%d,%d,%d): %d" % (3*n, 2*n, n, Tak(3*n, 2*n, n))
1.37 +print "Fib(3):", Fib(3)
1.38 +print "Tak(3.0,2.0,1.0):", TakFP(3.0, 2.0, 1.0)