Ackermann is now encapsulated in a generator.

This commit is contained in:
Cédric Bonhomme 2019-06-01 00:58:00 +02:00
parent 1704aec690
commit 5694e5c806
No known key found for this signature in database
GPG key ID: A1CB94DE57B7A70D
2 changed files with 21 additions and 2 deletions

View file

@ -118,14 +118,14 @@ def ackermann_naive(m: int, n: int) -> int:
return ackermann(m - 1, ackermann(m, n - 1))
def ackermann(m: int, n: int) -> int:
def ackermann_fast(m: int, n: int) -> int:
"""Ackermann number.
"""
while m >= 4:
if n == 0:
n = 1
else:
n = ackermann(m, n - 1)
n = ackermann_fast(m, n - 1)
m -= 1
if m == 3:
return (1 << n + 3) - 3
@ -136,6 +136,13 @@ def ackermann(m: int, n: int) -> int:
else:
return n + 1
def ackermann(m: int) -> Iterator[int]:
"""Ackermann written as a generator.
"""
n = 0
while True:
yield ackermann_fast(m, n)
n += 1
def fibonacci() -> Iterator[int]:
"""Generate the sequence of Fibonacci.