Project Euler Lab - Problem 756

#756 - Approximating a Sum

● ResearchOfficial difficulty: 74%ApproximationTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Consider a function \(f(k)\) defined for all positive integers \(k>0\). Let \(S\) be the sum of the first \(n\) values of \(f\). That is, \[S=f(1)+f(2)+f(3)+\cdots+f(n)=\sum_{k=1}^n f(k).\]

In this problem, we employ randomness to approximate this sum. That is, we choose a random, uniformly distributed, \(m\)-tuple of positive integers \((X_1,X_2,X_3,\cdots,X_m)\) such that \(0=X_0 \lt X_1 \lt X_2 \lt \cdots \lt X_m \leq n\) and calculate a modified sum \(S^*\) as follows. \[S^* = \sum_{i=1}^m f(X_i)(X_i-X_{i-1})\]

We now define the error of this approximation to be \(\Delta=S-S^*\).

Let \(\mathbb{E}(\Delta|f(k),n,m)\) be the expected value of the error given the function \(f(k)\), the number of terms \(n\) in the sum and the length of random sample \(m\).

For example, \(\mathbb{E}(\Delta|k,100,50) = 2525/1326 \approx 1.904223\) and \(\mathbb{E}(\Delta|\varphi(k),10^4,10^2)\approx 5842.849907\), where \(\varphi(k)\) is Euler's totient function.

Find \(\mathbb{E}(\Delta|\varphi(k),12345678,12345)\) rounded to six places after the decimal point.

This problem is taken from Project Euler, Problem 756.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=756. Published Saturday, 1st May 2021, 02:00 pm. Solved by 408 members at time of mirroring.

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.