Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96168
License: OTHER
1
"""This module contains a code example related to
2
3
Think Python, 2nd Edition
4
by Allen Downey
5
http://thinkpython2.com
6
7
Copyright 2015 Allen Downey
8
9
License: http://creativecommons.org/licenses/by/4.0/
10
"""
11
12
from __future__ import print_function, division
13
14
cache = {}
15
16
def ackermann(m, n):
17
"""Computes the Ackermann function A(m, n)
18
19
See http://en.wikipedia.org/wiki/Ackermann_function
20
21
n, m: non-negative integers
22
"""
23
if m == 0:
24
return n+1
25
if n == 0:
26
return ackermann(m-1, 1)
27
28
if (m, n) in cache:
29
return cache[m, n]
30
else:
31
cache[m, n] = ackermann(m-1, ackermann(m, n-1))
32
return cache[m, n]
33
34
35
print(ackermann(3, 4))
36
print(ackermann(3, 6))
37
38