All Euler problems
Project Euler

Investigating Gaussian Integers

For 1 <= n <= N = 10^8, find sum_(n=1)^N R(n), where R(n) is the sum of the real parts of all Gaussian integer divisors of n that have positive real part. A Gaussian integer g = a + bi (with a, b i...

Source sync Apr 19, 2026
Problem #0153
Level Level 08
Solved By 3,097
Languages C++, Python
Answer 17971254122360635
Length 411 words
number_theorybrute_forcemodular_arithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

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:

$\textcolor{red}{n}$Gaussian interger divisors with positive real partSum 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: $\displaystyle \sum \limits_{n = 1}^{5} {s(n)} = 35$.

You are also given $\displaystyle \sum \limits_{n = 1}^{10^5} {s(n)} = 17924657155$.

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

Problem 153: Investigating Gaussian Integers

Mathematical Development

Definition 1. A Gaussian integer is an element of Z[i]={a+bi:a,bZ}\mathbb{Z}[i] = \{a + bi : a, b \in \mathbb{Z}\}. The norm of g=a+big = a + bi is N(g)=a2+b2N(g) = a^2 + b^2. For a positive integer nn, a Gaussian integer gg divides nn if n/gZ[i]n/g \in \mathbb{Z}[i].

Theorem 1 (Divisibility Criterion). Let g=a+big = a + bi with gcd(a,b)=1\gcd(a, b) = 1 and a,b0a, b \ge 0, not both zero. Then gng \mid n in Z[i]\mathbb{Z}[i] if and only if (a2+b2)n(a^2 + b^2) \mid n.

Proof. We have n/g=ngˉ/g2=n(abi)/(a2+b2)n/g = n\bar{g}/|g|^2 = n(a - bi)/(a^2 + b^2). This lies in Z[i]\mathbb{Z}[i] if and only if (a2+b2)na(a^2 + b^2) \mid na and (a2+b2)nb(a^2 + b^2) \mid nb. Since gcd(a,b)=1\gcd(a, b) = 1, we claim gcd(a,a2+b2)=gcd(a,b2)=1\gcd(a, a^2 + b^2) = \gcd(a, b^2) = 1 (the last equality follows because any prime dividing both aa and b2b^2 divides bb, contradicting gcd(a,b)=1\gcd(a,b) = 1). Similarly gcd(b,a2+b2)=1\gcd(b, a^2 + b^2) = 1. Therefore both conditions reduce to (a2+b2)n(a^2 + b^2) \mid n. \square

Theorem 2 (General Divisibility). For a general Gaussian integer g=da+dbig = da' + db'i where d=gcd(a,b)d = \gcd(a,b), a=a/da' = a/d, b=b/db' = b/d, and gcd(a,b)=1\gcd(a', b') = 1, we have gng \mid n if and only if d(a2+b2)nd(a'^2 + b'^2) \mid n.

Proof. Write n/g=n/(d(a+bi))n/g = n/(d(a' + b'i)). For this to lie in Z[i]\mathbb{Z}[i], we need d(a+bi)nd(a' + b'i) \mid n, i.e., (a+bi)(n/d)(a' + b'i) \mid (n/d), which requires dnd \mid n and (a2+b2)(n/d)(a'^2 + b'^2) \mid (n/d), i.e., d(a2+b2)nd(a'^2 + b'^2) \mid n. \square

Theorem 3 (Decomposition of R(n)R(n)). Let R(n)R(n) denote the sum of the real parts of all Gaussian integer divisors of nn with positive real part. Then:

R(n)=σ(n)+2a1,b1gcd(a,b)=1d1d(a2+b2)ndaR(n) = \sigma(n) + 2 \sum_{\substack{a' \ge 1,\, b' \ge 1 \\ \gcd(a', b') = 1}} \sum_{\substack{d \ge 1 \\ d(a'^2 + b'^2) \mid n}} d \cdot a'

where σ(n)=dnd\sigma(n) = \sum_{d \mid n} d is the standard divisor sum function.

Proof. The Gaussian divisors of nn with positive real part decompose into three classes:

  1. Real divisors d+0id + 0i with dnd \mid n, d>0d > 0: contribute σ(n)\sigma(n).
  2. Purely imaginary divisors 0+bi0 + bi: contribute real part 00.
  3. Complex divisors a+bia + bi with a>0a > 0, b0b \neq 0: for each such divisor with b>0b > 0, the conjugate abia - bi also divides nn (since nn is real) and has the same positive real part aa. Writing a=daa = da', b=db|b| = db' with gcd(a,b)=1\gcd(a', b') = 1, the pair contributes 2da2da'.

Summing over all coprime pairs (a,b)(a', b') with a,b1a', b' \ge 1 and all valid multipliers dd yields the formula. \square

Theorem 4 (Summation Formula). The total answer is:

n=1NR(n)=d=1NdNdS1+2a1,b1gcd(a,b)=1a2+b2N  k=1k(a2+b2)NkaNk(a2+b2)S2\sum_{n=1}^{N} R(n) = \underbrace{\sum_{d=1}^{N} d \left\lfloor \frac{N}{d} \right\rfloor}_{S_1} + 2 \underbrace{\sum_{\substack{a' \ge 1,\, b' \ge 1 \\ \gcd(a', b') = 1 \\ a'^2 + b'^2 \le N}} \; \sum_{\substack{k = 1 \\ k(a'^2 + b'^2) \le N}}^{} k \cdot a' \left\lfloor \frac{N}{k(a'^2 + b'^2)} \right\rfloor}_{S_2}

Proof. For S1S_1: n=1Nσ(n)=n=1Ndnd=d=1NdN/d\sum_{n=1}^{N} \sigma(n) = \sum_{n=1}^{N} \sum_{d \mid n} d = \sum_{d=1}^{N} d \lfloor N/d \rfloor by exchanging the order of summation.

For S2S_2: fix a coprime pair (a,b)(a', b') and a multiplier d1d \ge 1. The Gaussian integer da+dbida' + db'i divides exactly those nn that are multiples of d(a2+b2)d(a'^2 + b'^2). Writing d=kd = k, the number of such nNn \le N is N/(k(a2+b2))\lfloor N/(k(a'^2 + b'^2)) \rfloor, and each contributes real part kaka'. Summing over all (a,b,k)(a', b', k) and exchanging with the sum over nn yields the formula. \square

Lemma 1 (Efficient Evaluation of S1S_1). The sum S1=d=1NdN/dS_1 = \sum_{d=1}^{N} d \lfloor N/d \rfloor can be computed in O(N)O(\sqrt{N}) time via the hyperbola method.

Proof. There are at most 2N2\sqrt{N} distinct values of N/d\lfloor N/d \rfloor for d{1,,N}d \in \{1, \ldots, N\}. Group consecutive values of dd sharing the same quotient q=N/dq = \lfloor N/d \rfloor into blocks [dmin,dmax][d_{\min}, d_{\max}] where dmax=N/qd_{\max} = \lfloor N/q \rfloor. Within each block, d=dmindmaxd=(dmin+dmax)(dmaxdmin+1)/2\sum_{d = d_{\min}}^{d_{\max}} d = (d_{\min} + d_{\max})(d_{\max} - d_{\min} + 1)/2 is computed in O(1)O(1). \square

Editorial

We part 1: Divisor sum via hyperbola method. We then part 2: Gaussian integer contributions. Finally, sum over multiplier k using hyperbola method.

Pseudocode

Part 1: Divisor sum via hyperbola method
Part 2: Gaussian integer contributions
Sum over multiplier k using hyperbola method

Complexity Analysis

  • Time: O(N)O(\sqrt{N}) for S1S_1 via the hyperbola method. For S2S_2, the outer loops iterate over coprime pairs (a,b)(a, b) with a2+b2Na^2 + b^2 \le N, totaling O(N)O(N) pairs (approximately 6π2πN4\frac{6}{\pi^2} \cdot \frac{\pi N}{4} coprime lattice points in the quarter-disk). The inner hyperbola summation for each pair takes O(N/norm)O(\sqrt{N / \text{norm}}), but the aggregate cost is dominated by O(N)O(N). Total: O(N)O(N).
  • Space: O(1)O(1) auxiliary space.

Answer

17971254122360635\boxed{17971254122360635}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_153/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    const ll N = 100000000LL;

    // Part 1: sum_{d=1}^{N} d * floor(N/d) via hyperbola method
    ll part1 = 0;
    for (ll d = 1; d <= N; ) {
        ll q = N / d;
        ll d_max = N / q;
        part1 += q * (d + d_max) * (d_max - d + 1) / 2;
        d = d_max + 1;
    }

    // Part 2: Gaussian integer contributions
    ll part2 = 0;
    for (ll b = 1; b * b + 1 <= N; b++) {
        for (ll a = 1; a * a + b * b <= N; a++) {
            if (__gcd(a, b) != 1) continue;
            ll norm = a * a + b * b;
            for (ll k = 1; k * norm <= N; k++) {
                ll cnt = N / (k * norm);
                part2 += 2 * k * a * cnt;
            }
        }
    }

    printf("%lld\n", part1 + part2);
    return 0;
}