Last active
November 24, 2017 12:12
-
-
Save daxeh/abb4244b1212d22f7768e300733286a3 to your computer and use it in GitHub Desktop.
Print a list of common divisors given two integers.
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
<?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