All Euler problems
Project Euler

Integer Partition Equations

For the equation 4^t = 2^t + k, if we set x = 2^t, then k = x(x-1). For each integer x >= 2, this gives a distinct positive integer k and a corresponding real t = log_2 x. Each such (k, t) pair is...

Source sync May 21, 2026
Problem #0207
Level Level 06
Solved By 5,308
Languages C++, Python
Answer 44043947822
Length 278 words
geometryoptimizationsequence

Problem Statement

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

For some positive integers \(k\), there exists an integer partition of the form \(4^t = 2^t + k\),

where \(4^t\), \(2^t\), and \(k\) are all positive integers and \(t\) is a real number.

The first two such partitions are \(4^1 = 2^1 + 2\) and \(4^{1.5849625\cdots } = 2^{1.5849625\cdots } + 6\).

Partitions where \(t\) is also an integer are called perfect.

For any \(m \ge 1\) let \(P(m)\) be the proportion of such partitions that are perfect with \(k \le m\).

Thus \(P(6) = 1/2\).

In the following table are listed some values of \(P(m)\).

\begin {align*} P(5) &= 1/1\\ P(10) &= 1/2\\ P(15) &= 2/3\\ P(20) &= 1/2\\ P(25) &= 1/2\\ P(30) &= 2/5\\ \cdots &\\ P(180) &= 1/4\\ P(185) &= 3/13 \end {align*}

Find the smallest \(m\) for which \(P(m) < 1/12345\).

Problem 207: Integer Partition Equations

Mathematical Development

Substitution

Let x=2tx = 2^t. Then 4t=(2t)2=x24^t = (2^t)^2 = x^2, so

x2=x+k  ⟹  k=x(x−1).x^2 = x + k \implies k = x(x-1).

For integer x≥2x \geq 2, the admissible values of kk are therefore

2,6,12,20,30,42,56,72,90,110,…2, 6, 12, 20, 30, 42, 56, 72, 90, 110, \ldots

Counting Partitions

At the boundary value k=x(x−1)k = x(x-1), the total number of partitions counted by P(k)P(k) is exactly the number of integers y≥2y \geq 2 with y(y−1)≤x(x−1)y(y-1) \leq x(x-1), namely x−1x - 1.

Perfect Partitions

A partition is perfect precisely when xx is a power of 2. Thus the perfect values are generated by

x=2,4,8,16,…x = 2, 4, 8, 16, \ldots

and at the boundary k=x(x−1)k = x(x-1) the number of perfect partitions is

⌊log⁡2x⌋.\lfloor \log_2 x \rfloor.

Ratio Analysis

Hence, for k=x(x−1)k = x(x-1),

P(k)=⌊log⁡2x⌋x−1.P(k) = \frac{\lfloor \log_2 x \rfloor}{x - 1}.

As kk moves between two consecutive admissible values, neither the numerator nor the denominator changes, so the minimum is attained at one of these boundary points.

Finding the Threshold

We need

⌊log⁡2x⌋x−1<112345,\frac{\lfloor \log_2 x \rfloor}{x - 1} < \frac{1}{12345},

which is equivalent to

x−1>12345⋅⌊log⁡2x⌋.x - 1 > 12345 \cdot \lfloor \log_2 x \rfloor.

For ⌊log⁡2x⌋=17\lfloor \log_2 x \rfloor = 17, we require

x−1>12345⋅17=209865,x - 1 > 12345 \cdot 17 = 209865,

so the first candidate is x=209867x = 209867. This indeed satisfies ⌊log⁡2209867⌋=17\lfloor \log_2 209867 \rfloor = 17, giving

k=209867⋅209866=44043947822.k = 209867 \cdot 209866 = 44043947822.

Editorial

The substitution x=2tx = 2^t completely linearizes the problem. Instead of reasoning about real exponents, we only need to study the integer sequence k=x(x−1)k = x(x-1). Up to the boundary value indexed by xx, there are exactly x−1x-1 admissible partitions, and the perfect ones are exactly those with xx equal to a power of 2, so there are ⌊log⁡2x⌋\lfloor \log_2 x \rfloor of them.

That means the ratio P(k)P(k) at a boundary point is just ⌊log⁡2x⌋/(x−1)\lfloor \log_2 x \rfloor / (x-1). The answer is therefore the first xx for which (x−1)(x-1) overtakes 12345⌊log⁡2x⌋12345 \lfloor \log_2 x \rfloor, and then we convert back to k=x(x−1)k = x(x-1).

Pseudocode

target = 12345
x = 2

Repeat:
    perfect_count = floor(log_2(x))
    If x - 1 > target * perfect_count:
        return x * (x - 1)
    Increase x by 1

Complexity Analysis

  • Time: O(x\*)O(x^\*), where x\*x^\* is the first integer satisfying the threshold inequality.
  • Space: O(1)O(1).

Answer

44043947822\boxed{44043947822}

Code

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

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

int main() {
    // 4^t = 2^t + k, let x = 2^t, then k = x(x-1).
    // Partitions are indexed by integer x >= 2.
    // Total partitions up to k = x(x-1): x - 1.
    // Perfect partitions: x is a power of 2.
    // Count of perfect partitions up to k = x(x-1): floor(log2(x)).
    // P(k) = floor(log2(x)) / (x-1).
    // P(k) is minimized at left endpoints k = x(x-1).
    // Find smallest x(x-1) where (x-1) > 12345 * floor(log2(x)).

    long long target = 12345;

    for (long long x = 2; x < 300000; x++) {
        int m = 63 - __builtin_clzll(x); // floor(log2(x))
        if ((x - 1) > target * m) {
            cout << x * (x - 1) << endl;
            return 0;
        }
    }

    return 0;
}