danbaron
16-06-2010, 21:39
[font=courier new][size=8pt]Here is a program that does exactly the same thing as the thinBasic script, here:
http://community.thinbasic.com/index.php?topic=3403.0
Only, this one is written in Python 3.
(Python 3.x is not necessarily backwardly compatible with Python 2.x.)
:oops:
Dan :x :P
#------------------------------------------------------
def ack1(m, n):
if (m < 0) or (n < 0):
return -1
if m == 0:
return n + 1
if n == 0:
return ack1(m - 1, 1)
return ack1(m - 1, ack1(m, n - 1))
#------------------------------------------------------
def ack2(m, n):
s = []
if (m < 0) or (n < 0):
return -1
while 1:
if m == 0:
if len(s) == 0:
return n + 1
else:
m = s.pop(0)
n += 1
continue
if n == 0:
m -= 1
n = 1
continue
s.insert(0, m - 1)
n -= 1
#------------------------------------------------------
print(" ack1 ack2")
print()
for i in range(0, 4):
for j in range(0, 8):
print('ack(%1d, %1d) = %04d %04d' % (i, j, ack1(i, j), ack2(i, j)))
print('ack(%1d, %1d) = %04d %04d' % (4, 0, ack1(4, 0), ack2(4, 0)))
print()
print("Done.")
print("Press a key.")
input()
#------------------------------------------------------
http://community.thinbasic.com/index.php?topic=3403.0
Only, this one is written in Python 3.
(Python 3.x is not necessarily backwardly compatible with Python 2.x.)
:oops:
Dan :x :P
#------------------------------------------------------
def ack1(m, n):
if (m < 0) or (n < 0):
return -1
if m == 0:
return n + 1
if n == 0:
return ack1(m - 1, 1)
return ack1(m - 1, ack1(m, n - 1))
#------------------------------------------------------
def ack2(m, n):
s = []
if (m < 0) or (n < 0):
return -1
while 1:
if m == 0:
if len(s) == 0:
return n + 1
else:
m = s.pop(0)
n += 1
continue
if n == 0:
m -= 1
n = 1
continue
s.insert(0, m - 1)
n -= 1
#------------------------------------------------------
print(" ack1 ack2")
print()
for i in range(0, 4):
for j in range(0, 8):
print('ack(%1d, %1d) = %04d %04d' % (i, j, ack1(i, j), ack2(i, j)))
print('ack(%1d, %1d) = %04d %04d' % (4, 0, ack1(4, 0), ack2(4, 0)))
print()
print("Done.")
print("Press a key.")
input()
#------------------------------------------------------