Skip to content

Instantly share code, notes, and snippets.

@kremerben
Created February 3, 2015 07:51
Show Gist options
  • Save kremerben/a10e3c03d377fd2fed11 to your computer and use it in GitHub Desktop.
Save kremerben/a10e3c03d377fd2fed11 to your computer and use it in GitHub Desktop.
# /*
# * Given two numbers, find all factors of each.
# * Return all of their common factors sorted from highest to lowest.
# *
# * Example 1:
# * Find all the common factors of 12 and 18.
# * Factors of 12 are 12, 6, 4, 3, 2, 1
# * Factors of 18 are 18, 9, 6, 3, 2, 1
# * The common factors of 12 and 18 are 6, 3, 2, 1
# *
# * Example 2:
# * var common = commonFactors(20, 25);
# * console.log(common); // [5, 1]
# */
@winnietong
Copy link

x = 12
y = 18

def factorise(x, y):
    common = []
    for i in range(1, x):
        if x%i == y%i == 0 :
            common.append(i)
    return common

print factorise(x, y)

@kremerben
Copy link
Author

def common_factors(num1, num2):
    common = []
    for x in range(1, min(num1, num2)+1):
        if num1 % x == 0 and num2 % x == 0:
            common.insert(0,x)
    return common

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment