Skip to content

Instantly share code, notes, and snippets.

@sedera-tax
Created June 9, 2020 07:57
Show Gist options
  • Save sedera-tax/65f2af9e88abf084fef1bbfb29b72148 to your computer and use it in GitHub Desktop.
Save sedera-tax/65f2af9e88abf084fef1bbfb29b72148 to your computer and use it in GitHub Desktop.
<?php
// A simple PHP program to
// find index of given
// Fibonacci number.
function findIndex($n)
{
// if Fibonacci number
// is less than 2,
// its index will be
// same as number
if ($n <= 1)
return $n;
$a = 0; $b = 1; $c = 1;
$res = 1;
// iterate until generated
// fibonacci number
// is less than given
// fibonacci number
while ($c < $n)
{
$c = $a + $b;
// res keeps track of
// number of generated
// fibonacci number
$res++;
$a = $b;
$b = $c;
}
return $res;
}
// Driver Code
$result = findIndex(21);
echo($result);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment