Free written solution

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

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.

Read the intuition, approach, code and complexity →