Skip to content

Instantly share code, notes, and snippets.

@multun
Created September 9, 2020 09:40
Show Gist options
  • Save multun/f7b8bc3e6e679345a404657140b65042 to your computer and use it in GitHub Desktop.
Save multun/f7b8bc3e6e679345a404657140b65042 to your computer and use it in GitHub Desktop.
Fast ackermann
from functools import lru_cache
@lru_cache(maxsize=1024)
def kn(a, b, order):
assert order >= 1
if order == 1:
return a ** b
sub_order = order - 1
res = a
for i in range(order):
res = kn(res, b, sub_order)
return res
def conway(p, q, r):
return kn(p, q, r)
def ackermann(m, n):
return conway(2, n + 3, m - 2) - 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment