Skip to content

Instantly share code, notes, and snippets.

@daxeh
Last active November 24, 2017 12:12
Show Gist options
  • Save daxeh/abb4244b1212d22f7768e300733286a3 to your computer and use it in GitHub Desktop.
Save daxeh/abb4244b1212d22f7768e300733286a3 to your computer and use it in GitHub Desktop.
Print a list of common divisors given two integers.
<?php
// Print a list of common divisors given two integers.
function generate($min, $max) {
// Base case: max >= min > 1
if ($min <= 1 || $min > $max) {
return;
}
// A divisor if, max modulo min has 0 remainder, print it.
echo $max % $min == 0 ? " $min " : '';
return 1 * generate($min + 1, $max);
}
// Inputs
$array = [[1,5], [2, 6], [5, 7], [5, 24], [3, 10], [7,27]];
foreach ($array as $mm) {
$min = $mm[0];
$max = $mm[1];
echo "($min, $max)\n";
generate($min, $max);
echo "\n\n";
}
// stdout
// (1, 5)
// (2, 6)
// 2 3 6
// (5, 7)
// 7
// (5, 24)
// 6 8 12 24
// (3, 10)
// 5 10
// (7, 27)
// 9 27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment