Project Euler Lab - Problem 122

#122 - Efficient Exponentiation

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

The most naive way of computing \(n^{15}\) requires fourteen multiplications: \[n \times n \times \cdots \times n = n^{15}.\]

But using a "binary" method you can compute it in six multiplications:

\[\begin{align} n \times n &= n^2\\ n^2 \times n^2 &= n^4\\ n^4 \times n^4 &= n^8\\ n^8 \times n^4 &= n^{12}\\ n^{12} \times n^2 &= n^{14}\\ n^{14} \times n &= n^{15} \end{align}\]

However it is yet possible to compute it in only five multiplications:

\[\begin{align} n \times n &= n^2\\ n^2 \times n &= n^3\\ n^3 \times n^3 &= n^6\\ n^6 \times n^6 &= n^{12}\\ n^{12} \times n^3 &= n^{15} \end{align}\]

We shall define \(m(k)\) to be the minimum number of multiplications to compute \(n^k\); for example \(m(15) = 5\).

Find \(\sum\limits_{k = 1}^{200} m(k)\).

This problem is taken from Project Euler, Problem 122.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=122. Published Friday, 2nd June 2006, 06:00 pm. Solved by 9,096 members at time of mirroring.

Why this is useful

General Problem Solving. Builds computational thinking, decomposition, and debugging discipline - transferable, without a specific financial application.

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

Prerequisites

Lessons that prepare you:
1.1 Sets, Functions, and Relations · 19.4 Exact Arithmetic: Big Integers, Rationals, and Floating-Point Traps · 2.1 Functions, Limits, and Continuity · 4.2 Linear Maps, Matrices, Rank, and the Null Space

Recommended stepping-stone problems: #630 · #135 · #144

Concepts: algebra

Likely techniques: big-integer

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.