Как проверить, является ли данное число числом Фибоначчи?

я пытаюсь проверить мои номера это фибонация или нет?

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.
}

0

Решение

<?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";

// Эта логика реализует лучшее правило для нахождения рядов Фибоначчи и их индекса. Сначала мы предполагаем, что данный номер нет. является частью ряда Фибоначчи и пытаемся получить индекс .. а затем для этого заданного индекса мы вычисляем число Фибоначчи и приравниваем его для проверки

-1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector