Created
June 9, 2020 07:57
-
-
Save sedera-tax/65f2af9e88abf084fef1bbfb29b72148 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// 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