i am trying to check my numbers is fibonacii or not ?
isFibonacci(13);
function isFibonacci( $testedNumber, $a = 1, $b = 1 )
{
if( $testedNumber == 0 || $testedNumber == 1 )
return true;//returning true for 0 and 1 right away.
$nextFib = $a + $b;//getting the next number in the sequence
if( $nextFib > $testedNumber )
return false;//if we have passed the tested number, it's not in the sequence
else if( $nextFib == $testedNumber )
return true;//if we have a perfect match, the tested number is in the sequence
else
isFibonacci( $testedNumber, $b, $nextFib );//otherwise, get the next fibonacci number and repeat.
}
<?php
function getFibonicciIndex($number)
{
$log_base = (1+sqrt(5))/2;
$index = log(($number*sqrt(5)-(1/2)), $log_base);
return floor($index)+1;
}
function getFibonicciNumber($term)
{
$a = (1+sqrt(5))/2;
$b = (1-sqrt(5))/2;
$fibonicci_number = (pow($a, $term)-pow($b, $term))/sqrt(5);
return $fibonicci_number;
}
$number = 14;
$index = getFibonicciIndex($number);
$index_value = getFibonicciNumber($index);
echo ($number == $index_value) ? "yes" : "no";
//This logic implements the best rule to find the Fibonacci series and their index. First we suppose the given no. is a part of Fibonacci series and try to get index .. and then for that given index we calculate the Fibonacci number and equate it to verify
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.