Last active
February 1, 2017 05:18
-
-
Save aliuygur/4534190 to your computer and use it in GitHub Desktop.
php fibonacci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get value of index in fibonacci series | |
* | |
* @author Ali OYGUR <[email protected]> | |
* @param int $n index number | |
* @return int value of index | |
*/ | |
function fibonacci($n) | |
{ | |
// 0,1 | |
$fib = array(0,1); | |
// 1,1 | |
//$fib = array(1,1); | |
for($i = 1; $i < $n; $i++) { | |
$fib[] = array_sum($fib); | |
$fib = array_slice($fib, 1, NULL, TRUE); | |
} | |
return $fib[$n]; | |
} | |
/** | |
* Get fibonacci series | |
* | |
* @author Ali OYGUR <[email protected]> | |
* @param int $n series length | |
* @return array the fibonacci series | |
*/ | |
function fibonacci_series($n) | |
{ | |
// use 0,1 series | |
$fib = array(0,1); | |
// OR you can use 1,1 series | |
//$fib = array(1,1); | |
for($i = 1; $i < $n; $i++) { | |
$fib[] = array_sum(array_slice($fib, -2)); | |
} | |
return $fib; | |
} | |
// End of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment