Created
July 27, 2019 07:26
-
-
Save fre2mansur/7c3b2398f355f1cb5a0269b710a2b0ff to your computer and use it in GitHub Desktop.
Getting the next fibonacci number of given any number
This file contains 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
function isSubset($array1, $array2) { | |
echo (!array_diff($array2, $array1) ? "true" : "false"); | |
} | |
//output | |
isSubset(array("A","B","C","D","E"), array("A","E","D")); // true | |
isSubset(array("A","B","C","D","E"), array("A","D","Z")); // false | |
isSubset(array("A","D","E"), array("A","A","D","E")); // true |
This file contains 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
$input = array(1,22,9,0); | |
var_dump(nextFibonacci($input)); //output [2,34,13,1] | |
function nextFibonacci($input) { | |
foreach($input as $i) { | |
while($i>=0) { | |
$store[] = $i; | |
if(isFibonacci($i)) | |
break; | |
$i--; | |
} | |
$last = end($store); | |
if($last == 0) { | |
$output[] = 1; | |
} else { | |
$output[] = round($last/0.618); | |
} | |
} | |
return $output; | |
} | |
//Checking for square root | |
function isPerfectSquare($x) | |
{ | |
$s = (int)(sqrt($x)); | |
return ($s * $s == $x); | |
} | |
//Determining the fibonacci number | |
function isFibonacci($n) | |
{ | |
return isPerfectSquare(5 * $n * $n + 4) || isPerfectSquare(5 * $n * $n - 4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment