Count integer pairs satisfying 1/x + 1/y = 1/N
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates understanding of number theory and algorithmic problem-solving, focusing on Diophantine equation manipulation, divisor counting, and translating algebraic constraints into combinatorial counts.
Constraints
- 1 <= N <= 10^6
- x and y are positive integers
- Count ordered pairs: (x, y) and (y, x) are counted separately when x != y
Examples
Input: (1,)
Expected Output: 1
Explanation: N=1: only pair is (2,2). N^2=1 has 1 divisor.
Input: (2,)
Expected Output: 3
Explanation: N=2: pairs (3,6),(4,4),(6,3). N^2=4 has divisors 1,2,4 -> 3.
Input: (4,)
Expected Output: 5
Explanation: N=4=2^2, N^2=2^4 has 5 divisors -> 5 ordered pairs.
Input: (6,)
Expected Output: 9
Explanation: N=6=2*3, divisor count = (2*1+1)(2*1+1) = 3*3 = 9.
Input: (12,)
Expected Output: 15
Explanation: N=12=2^2*3, count = (2*2+1)(2*1+1) = 5*3 = 15.
Input: (360,)
Expected Output: 105
Explanation: N=360=2^3*3^2*5, count = 7*5*3 = 105.
Input: (999983,)
Expected Output: 3
Explanation: 999983 is prime, so N^2=p^2 has 3 divisors -> 3.
Input: (1000000,)
Expected Output: 169
Explanation: N=10^6=2^6*5^6, count = (2*6+1)(2*6+1) = 13*13 = 169 (upper bound case).
Hints
- Manipulate 1/x + 1/y = 1/N algebraically. With x = N + a and y = N + b, the equation reduces to a*b = N^2.
- Every positive divisor a of N^2 yields exactly one valid ordered pair, so the answer is the number of positive divisors of N^2.
- If N = product of p_i^e_i, then N^2 = product of p_i^(2*e_i), and the divisor count is the product of (2*e_i + 1). Factor N by trial division up to sqrt(N).