Skip to content

Instantly share code, notes, and snippets.

@amnuts
Last active March 10, 2021 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amnuts/09129570bf2a98522975d3ba56c7be7b to your computer and use it in GitHub Desktop.
Save amnuts/09129570bf2a98522975d3ba56c7be7b to your computer and use it in GitHub Desktop.
jotting down functions
<?php
// Return greatest common divider of two numbers
function gcd($a, $b) {
return $b ? gcd($b, $a % $b) : $a;
}
// Returns the least common multiple of two or more numbers.
function lcm(...$numbers): int
{
$ans = $numbers[0];
for ($i = 1, $max = count($numbers); $i < $max; $i++) {
$ans = (($numbers[$i] * $ans) / gcd($numbers[$i], $ans));
}
return $ans;
}
lcm(12, 7); // 84
lcm(1, 3, 4, 5); // 60
/**
* @param string $needle Such as, `/^AWS_/`
* @param array $input An associative array
* @return array
*/
function preg_grep_keys(string $needle, array $haystack): array {
return array_intersect_key($haystack, array_flip(preg_grep($needle, array_keys($haystack))));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment