All Euler problems
Project Euler

Divisibility Streaks

For every positive integer n, define streak(n) = k as the smallest positive integer k such that n + k is not divisible by k + 1. Examples: streak(13) = 4 because 2 | 14, 3 | 15, 4 | 16, but 5 nmid...

Source sync Apr 19, 2026
Problem #0601
Level Level 10
Solved By 2,428
Languages C++, Python
Answer 1617243
Length 256 words
number_theorymodular_arithmeticbrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

For every positive number \(n\) we define the function \(\mathop {streak}(n)=k\) as the smallest positive integer \(k\) such that \(n+k\) is not divisible by \(k+1\).

E.g:

\(13\) is divisible by \(1\)

\(14\) is divisible by \(2\)

\(15\) is divisible by \(3\)

\(16\) is divisible by \(4\)

\(17\) is NOT divisible by \(5\)

So \(\mathop {streak}(13) = 4\).

Similarly:

\(120\) is divisible by \(1\)

\(121\) is NOT divisible by \(2\)

So \(\mathop {streak}(120) = 1\).

Define \(P(s, N)\) to be the number of integers \(n\), \(1 < n N\), for which \(\mathop {streak}(n) = s\).

So \(P(3, 14) = 1\) and \(P(6, 10^6) = 14286\).

Find the sum, as \(i\) ranges from \(1\) to \(31\), of \(P(i, 4^i)\).

Problem 601: Divisibility Streaks

Mathematical Foundation

Definition. Let Ls=lcm(2,3,,s)L_s = \operatorname{lcm}(2, 3, \ldots, s) for s2s \geq 2, and L1=1L_1 = 1.

Theorem 1. For all positive integers nn and s1s \geq 1, streak(n)s\operatorname{streak}(n) \geq s if and only if n1(modLs)n \equiv 1 \pmod{L_s}, where Ls=lcm(2,3,,s)L_s = \operatorname{lcm}(2, 3, \ldots, s).

Proof. The condition streak(n)s\operatorname{streak}(n) \geq s means (j+1)(n+j)(j+1) \mid (n+j) for all j=1,2,,sj = 1, 2, \ldots, s. Since n+j=n1+(j+1)n + j = n - 1 + (j + 1), we have (j+1)(n+j)(j+1) \mid (n+j) if and only if (j+1)(n1)(j+1) \mid (n-1), i.e., n1(modj+1)n \equiv 1 \pmod{j+1}.

The system of congruences n1(modm)n \equiv 1 \pmod{m} for m=2,3,,s+1m = 2, 3, \ldots, s+1 is equivalent to n1(modlcm(2,3,,s+1))n \equiv 1 \pmod{\operatorname{lcm}(2, 3, \ldots, s+1)}. Reindexing, streak(n)s\operatorname{streak}(n) \geq s requires (j+1)(n1)(j+1) \mid (n-1) for j=1,,sj = 1, \ldots, s, i.e., m(n1)m \mid (n-1) for m=2,,s+1m = 2, \ldots, s+1, which is lcm(2,,s+1)(n1)\operatorname{lcm}(2, \ldots, s+1) \mid (n-1).

However, adopting the convention Ls=lcm(2,3,,s+1)L_s = \operatorname{lcm}(2, 3, \ldots, s+1) complicates notation. Instead, note that streak(n)s\operatorname{streak}(n) \geq s means (j+1)(n1)(j+1) \mid (n-1) for j=1,,sj = 1, \ldots, s, equivalently lcm(2,3,,s+1)(n1)\operatorname{lcm}(2, 3, \ldots, s+1) \mid (n-1).

Let us define Ms=lcm(2,3,,s+1)M_s = \operatorname{lcm}(2, 3, \ldots, s+1). Then streak(n)s    Ms(n1)    n1(modMs)\operatorname{streak}(n) \geq s \iff M_s \mid (n-1) \iff n \equiv 1 \pmod{M_s}. \square

Lemma 1. The number of integers nn with 1<n<N1 < n < N satisfying n1(modM)n \equiv 1 \pmod{M} is N2M\left\lfloor \frac{N-2}{M} \right\rfloor.

Proof. We need to count integers n{2,3,,N1}n \in \{2, 3, \ldots, N-1\} with M(n1)M \mid (n-1). Substituting k=n1k = n - 1, we count k{1,2,,N2}k \in \{1, 2, \ldots, N-2\} with MkM \mid k. The number of positive multiples of MM up to N2N - 2 is N2M\left\lfloor \frac{N-2}{M} \right\rfloor. \square

Theorem 2 (Counting Formula). Let Ms=lcm(2,3,,s+1)M_s = \operatorname{lcm}(2, 3, \ldots, s+1). Then

P(s,N)=N2MsN2Ms+1.P(s, N) = \left\lfloor \frac{N-2}{M_s} \right\rfloor - \left\lfloor \frac{N-2}{M_{s+1}} \right\rfloor.

Proof. streak(n)=s\operatorname{streak}(n) = s means streak(n)s\operatorname{streak}(n) \geq s and streak(n)≱s+1\operatorname{streak}(n) \not\geq s+1. By Theorem 1, this is Ms(n1)M_s \mid (n-1) and Ms+1(n1)M_{s+1} \nmid (n-1). By Lemma 1, the count of n(1,N)n \in (1, N) with Ms(n1)M_s \mid (n-1) is (N2)/Ms\lfloor (N-2)/M_s \rfloor, and the count with Ms+1(n1)M_{s+1} \mid (n-1) is (N2)/Ms+1\lfloor (N-2)/M_{s+1} \rfloor. By inclusion, P(s,N)=(N2)/Ms(N2)/Ms+1P(s, N) = \lfloor (N-2)/M_s \rfloor - \lfloor (N-2)/M_{s+1} \rfloor. \square

Remark. By the Prime Number Theorem, logMss\log M_s \sim s (Chebyshev’s theorem gives loglcm(1,,n)n\log \operatorname{lcm}(1,\ldots,n) \sim n), so MsM_s grows roughly as ese^s. For large ii, Mi+1>4i2M_{i+1} > 4^i - 2, making the second floor term zero.

Editorial

We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

    M[1] = 1
    For k from 2 to 33:
        M[k] = lcm(M[k-1], k+1) // M[s] = lcm(2, 3, ..., s+1)

    total = 0
    For i from 1 to 31:
        N = 4^i
        term = floor((N - 2) / M[i]) - floor((N - 2) / M[i+1])
        total = total + term

    Return total

Complexity Analysis

  • Time: O(31B(n))O(31 \cdot B(n)) where B(n)B(n) is the cost of big-integer division. Since MsM_s and 4i4^i have O(i)O(i) digits, each division is O(i2)O(i^2) with schoolbook arithmetic, giving O(i=131i2)=O(313)=O(1)O(\sum_{i=1}^{31} i^2) = O(31^3) = O(1) (constant with respect to any scaling parameter).
  • Space: O(d)O(d) where dd is the maximum digit count of M32M_{32}, which is bounded by a constant.

Answer

1617243\boxed{1617243}

Code

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

C++ project_euler/problem_601/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // streak(n) = k: smallest k s.t. n+k NOT div by k+1
    // streak(n) >= s: n ≡ 1 (mod lcm(2,...,s))
    // P(s,N) = floor((N-2)/lcm(2,...,s)) - floor((N-2)/lcm(2,...,s+1))
    // Answer = sum_{i=1}^{31} P(i, 4^i)

    typedef __int128 lll;

    auto my_gcd = [](lll a, lll b) -> lll {
        while (b) { a %= b; swap(a, b); }
        return a;
    };

    // L[k] = lcm(2, 3, ..., k)
    vector<lll> L(34, 1);
    lll cur = 1;
    for (int k = 2; k <= 33; k++) {
        cur = cur / my_gcd(cur, (lll)k) * k;
        L[k] = cur;
    }

    lll ans = 0;
    for (int i = 1; i <= 31; i++) {
        lll N = 1;
        for (int j = 0; j < i; j++) N *= 4;
        lll Nm2 = N - 2;

        lll c1;
        if (i == 1) {
            c1 = Nm2;
        } else {
            c1 = (L[i] <= Nm2) ? Nm2 / L[i] : 0;
        }
        lll c2 = (L[i+1] <= Nm2) ? Nm2 / L[i+1] : 0;

        ans += c1 - c2;
    }

    // Print __int128
    long long result = (long long)ans;
    cout << result << endl;

    return 0;
}