Skip to content

Instantly share code, notes, and snippets.

@DuffleOne
Created June 21, 2015 20:46
Show Gist options
  • Save DuffleOne/3f4fc49725ed5df3ef1b to your computer and use it in GitHub Desktop.
Save DuffleOne/3f4fc49725ed5df3ef1b to your computer and use it in GitHub Desktop.
25 Minutes and 22 Seconds to run ack(4,1);
<?php
function ack($m, $n)
{
if (!is_integer($m) or ! is_integer($n)) {
throw new Exception('$m and $n must be integers.');
}
if ($m == 0) {
return $n + 1;
}
if ($n == 0) {
return ack($m - 1, 1);
}
return ack($m - 1, ack($m, $n - 1));
}
for ($i = 0; $i < 6; $i++) {
for ($j = 0; $j < 6; $j ++) {
$start = microtime(true);
$ans = ack($i, $j);
$end = microtime(true);
$diff = $end - $start;
printf("ackermann (%d, %d) is: %d. (%f)\n", $i, $j, $ans, $diff);
}
}
?>
E:\Git\ackermann>php ackermann.php
ackermann (0, 0) is: 1. (0.000011)
ackermann (0, 1) is: 2. (0.000003)
ackermann (0, 2) is: 3. (0.000003)
ackermann (0, 3) is: 4. (0.000003)
ackermann (0, 4) is: 5. (0.000002)
ackermann (0, 5) is: 6. (0.000002)
ackermann (1, 0) is: 2. (0.000004)
ackermann (1, 1) is: 3. (0.000008)
ackermann (1, 2) is: 4. (0.000005)
ackermann (1, 3) is: 5. (0.000006)
ackermann (1, 4) is: 6. (0.000007)
ackermann (1, 5) is: 7. (0.000007)
ackermann (2, 0) is: 3. (0.000007)
ackermann (2, 1) is: 5. (0.000009)
ackermann (2, 2) is: 7. (0.000015)
ackermann (2, 3) is: 9. (0.000022)
ackermann (2, 4) is: 11. (0.000032)
ackermann (2, 5) is: 13. (0.000046)
ackermann (3, 0) is: 5. (0.000010)
ackermann (3, 1) is: 13. (0.000051)
ackermann (3, 2) is: 29. (0.000247)
ackermann (3, 3) is: 61. (0.001093)
ackermann (3, 4) is: 125. (0.004587)
ackermann (3, 5) is: 253. (0.019103)
ackermann (4, 0) is: 13. (0.000053)
ackermann (4, 1) is: 65533. (1522.153492)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment