Project Euler Lab - Problem 609

#609 - $\pi$ Sequences

● AdvancedOfficial difficulty: 42%DivisibilityTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

For every \(n \ge 1\) the prime-counting function \(\pi(n)\) is equal to the number of primes not exceeding \(n\).
E.g. \(\pi(6)=3\) and \(\pi(100)=25\).

We say that a sequence of integers \(u = (u_0,\cdots,u_m)\) is a \(\pi\) sequence if

  • \(u_n \ge 1\) for every \(n\)
  • \(u_{n+1}= \pi(u_n)\)
  • \(u\) has two or more elements

For \(u_0=10\) there are three distinct \(\pi\) sequences: \((10,4)\), \((10,4,2)\) and \((10,4,2,1)\).

Let \(c(u)\) be the number of elements of \(u\) that are not prime.
Let \(p(n,k)\) be the number of \(\pi\) sequences \(u\) for which \(u_0\le n\) and \(c(u)=k\).
Let \(P(n)\) be the product of all \(p(n,k)\) that are larger than \(0\).
You are given: \(P(10)=3 \times 8 \times 9 \times 3=648\) and \(P(100)=31038676032\).

Find \(P(10^8)\). Give your answer modulo \(1000000007\).

This problem is taken from Project Euler, Problem 609.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=609. Published Saturday, 9th September 2017, 04:00 pm. Solved by 1,106 members at time of mirroring.

Why this is useful

Mathematical Foundation. Exact integer reasoning and algorithmic efficiency. Foundational rigour and computational discipline rather than a direct trading application.

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 C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided 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.