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.
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
a = 12, b = 6
4
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
a = 25, b = 30
2
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
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.