Positive Divisors of N² via Prime Factorization of N
Math & Bit ManipulationAuto-graded · 23 test casesLast updated
Tests whether a candidate can reduce a unit-fraction equation to a divisor-counting problem using algebraic factoring and number theory.
Unit Fraction Pair Counter
You are given a positive integer n. Two positive integers x and y are called a matching pair for n if they satisfy:
$$\frac{1}{x} + \frac{1}{y} = \frac{1}{n}$$
Your task is to count how many ordered matching pairs (x, y) exist, where both x and y are positive integers. Order matters: (x, y) and (y, x) are counted separately when x ≠ y.
Your solution must be efficient enough to handle values as large as n = 10^6 without brute-forcing over candidate values of x or y.
Constraints
1 ≤ n ≤ 10^6- Both
xandymust be strictly positive integers. - Ordered pairs are counted:
(x, y)and(y, x)are distinct wheneverx ≠ y. - The answer is always a positive odd integer.
Examples
Example 1
Input: count_integer_pairs(1)
Output: 1
Example 2
Input: count_integer_pairs(2)
Output: 3
Example 3
Input: count_integer_pairs(12)
Output: 15
Hints
Hint 1
Try multiplying both sides by x·y·n to clear all three denominators, then rearrange so that one side equals zero. See if you can add a constant to both sides to make the left side factor into two binomials.
Hint 2
After factoring, you get (x − n)(y − n) = n². Convince yourself that both factors must be strictly positive. This means you are really counting the number of ways to write n² as a product of two positive integers — which is just the number of positive divisors of n².
Hint 3
You never need to compute n² explicitly. If n = p₁^e₁ · p₂^e₂ · … then d(n²) = ∏(2eᵢ + 1). Factor n by trial division up to √n in O(√n) time, accumulate the product, and handle any leftover prime greater than √n at the end.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Step 1 — Algebraic reduction
Start from the equation:
$$\frac{1}{x} + \frac{1}{y} = \frac{1}{n}$$
Multiply every term by x·y·n to clear the denominators:
$$n \cdot y + n \cdot x = x \cdot y$$
Rearrange so the right side is zero:
$$xy - nx - ny = 0$$
Add n² to both sides (Simon's Favourite Factoring Trick):
$$(x - n)(y - n) = n^2$$
Let a = x − n and b = y − n. The equation becomes a · b = n².
Step 2 — Why both factors must be positive
Because y > 0, the original equation gives 1/x = 1/n − 1/y. For this to be positive we need 1/n > 1/y, i.e. y > n, so b = y − n ≥ 1. The same argument applied to x gives a ≥ 1. Negative factor pairs are impossible: if both a and b were negative their product would be at most (n−1)² < n², a contradiction.
Step 3 — Ordered pairs ↔ positive divisors of n²
Every positive divisor a of n² pairs with exactly one other positive divisor b = n²/a, giving the unique solution x = a + n, y = b + n. The mapping is a bijection, so the answer is exactly d(n²), the number of positive divisors of n².
Step 4 — Compute d(n²) without squaring n
By the multiplicative divisor formula, if n = p₁^e₁ · p₂^e₂ · … then:
$$d(n^2) = \prod_{i} (2e_i + 1)$$
Factor n by trial division up to √n. For each prime p found with exponent e, multiply the running product by 2e + 1. If a factor > 1 remains after the loop, it is a prime with exponent 1 and contributes a factor of 3.
Why the answer is always odd
Each term 2e + 1 is odd, and a product of odd numbers is odd. Geometrically, the one "symmetric" solution x = y = 2n (from a = b = n) is always present and is the only self-paired divisor.
Correctness on edge cases
n | Reason | Answer |
|---|---|---|
| 1 | No primes found, remaining stays 1, loop contributes nothing | 1 |
| 2 | Prime 2 with e=1 → 3; no remainder | 3 |
| 12 = 2²·3 | Factors: (2·2+1)·(2·1+1) = 5·3 | 15 |
| prime p | No factor found in loop; leftover prime gives 3 | 3 |
Complexity
- Time: O(√n) — trial division up to √n, at most ~1 000 iterations for n = 10⁶.
- Space: O(1) — only a handful of scalar variables.
Brute-forcing over all x from n+1 to n² + n would be O(n²) ≈ 10¹² steps for n = 10⁶, which is completely infeasible. Enumerating divisors of n² directly up to √(n²) = n is O(n), which would pass but misses the key insight.
Full solution
def count_integer_pairs(n: int) -> int:
"""
Count ordered pairs (x, y) of positive integers satisfying 1/x + 1/y = 1/n.
Algebraic reduction:
1/x + 1/y = 1/n
=> n*y + n*x = x*y
=> x*y - n*x - n*y = 0
=> (x - n)(y - n) = n^2 [Simon's Favourite Factoring Trick]
Both factors must be positive (since x > n and y > n),
so the answer equals the number of positive divisors of n^2.
Divisor-count formula: if n = p1^e1 * p2^e2 * ...,
then d(n^2) = product of (2*ei + 1).
We factor n by trial division in O(sqrt(n)) without ever squaring n.
"""
result = 1
p = 2
remaining = n
while p * p <= remaining:
if remaining % p == 0:
exponent = 0
while remaining % p == 0:
remaining //= p
exponent += 1
result *= 2 * exponent + 1 # prime p^e in n contributes p^(2e) in n^2
p += 1
if remaining > 1: # leftover prime with exponent 1 contributes factor of 3
result *= 3
return result