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 Apr 19, 2026
Problem #0179
Level Level 04
Solved By 12,519
Languages C++, Python
Answer 986262
Length 348 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 Analysis

The Divisor Function

Definition. For n=p1e1p2e2pkekn = 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(NlogN)\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 nNn \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:

limN{nN: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 n107n \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

Solution Approaches

Approach 1: Divisor Sieve (Primary)

  1. Initialize d[1N]=0d[1 \ldots N] = 0.
  2. For k=1k = 1 to N+1N+1: increment d[j]d[j] for all multiples jN+1j \leq N+1.
  3. Count nn where d[n]=d[n+1]d[n] = d[n+1] for 2n<1072 \leq n < 10^7.

Approach 2: SPF Sieve (Faster)

  1. Compute smallest prime factor via linear sieve.
  2. Factorize each nn and compute d(n)=(ei+1)d(n) = \prod(e_i + 1).
  3. Count consecutive equal values.

Approach 3: Brute Force Factorization

For each nn, trial-divide to find the prime factorization and compute d(n)d(n). Time: O(NN)O(N \sqrt{N}) — too slow.

Proof of Correctness

The divisor sieve correctly counts divisors: each kk contributes exactly 1 to d[j]d[j] for every multiple jj of kk. By definition, d(n)={k:kn}d(n) = |\{k : k \mid n\}|, and the sieve iterates over all (k,j)(k, j) pairs with kjk \mid j.

Complexity Analysis

  • Divisor sieve: O(NlogN)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;
}