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 Development
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 |
Editorial
The simplest workable approach is a divisor sieve. Instead of factorizing each number independently, the program lets every integer contribute one divisor count to each of its multiples. After this sweep, the array entry at position is exactly .
Once the divisor counts are available, the rest is just a linear scan through consecutive pairs. Every index with is checked against , and the answer is the number of times the two divisor counts match. The code also keeps a small brute-force verifier for tiny limits, but the main computation is entirely driven by the sieve.
Pseudocode
Create an array of divisor counts initialized to zero.
For each integer $k$ from 1 up to $10^7 + 1$:
visit every multiple of $k$ within the array bounds
and add 1 to that multiple's divisor count.
Initialize the answer to zero.
For each $n$ from 2 up to $10^7 - 1$:
if the counts of $n$ and $n+1$ are equal,
increase the answer.
Return the final count.
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)