Skip to content
LC-2427 Easy LeetCode

2427. Number of Common Factors

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 79% Topics: Math, Enumeration, Number Theory
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(log(min(a, b)) + sqrt(gcd))
# Space: O(1)

# math
class Solution(object):
    def commonFactors(self, a, b):
        """
        :type a: int
        :type b: int
        :rtype: int
        """
        def gcd(a, b):  # Time: O(log(min(a, b)))
            while b:
                a, b = b, a%b
            return a
        
        g = gcd(a, b)
        result = 0
        x = 1
        while x*x <= g:
            if g%x == 0:
                result += 1 if g//x == x else 2
            x += 1
        return result

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions