Project Euler Lab - Problem 508

#508 - Integers in Base $i-1$

● ResearchOfficial difficulty: 97%RecurrencesTier D - conceptual / notebook executionNot viewed
↖ Euler Lab

Consider the Gaussian integer \(i-1\). A base \(i-1\) representation of a Gaussian integer \(a+bi\) is a finite sequence of digits \(d_{n - 1}d_{n - 2}\cdots d_1 d_0\) such that:

  • \(a+bi = d_{n - 1}(i - 1)^{n - 1} + d_{n - 2}(i - 1)^{n - 2} + \cdots + d_1(i - 1) + d_0\)
  • Each \(d_k\) is in \(\{0,1\}\)
  • There are no leading zeroes, i.e. \(d_{n-1} \ne 0\), unless \(a+bi\) is itself \(0\)

Here are base \(i-1\) representations of a few Gaussian integers:

\(11+24i \to 111010110001101\)
\(24-11i \to 110010110011\)
\(8+0i \to 111000000\)
\(-5+0i \to 11001101\)
\(0+0i \to 0\)

Remarkably, every Gaussian integer has a unique base \(i-1\) representation!

Define \(f(a + bi)\) as the number of \(1\)s in the unique base \(i-1\) representation of \(a + bi\). For example, \(f(11+24i) = 9\) and \(f(24-11i) = 7\).

Define \(B(L)\) as the sum of \(f(a + bi)\) for all integers \(a, b\) such that \(|a| \le L\) and \(|b| \le L\). For example, \(B(500) = 10795060\).

Find \(B(10^{15}) \bmod 1\,000\,000\,007\).

This problem is taken from Project Euler, Problem 508.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=508. Published Sunday, 22nd March 2015, 10:00 am. Solved by 286 members at time of mirroring.

Why this is useful

Optimization. The transferable skill is replacing infeasible enumeration with a mathematical reduction - the core move in calibration and large-scale computation (Phases 10, 13).

We classify relevance honestly - not every Euler problem is a trading application.

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier D - conceptual / notebook execution
Too heavy for browser Pyodide at original scale: the browser is used for planning, small cases and reasoning; full scale runs in a local notebook.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.