Project Euler Lab - Problem 535

#535 - Fractal Sequence

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

Consider the infinite integer sequence S starting with:
\(S = 1, 1, 2, 1, 3, 2, 4, 1, 5, 3, 6, 2, 7, 8, 4, 9, 1, 10, 11, 5, \dots\)

Circle the first occurrence of each integer.
\(S = \enclose{circle}1, 1, \enclose{circle}2, 1, \enclose{circle}3, 2, \enclose{circle}4, 1, \enclose{circle}5, 3, \enclose{circle}6, 2, \enclose{circle}7, \enclose{circle}8, 4, \enclose{circle}9, 1, \enclose{circle}{10}, \enclose{circle}{11}, 5, \dots\)

The sequence is characterized by the following properties:

  • The circled numbers are consecutive integers starting with \(1\).
  • Immediately preceding each non-circled numbers \(a_i\), there are exactly \(\lfloor \sqrt{a_i} \rfloor\) adjacent circled numbers, where \(\lfloor\,\rfloor\) is the floor function.
  • If we remove all circled numbers, the remaining numbers form a sequence identical to \(S\), so \(S\) is a fractal sequence.

Let \(T(n)\) be the sum of the first \(n\) elements of the sequence.
You are given \(T(1) = 1\), \(T(20) = 86\), \(T(10^3) = 364089\) and \(T(10^9) = 498676527978348241\).

Find \(T(10^{18})\). Give the last \(9\) digits of your answer.

This problem is taken from Project Euler, Problem 535.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=535. Published Saturday, 21st November 2015, 04:00 pm. Solved by 336 members at time of mirroring.

Why this is useful

Algorithmic Development. Graph/state-space search transfers to routing, dependency resolution, and execution-path optimisation (Phases 16, 17).

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.