Ackermann function: Difference between revisions
Content added Content deleted
No edit summary |
(Add improved Seed7 implementation) |
||
Line 8,502: | Line 8,502: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
===Basic version=== |
|||
<syntaxhighlight lang="seed7">const func integer: ackermann (in integer: m, in integer: n) is func |
<syntaxhighlight lang="seed7">const func integer: ackermann (in integer: m, in integer: n) is func |
||
result |
result |
||
Line 8,515: | Line 8,516: | ||
end func;</syntaxhighlight> |
end func;</syntaxhighlight> |
||
Original source: [http://seed7.sourceforge.net/algorith/math.htm#ackermann] |
Original source: [http://seed7.sourceforge.net/algorith/math.htm#ackermann] |
||
===Improved version=== |
|||
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
|||
include "bigint.s7i"; |
|||
const func bigInteger: ackermann (in bigInteger: m, in bigInteger: n) is func |
|||
result |
|||
var bigInteger: ackermann is 0_; |
|||
begin |
|||
case m of |
|||
when {0_}: ackermann := succ(n); |
|||
when {1_}: ackermann := n + 2_; |
|||
when {2_}: ackermann := 3_ + 2_ * n; |
|||
when {3_}: ackermann := 5_ + 8_ * pred(2_ ** ord(n)); |
|||
otherwise: |
|||
if n = 0_ then |
|||
ackermann := ackermann(pred(m), 1_); |
|||
else |
|||
ackermann := ackermann(pred(m), ackermann(m, pred(n))); |
|||
end if; |
|||
end case; |
|||
end func; |
|||
const proc: main is func |
|||
local |
|||
var bigInteger: m is 0_; |
|||
var bigInteger: n is 0_; |
|||
var string: stri is ""; |
|||
begin |
|||
for m range 0_ to 3_ do |
|||
for n range 0_ to 9_ do |
|||
writeln("A(" <& m <& ", " <& n <& ") = " <& ackermann(m, n)); |
|||
end for; |
|||
end for; |
|||
writeln("A(4, 0) = " <& ackermann(4_, 0_)); |
|||
writeln("A(4, 1) = " <& ackermann(4_, 1_)); |
|||
stri := str(ackermann(4_, 2_)); |
|||
writeln("A(4, 2) = (" <& length(stri) <& " digits)"); |
|||
writeln(stri[1 len 80]); |
|||
writeln("..."); |
|||
writeln(stri[length(stri) - 79 ..]); |
|||
end func;</syntaxhighlight> |
|||
Original source: [https://seed7.sourceforge.net/algorith/math.htm#bigInteger_ackermann] |
|||
{{out}} |
|||
<pre> |
|||
A(0, 0) = 1 |
|||
A(0, 1) = 2 |
|||
A(0, 2) = 3 |
|||
A(0, 3) = 4 |
|||
A(0, 4) = 5 |
|||
A(0, 5) = 6 |
|||
A(0, 6) = 7 |
|||
A(0, 7) = 8 |
|||
A(0, 8) = 9 |
|||
A(0, 9) = 10 |
|||
A(1, 0) = 2 |
|||
A(1, 1) = 3 |
|||
A(1, 2) = 4 |
|||
A(1, 3) = 5 |
|||
A(1, 4) = 6 |
|||
A(1, 5) = 7 |
|||
A(1, 6) = 8 |
|||
A(1, 7) = 9 |
|||
A(1, 8) = 10 |
|||
A(1, 9) = 11 |
|||
A(2, 0) = 3 |
|||
A(2, 1) = 5 |
|||
A(2, 2) = 7 |
|||
A(2, 3) = 9 |
|||
A(2, 4) = 11 |
|||
A(2, 5) = 13 |
|||
A(2, 6) = 15 |
|||
A(2, 7) = 17 |
|||
A(2, 8) = 19 |
|||
A(2, 9) = 21 |
|||
A(3, 0) = 5 |
|||
A(3, 1) = 13 |
|||
A(3, 2) = 29 |
|||
A(3, 3) = 61 |
|||
A(3, 4) = 125 |
|||
A(3, 5) = 253 |
|||
A(3, 6) = 509 |
|||
A(3, 7) = 1021 |
|||
A(3, 8) = 2045 |
|||
A(3, 9) = 4093 |
|||
A(4, 0) = 13 |
|||
A(4, 1) = 65533 |
|||
A(4, 2) = (19729 digits) |
|||
20035299304068464649790723515602557504478254755697514192650169737108940595563114 |
|||
... |
|||
84717124577965048175856395072895337539755822087777506072339445587895905719156733 |
|||
</pre> |
|||
=={{header|SETL}}== |
=={{header|SETL}}== |