mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-07-10 03:26:19 +02:00
27 lines
491 B
PHP
27 lines
491 B
PHP
<?php
|
|
|
|
/*
|
|
* Function to find nth number in Fibonacci sequence.
|
|
* Uses a version of memoization and runs very fast!
|
|
*/
|
|
|
|
/**
|
|
* @param int $n position to check
|
|
* @param array $m array to store solved trees
|
|
*/
|
|
function fibonacciPosition(int $n, array &$m = [])
|
|
{
|
|
if (isset($m[$n])) {
|
|
return $m[$n];
|
|
}
|
|
if ($n < 2) {
|
|
return $n;
|
|
}
|
|
|
|
$m[$n] = fibonacciPosition($n - 1, $m) + fibonacciPosition($n - 2, $m);
|
|
|
|
return $m[$n];
|
|
}
|
|
|
|
print fibonacciPosition(59);
|