Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Created October 27, 2017 01:28
Show Gist options
  • Save bdkosher/c218d9020b0e418061951a79f5a7143b to your computer and use it in GitHub Desktop.
Save bdkosher/c218d9020b0e418061951a79f5a7143b to your computer and use it in GitHub Desktop.
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