Created
October 27, 2017 01:28
-
-
Save bdkosher/c218d9020b0e418061951a79f5a7143b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def factors(int n) { | |
(2..<n).findAll { i -> n % i == 0 } | |
} | |
// FIXME. If you provide, e.g., 15 and 30 it should return [3, 5, 15] | |
def commonFactors(int... nums) { | |
def commonFactors = factors(nums[0]) | |
for (int i = 1; i < nums.length; ++i) { | |
commonFactors = commonFactors.intersect(factors(nums[i])) | |
} | |
commonFactors | |
} | |
def gcf(int... nums) { | |
commonFactors(nums).max() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment