<?php
function memoize($func) {
$memo = [];
return function($n) use ($func, $memo) {
if (array_key_exists($n, $memo)) {
return $memo[$n];
}
$memo[$n] = $func($n);
return $memo[$n];
};
}
function fib($n) {
if ($n <= 2) {
return 1;
}
return fib($n - 1) + fib($n - 2);
}
for ($i = 1; $i < 11; $i++) {
$fib = memoize('fib');
echo $fib($i) . "\n";
}
To embed this project on your website, copy the following code and paste it into your website's HTML: