Skip to content

Instantly share code, notes, and snippets.

@aliuygur
Last active February 1, 2017 05:18
Show Gist options
  • Save aliuygur/4534190 to your computer and use it in GitHub Desktop.
Save aliuygur/4534190 to your computer and use it in GitHub Desktop.
php fibonacci
<?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