Skip to content

Instantly share code, notes, and snippets.

@BenGoldberg1
Last active October 7, 2016 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenGoldberg1/71a42d16c43e444ac10e0247d17c32b0 to your computer and use it in GitHub Desktop.
Save BenGoldberg1/71a42d16c43e444ac10e0247d17c32b0 to your computer and use it in GitHub Desktop.
Yet another Ackermann function impl.
use v6;
sub ack(Int $m is copy, Int $n is copy) {
my @stack;
loop {
if $m > 3 {
--$m;
push @stack, ($m xx $n).Slip;
--$m;
push @stack, (3 .. $m).reverse.Slip;
$n = 13;
} elsif $m == 3 {
$n = 2 ** ( $n + 3 ) - 3;
} elsif $m == 2 {
$n = 2 * $n + 3;
} elsif $m >= 0 {
$n = $n + $m + 1
} else {
fail "negative m!";
}
return $n unless @stack;
$m = pop @stack;
}
}
say ack(4,1);
say .chars, " digits starting with ", .substr(0,50), "..." given ack(4,2);
say "Took {now - INIT {now}} seconds."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment