LeetCode solutions

2507. Number of Common Factors

My accepted Python solution to LeetCode problem 2507, Number of Common Factors, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 12.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 12.5MB, accepted 2025-12-24.

python
class Solution(object):
    def commonFactors(self, a, b):
        def gcd(x, y):
            while y:
                x, y = y, x % y
            return x
        g = gcd(a, b)
        count = 0
        for i in range(1, g + 1):
            if g % i == 0:
                count += 1
        return count

Source