Skip to content

Practice Number of Common Factors

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2427
LeetCode Problem

Number of Common Factors

Problem Statement

Given two positive integers a and b, return the number of common factors of a and b. An integer x is a common factor of a and b if x divides both a and b.

Source: Number of Common Factors on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
a = 12, b = 6
Output
4
Explanation

The divisors of 6 are 1, 2, 3 and 6, and all four also divide 12 exactly. No number greater than 6 can be a positive divisor of 6, so this list is complete and the answer is 4. Equivalently, the common factors are precisely the divisors of gcd(12,6) = 6. Return their count, not the GCD itself; returning 6 would confuse two different quantities.

# Example 2

Input
a = 25, b = 30
Output
2
Explanation

The positive divisors of 25 are 1, 5 and 25. Of these, 1 and 5 also divide 30, while 25 does not. Thus there are two common factors. The GCD is 5, whose divisors are exactly 1 and 5. Count 1 as a factor even when there are no larger shared divisors, and test divisibility of both numbers rather than accepting a divisor of either one.

Constraints

1 <= a, b <= 1000

Related LeetCode Problems

Practice more problems similar to Number of Common Factors.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.