Project Euler Lab - Problem 585

#585 - Nested Square Roots

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

Consider the term \(\small \sqrt{x+\sqrt{y}+\sqrt{z}}\) that is representing a nested square root. \(x\), \(y\) and \(z\) are positive integers and \(y\) and \(z\) are not allowed to be perfect squares, so the number below the outer square root is irrational. Still it can be shown that for some combinations of \(x\), \(y\) and \(z\) the given term can be simplified into a sum and/or difference of simple square roots of integers, actually denesting the square roots in the initial expression.

Here are some examples of this denesting:
\(\small \sqrt{3+\sqrt{2}+\sqrt{2}}=\sqrt{2}+\sqrt{1}=\sqrt{2}+1\)
\(\small \sqrt{8+\sqrt{15}+\sqrt{15}}=\sqrt{5}+\sqrt{3}\)
\(\small \sqrt{20+\sqrt{96}+\sqrt{12}}=\sqrt{9}+\sqrt{6}+\sqrt{3}-\sqrt{2}=3+\sqrt{6}+\sqrt{3}-\sqrt{2}\)
\(\small \sqrt{28+\sqrt{160}+\sqrt{108}}=\sqrt{15}+\sqrt{6}+\sqrt{5}-\sqrt{2}\)

As you can see the integers used in the denested expression may also be perfect squares resulting in further simplification.

Let F(\(n\)) be the number of different terms \(\small \sqrt{x+\sqrt{y}+\sqrt{z}}\), that can be denested into the sum and/or difference of a finite number of square roots, given the additional condition that \(0<x \le n\). That is,
\(\small \displaystyle \sqrt{x+\sqrt{y}+\sqrt{z}}=\sum_{i=1}^k s_i\sqrt{a_i}\)
with \(k\), \(x\), \(y\), \(z\) and all \(a_i\) being positive integers, all \(s_i =\pm 1\) and \(x\le n\).
Furthermore \(y\) and \(z\) are not allowed to be perfect squares.

Nested roots with the same value are not considered different, for example \(\small \sqrt{7+\sqrt{3}+\sqrt{27}}\), \(\small \sqrt{7+\sqrt{12}+\sqrt{12}}\) and \(\small \sqrt{7+\sqrt{27}+\sqrt{3}}\), that can all three be denested into \(\small 2+\sqrt{3}\), would only be counted once.

You are given that \(F(10)=17\), \(F(15)=46\), \(F(20)=86\), \(F(30)=213\) and \(F(100)=2918\) and \(F(5000)=11134074\).
Find \(F(5000000)\).

This problem is taken from Project Euler, Problem 585.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=585. Published Saturday, 7th January 2017, 07:00 pm. Solved by 222 members at time of mirroring.

Why this is useful

Mathematical Foundation. Exact counting underlies discrete pricing lattices, scenario enumeration, and combinatorial probability (Phase 7).

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.