Consecutive Positive Divisors
Find the number of integers n, 1 < n < 10^7, for which d(n) = d(n+1), where d(n) denotes the number of positive divisors of n.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Find the number of integers \(1 < n < 10^7\), for which \(n\) and \(n + 1\) have the same number of positive divisors. For example, \(14\) has the positive divisors \(1, 2, 7, 14\) while \(15\) has \(1, 3, 5, 15\).
Problem 179: Consecutive Positive Divisors
Mathematical Analysis
The Divisor Function
Definition. For , the divisor count function is:
Key properties:
- is multiplicative: when .
- for any prime .
- .
- Highly composite numbers have many divisors: .
Computing via Sieve
Method 1: Divisor sieve (O(N log N)). For each from 1 to , increment for all multiples . The harmonic series gives the complexity.
Method 2: Smallest prime factor sieve (O(N)). Compute the smallest prime factor (SPF) for all using a linear sieve. Then for each , factorize via repeated division by SPF and compute .
Density of Consecutive Equal Divisor Counts
Theorem (Erdos, 1936). The set of with has positive natural density. That is:
Empirically, this density is approximately , meaning about 9.86% of consecutive pairs have equal divisor counts.
Verification Table
| Count of with | Density | |
|---|---|---|
| 98 | 0.098 | |
| 981 | 0.098 | |
| 9853 | 0.099 | |
| 98530 | 0.099 | |
| 986262 | 0.099 |
Common Divisor Counts
The most common divisor counts for :
| Frequency | Example | |
|---|---|---|
| 2 | 664579 (primes) | 2, 3, 5, 7 |
| 4 | 1904932 | 6, 8, 10, 14 |
| 6 | 1089960 | 12, 18, 20 |
| 8 | 1247701 | 24, 30, 40 |
Solution Approaches
Approach 1: Divisor Sieve (Primary)
- Initialize .
- For to : increment for all multiples .
- Count where for .
Approach 2: SPF Sieve (Faster)
- Compute smallest prime factor via linear sieve.
- Factorize each and compute .
- Count consecutive equal values.
Approach 3: Brute Force Factorization
For each , trial-divide to find the prime factorization and compute . Time: — too slow.
Proof of Correctness
The divisor sieve correctly counts divisors: each contributes exactly 1 to for every multiple of . By definition, , and the sieve iterates over all pairs with .
Complexity Analysis
- Divisor sieve: time, space.
- SPF sieve: time, space.
For , the divisor sieve takes about 2 seconds in C++.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
const int N = 10000000;
vector<int> d(N + 2, 0);
for (int k = 1; k <= N + 1; k++)
for (int j = k; j <= N + 1; j += k)
d[j]++;
int count = 0;
for (int n = 2; n < N; n++)
if (d[n] == d[n + 1]) count++;
assert(count == 986262);
cout << count << endl;
return 0;
}
"""
Problem 179: Consecutive Positive Divisors
Count n in (1, 10^7) where d(n) = d(n+1).
"""
def solve_sieve(N: int = 10_000_000):
d = [0] * (N + 2)
for k in range(1, N + 2):
for j in range(k, N + 2, k):
d[j] += 1
count = sum(1 for n in range(2, N) if d[n] == d[n + 1])
return count, d
def solve_brute(N: int = 1000):
def divisor_count(n):
c = 0
for d in range(1, n + 1):
if n % d == 0: c += 1
return c
return sum(1 for n in range(2, N) if divisor_count(n) == divisor_count(n + 1))
# Verify small case
assert solve_brute(100) == sum(1 for n in range(2, 100)
if sum(1 for d in range(1, n+1) if n%d==0) == sum(1 for d in range(1, n+2) if (n+1)%d==0))
answer, d = solve_sieve()
assert answer == 986262, f"Expected 986262, got {answer}"
print(answer)