Project Euler Lab - Problem 153

#153 - Investigating Gaussian Integers

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

As we all know the equation \(x^2=-1\) has no solutions for real \(x\).
If we however introduce the imaginary number \(i\) this equation has two solutions: \(x=i\) and \(x=-i\).
If we go a step further the equation \((x-3)^2=-4\) has two complex solutions: \(x=3+2i\) and \(x=3-2i\).
\(x=3+2i\) and \(x=3-2i\) are called each others' complex conjugate.
Numbers of the form \(a+bi\) are called complex numbers.
In general \(a+bi\) and \(a-bi\) are each other's complex conjugate.

A Gaussian Integer is a complex number \(a+bi\) such that both \(a\) and \(b\) are integers.
The regular integers are also Gaussian integers (with \(b=0\)).
To distinguish them from Gaussian integers with \(b \ne 0\) we call such integers "rational integers."
A Gaussian integer \(a+bi\) is called a divisor of a rational integer \(n\) if the result \(\dfrac n {a + bi}\) is also a Gaussian integer.
If for example we divide \(5\) by \(1+2i\) we can simplify \(\dfrac{5}{1 + 2i}\) in the following manner:
Multiply numerator and denominator by the complex conjugate of \(1+2i\): \(1-2i\).
The result is \(\dfrac{5}{1 + 2i} = \dfrac{5}{1 + 2i}\dfrac{1 - 2i}{1 - 2i} = \dfrac{5(1 - 2i)}{1 - (2i)^2} = \dfrac{5(1 - 2i)}{1 - (-4)} = \dfrac{5(1 - 2i)}{5} = 1 - 2i\).
So \(1+2i\) is a divisor of \(5\).
Note that \(1+i\) is not a divisor of \(5\) because \(\dfrac{5}{1 + i} = \dfrac{5}{2} - \dfrac{5}{2}i\).
Note also that if the Gaussian Integer \((a+bi)\) is a divisor of a rational integer \(n\), then its complex conjugate \((a-bi)\) is also a divisor of \(n\).

In fact, \(5\) has six divisors such that the real part is positive: \(\{1, 1 + 2i, 1 - 2i, 2 + i, 2 - i, 5\}\).
The following is a table of all of the divisors for the first five positive rational integers:

\(n\) Gaussian integer divisors
with positive real part
Sum \(s(n)\) of
these divisors
\(1\)\(1\)\(1\)
\(2\)\(1, 1+i, 1-i, 2\)\(5\)
\(3\)\(1, 3\)\(4\)
\(4\)\(1, 1+i, 1-i, 2, 2+2i, 2-2i,4\)\(13\)
\(5\)\(1, 1+2i, 1-2i, 2+i, 2-i, 5\)\(12\)

For divisors with positive real parts, then, we have: \(\sum \limits_{n = 1}^{5} {s(n)} = 35\).

\(\sum \limits_{n = 1}^{10^5} {s(n)} = 17924657155\).

What is \(\sum \limits_{n = 1}^{10^8} {s(n)}\)?

This problem is taken from Project Euler, Problem 153.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=153. Published Saturday, 5th May 2007, 10:00 am. Solved by 3,135 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.