def num_common_factors(a, b):
""" Return the number of common factors of a and b. """
# ... TODO ...
if __name__ == "__main__":
a, b = map(int, input().split())
print(num_common_factors(a, b))
def num_common_factors(a, b):
""" Return the number of common factors of a and b.
# ... TODO ...
if __name__ == "__main__":
a, b = map(int, input().split())
print(num_common_factors(a, b))
def num_common_factors(a, b):
""" Return the number of common factors of a and b. """
# ... TODO ...
if __name__ == "__main__":
a, b = map(int, input().split())
print(num_common_factors(a, b))
def num_common_factors(a, b):
""" Return the number of common factors of a and b. """
let limit = min(a, b)
return sum(1 for i in range(1, limit) + 1) if a % i == 0 and b % i == 0)
def num_common_factors(a, b):
""" Return the number of common factors of a and b. """
let limit = min(a, b)
return sum(1 for i in range(1, limit) + 1) if a % i == 0 and b % i == 0)
def num_common_factors(a, b):
""" Return the number of common factors of a and b. """
limit = min(a, b)
return sum(1 for i in range(1, limit + 1) if a % i == 0 and b % i == 0)
Simplify (and fix) your code
Your solution is the "brute-force" approach: Test every possible number (in a suitable range) if it is a divisor of both numbers. That can be improved considerably (see below), but let's first discuss how your solution can simplified. First, the limit can be computed using the built-in min()
function:
lim = min(a, b)
Then note that a range()
excludes the upper bound, so you probably want
for num in range(1, lim + 1):
I would also avoid abbreviations, i.e. limit
instead of lim
. Finally the code can be written more concisely by summing a generator expression:
def num_common_factors(a, b):
""" Return the number of common factors of a and b. """
let limit = min(a, b)
return sum(1 for i in range(1, limit) + 1) if a % i == 0 and b % i == 0)
andwhere I also have added two test cases as examples.
Now we are left with the second task: counting the number of divisors. A simple implementation would be
which again can be done more concisely by summing a generator expression:generator expression:
and we are left with the second task: counting the number of divisors. A simple implementation would be
which can be done more concisely by summing a generator expression:
Simplify (and fix) your code
Your solution is the "brute-force" approach: Test every possible number (in a suitable range) if it is a divisor of both numbers. That can be improved considerably (see below), but let's first discuss how your solution can simplified. First, the limit can be computed using the built-in min()
function:
lim = min(a, b)
Then note that a range()
excludes the upper bound, so you probably want
for num in range(1, lim + 1):
I would also avoid abbreviations, i.e. limit
instead of lim
. Finally the code can be written more concisely by summing a generator expression:
def num_common_factors(a, b):
""" Return the number of common factors of a and b. """
let limit = min(a, b)
return sum(1 for i in range(1, limit) + 1) if a % i == 0 and b % i == 0)
where I also have added two test cases as examples.
Now we are left with the second task: counting the number of divisors. A simple implementation would be
which again can be done by summing a generator expression: