Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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.
Example 1:
Input: a = 12, b = 6 Output: 4 Explanation: The common factors of 12 and 6 are 1, 2, 3, 6.
Example 2:
Input: a = 25, b = 30 Output: 2 Explanation: The common factors of 25 and 30 are 1, 5.
Constraints:
1 <= a, b <= 1000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
12 6
25 30
count-primes)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2427: Number of Common Factors
class Solution {
public int commonFactors(int a, int b) {
int g = gcd(a, b);
int ans = 0;
for (int x = 1; x <= g; ++x) {
if (g % x == 0) {
++ans;
}
}
return ans;
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
// Accepted solution for LeetCode #2427: Number of Common Factors
func commonFactors(a int, b int) (ans int) {
g := gcd(a, b)
for x := 1; x <= g; x++ {
if g%x == 0 {
ans++
}
}
return
}
func gcd(a int, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
# Accepted solution for LeetCode #2427: Number of Common Factors
class Solution:
def commonFactors(self, a: int, b: int) -> int:
g = gcd(a, b)
return sum(g % x == 0 for x in range(1, g + 1))
// Accepted solution for LeetCode #2427: Number of Common Factors
fn common_factors(a: i32, b: i32) -> i32 {
(1..=(std::cmp::min(a, b)))
.into_iter()
.fold(0, |acc, i| {
if a % i == 0 && b % i == 0 {
acc + 1
} else {
acc
}
})
}
fn main() {
let ret = common_factors(12, 6);
println!("ret={ret}");
}
#[test]
fn test_common_factors() {
assert_eq!(common_factors(12, 6), 4);
assert_eq!(common_factors(25, 30), 2);
}
// Accepted solution for LeetCode #2427: Number of Common Factors
function commonFactors(a: number, b: number): number {
const g = gcd(a, b);
let ans = 0;
for (let x = 1; x <= g; ++x) {
if (g % x === 0) {
++ans;
}
}
return ans;
}
function gcd(a: number, b: number): number {
return b === 0 ? a : gcd(b, a % b);
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.