Skip to content

Instantly share code, notes, and snippets.

@fre2mansur
Created July 27, 2019 07:28
Show Gist options
  • Save fre2mansur/f900defd3b5fbccdac01c0a089b29e5a to your computer and use it in GitHub Desktop.
Save fre2mansur/f900defd3b5fbccdac01c0a089b29e5a to your computer and use it in GitHub Desktop.
My answers, please read the readme.md for my explanations
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
$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