"""This module contains code from Think Python by Allen B. Downey http://thinkpython.com Copyright 2012 Allen B. Downey License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html """ cache = {} def ackermann(m, n): """Computes the Ackermann function A(m, n) See http://en.wikipedia.org/wiki/Ackermann_function n, m: non-negative integers """ if m == 0: return n+1 if n == 0: return ackermann(m-1, 1) try: return cache[m, n] except KeyError: cache[m, n] = ackermann(m-1, ackermann(m, n-1)) return cache[m, n] print ackermann(3, 4) print ackermann(3, 6)