Number of Common Factors (LeetCode 2427): Solution and Explanation

Easy
Math
Enumeration
Number Theory

Every common factor of a and b divides gcd(a, b), and every divisor of the gcd divides both. So find the gcd with Euclid's algorithm and count its divisors, testing only up to the square root.

Intuition

The direct way tries every x from 1 to min(a, b) and checks both remainders. That is fine for numbers up to 1000, but there is a cleaner view.

If x divides a and b, it divides any combination of them, including the remainder a % b. That is why Euclid's algorithm keeps the set of common divisors unchanged while the numbers shrink, ending at gcd(a, b).

So the question becomes: how many divisors does g = gcd(a, b) have? Divisors pair up as d and g / d, with the smaller one at most the square root of g, so a short scan finds them all.

Approach: Euclid, then paired divisor count

  1. While b is not 0, replace (a, b) with (b, a % b). When the loop ends, a holds the gcd g.
  2. Set count = 0 and test each d from 1 while d * d <= g.
  3. If g % d == 0, add 2 for the pair (d, g / d), or 1 when d * d == g because the pair is a single number.
  4. Return count.

Step-by-step example

a = 12, b = 6
  1. Euclid: 12 = 2 × 6 + 0, so the remainder is 0 and gcd(12, 6) = 6.
  2. d = 1: 6 % 1 = 0, the pair (1, 6) adds 2. count = 2.
  3. d = 2: 6 % 2 = 0, the pair (2, 3) adds 2. count = 4.
  4. d = 3: 3 × 3 = 9 > 6, so the scan stops.
  5. The common factors are 1, 2, 3, 6. Return 4.

Code: Python, Java, C++

1class Solution:
2    def commonFactors(self, a: int, b: int) -> int:
3        while b:
4            a, b = b, a % b
5        count = 0
6        d = 1
7        while d * d <= a:
8            if a % d == 0:
9                count += 1 if d * d == a else 2
10            d += 1
11        return count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(log(min(a, b)) + sqrt(g))

Euclid's algorithm takes a logarithmic number of steps, and the divisor scan stops at the square root of the gcd.

Space

O(1)

Only a few integer variables are used.

Other approaches

Try every candidate (time O(min(a, b)), space O(1))

Loop x from 1 to min(a, b) and count the x with a % x == 0 and b % x == 0. Simple and fast enough for a, b up to 1000.

Prime factorization of the gcd (time O(sqrt(g)), space O(1))

Factor g as p1^e1 × p2^e2 × ... and return (e1 + 1)(e2 + 1)... . Useful when the same gcd is queried often or g is large.

Common mistakes

  • Counting a perfect-square divisor twice. When d * d == g, the pair (d, g / d) is one number.
  • Forgetting that 1 is always a common factor, so the answer is never 0.
  • Scanning only to sqrt(min(a, b)) without first reducing to the gcd. Some divisors of the smaller number do not divide the larger one.

Frequently asked questions

Why are the common factors exactly the divisors of the gcd?

The gcd is a common factor, so its divisors divide a and b too. Conversely, Euclid's algorithm shows that any common factor divides every remainder it produces, including the final one, which is the gcd.

Is the gcd step needed for a, b up to 1000?

No, the brute-force loop is fast enough. The gcd approach scales to large inputs and is the idea interviewers usually want to hear.

What happens when a and b are coprime?

Their gcd is 1, which has a single divisor, so the answer is 1.