All Euler problems
Project Euler

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.

Source sync May 21, 2026
Problem #0179
Level Level 04
Solved By 12,519
Languages C++, Python
Answer 986262
Length 354 words
number_theorymodular_arithmeticbrute_force

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 n=p1e1p2e2⋯pkekn = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k}, the divisor count function is:

d(n)=∏i=1k(ei+1)d(n) = \prod_{i=1}^{k} (e_i + 1)

Key properties:

  1. dd is multiplicative: d(mn)=d(m)d(n)d(mn) = d(m)d(n) when gcd⁡(m,n)=1\gcd(m,n) = 1.
  2. d(p)=2d(p) = 2 for any prime pp.
  3. d(pk)=k+1d(p^k) = k + 1.
  4. Highly composite numbers have many divisors: d(720720)=240d(720720) = 240.

Computing d(n)d(n) via Sieve

Method 1: Divisor sieve (O(N log N)). For each kk from 1 to NN, increment d[j]d[j] for all multiples j=k,2k,3k,…j = k, 2k, 3k, \ldots. The harmonic series ∑k=1NN/k=O(Nlog⁡N)\sum_{k=1}^{N} N/k = O(N \log N) gives the complexity.

Method 2: Smallest prime factor sieve (O(N)). Compute the smallest prime factor (SPF) for all n≤Nn \leq N using a linear sieve. Then for each nn, factorize via repeated division by SPF and compute d(n)=∏(ei+1)d(n) = \prod(e_i + 1).

Density of Consecutive Equal Divisor Counts

Theorem (Erdos, 1936). The set of nn with d(n)=d(n+1)d(n) = d(n+1) has positive natural density. That is:

lim⁡N→∞∣{n≤N:d(n)=d(n+1)}∣N>0\lim_{N \to \infty} \frac{|\{n \leq N : d(n) = d(n+1)\}|}{N} > 0

Empirically, this density is approximately 0.09860.0986, meaning about 9.86% of consecutive pairs have equal divisor counts.

Verification Table

NNCount of n<Nn < N with d(n)=d(n+1)d(n) = d(n+1)Density
10310^3980.098
10410^49810.098
10510^598530.099
10610^6985300.099
10710^79862620.099

Common Divisor Counts

The most common divisor counts for n≤107n \leq 10^7:

d(n)d(n)FrequencyExample nn
2664579 (primes)2, 3, 5, 7
419049326, 8, 10, 14
6108996012, 18, 20
8124770124, 30, 40

Editorial

The simplest workable approach is a divisor sieve. Instead of factorizing each number independently, the program lets every integer kk contribute one divisor count to each of its multiples. After this sweep, the array entry at position nn is exactly d(n)d(n).

Once the divisor counts are available, the rest is just a linear scan through consecutive pairs. Every index nn with 2≤n<1072 \le n < 10^7 is checked against n+1n+1, 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: O(Nlog⁡N)O(N \log N) time, O(N)O(N) space.
  • SPF sieve: O(N)O(N) time, O(N)O(N) space.

For N=107N = 10^7, the divisor sieve takes about 2 seconds in C++.

Answer

986262\boxed{986262}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_179/solution.cpp
#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;
}