Number of Common Factors - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: a = 12, b = 18
Output: 4
💡 Note: Common factors are 1, 2, 3, and 6. Both 12 and 18 are divisible by each of these numbers.
Example 2 — Small Numbers
$ Input: a = 25, b = 30
Output: 2
💡 Note: Common factors are 1 and 5. GCD(25,30) = 5, and 5 has factors 1 and 5.
Example 3 — Coprime Numbers
$ Input: a = 7, b = 13
Output: 1
💡 Note: 7 and 13 are prime numbers with no common factors except 1.

Constraints

  • 1 ≤ a, b ≤ 1000

Visualization

Tap to expand
Number of Common Factors: Input → Process → Outputa = 12b = 18Input NumbersFind numbers that divide both:1✓, 2✓, 3✓, 4✗, 5✗, 6✓Check Divisibility4Common FactorsCommon factors of 12 and 18:1236Total count: 4 common factors
Understanding the Visualization
1
Input
Two positive integers a=12, b=18
2
Process
Find all numbers that divide both evenly
3
Output
Count of common factors: 4
Key Takeaway
🎯 Key Insight: Common factors of two numbers are exactly the factors of their GCD
Asked in
Google 15 Microsoft 12
25.0K Views
Medium Frequency
~10 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen