1 # The Computer Language Shootout 2 # http://shootout.alioth.debian.org/ 3 # based on bearophile's psyco program 4 # slightly modified by Isaac Gouy 5 6 def Ack(x, y): 7 if x == 0: return y+1 8 if y == 0: return Ack(x-1, 1) 9 return Ack(x-1, Ack(x, y-1)) 10 11 def Fib(n): 12 if n < 2: return 1 13 return Fib(n-2) + Fib(n-1) 14 15 def FibFP(n): 16 if n < 2.0: return 1.0 17 return FibFP(n-2.0) + FibFP(n-1.0) 18 19 def Tak(x, y, z): 20 if y < x: return Tak( Tak(x-1,y,z), Tak(y-1,z,x), Tak(z-1,x,y) ) 21 return z 22 23 def TakFP(x, y, z): 24 if y < x: return TakFP( TakFP(x-1.0,y,z), TakFP(y-1.0,z,x), TakFP(z-1.0,x,y) ) 25 return z 26 27 from sys import argv, setrecursionlimit 28 setrecursionlimit(20000) 29 30 n = int(argv[1]) - 1 31 print "Ack(3,%d):" % (n+1), Ack(3, n+1) 32 print "Fib(%.1f): %.1f" % (28.0+n, FibFP(28.0+n)) 33 print "Tak(%d,%d,%d): %d" % (3*n, 2*n, n, Tak(3*n, 2*n, n)) 34 print "Fib(3):", Fib(3) 35 print "Tak(3.0,2.0,1.0):", TakFP(3.0, 2.0, 1.0)