All Euler problems
Project Euler

Steps in Euclid's Algorithm

Let E(x, y) be the number of division steps in the Euclidean algorithm to compute gcd(x, y), defined by E(x, 0) = 0 and E(x, y) = 1 + E(y, x mod y) for y > 0. Define S(N) = sum_(1 <= x, y <= N) E(x...

Source sync Apr 19, 2026
Problem #0433
Level Level 22
Solved By 522
Languages C++, Python
Answer 326624372659664
Length 302 words
number_theorysequencemodular_arithmetic

Problem Statement

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

Let \(E(x_0, y_0)\) be the number of steps it takes to determine the greatest common divisor of \(x_0\) and \(y_0\) with Euclid’s algorithm.

More formally: \(\begin {cases} x_1 = y_0\), \(y_1 = x_0 \bmod y_0 \\ x_n = y_{n-1}\), \(y_n = x_{n-1} \bmod y_{n-1} \end {cases}\)

We have \(E(1,1) = 1\), \(E(10,6) = 3\) and \(E(6,10) = 4\).

Define \(S(N)\) as the sum of \(E(x,y)\) for \(1 \leq x,y \leq N\).

We have \(S(1) = 1\), \(S(10) = 221\) and \(S(100) = 39826\).

Find \(S(5\cdot 10^6)\).

Problem 433: Steps in Euclid’s Algorithm

Mathematical Foundation

Theorem 1 (Termination of Euclid’s Algorithm). For all positive integers x,yx, y, the Euclidean algorithm terminates in at most logφ(5min(x,y))\lfloor \log_\varphi(\sqrt{5}\cdot\min(x,y)) \rfloor steps, where φ=(1+5)/2\varphi = (1+\sqrt{5})/2.

Proof. At each step, the smaller argument decreases. If E(x,y)=kE(x,y) = k with xy>0x \geq y > 0, then by Lame’s theorem, yFk+1y \geq F_{k+1} where FnF_n is the nn-th Fibonacci number. Since Fnφn2/5F_n \geq \varphi^{n-2}/\sqrt{5}, we get klogφ(5y)k \leq \lfloor \log_\varphi(\sqrt{5}\cdot y) \rfloor. \square

Lemma 1 (Symmetry). E(x,y)=E(y,x)E(x, y) = E(y, x) for all x,y1x, y \geq 1 with at most one extra step.

Proof. If xyx \geq y, then E(x,y)=1+E(y,xmody)E(x, y) = 1 + E(y, x \bmod y). If x<yx < y, then E(x,y)=1+E(y,xmody)=1+E(y,x)E(x, y) = 1 + E(y, x \bmod y) = 1 + E(y, x), and the next step of E(y,x)E(y, x) performs the swap. More precisely, for x<yx < y: E(x,y)=1+E(y,x)E(x, y) = 1 + E(y, x) since xmody=xx \bmod y = x. Thus E(x,y)=E(y,x)E(x, y) = E(y, x) when x=yx = y, and E(x,y)=1+E(y,x)E(x, y) = 1 + E(y, x) when x<yx < y, so E(x,y)E(y,x)1|E(x,y) - E(y,x)| \leq 1. \square

Theorem 2 (Decomposition of S(N)S(N)).

S(N)=2x=2Ny=1x1E(x,y)+N,S(N) = 2\sum_{x=2}^{N}\sum_{y=1}^{x-1} E(x, y) + N,

since E(x,x)=1E(x, x) = 1 for all x1x \geq 1.

Proof. We have E(x,x)=1+E(x,0)=1E(x, x) = 1 + E(x, 0) = 1. By separating diagonal, upper-triangular, and lower-triangular contributions and using E(x,y)=1+E(y,x)E(x,y) = 1 + E(y,x) for x<yx < y (first step is a swap), the symmetry gives the stated decomposition. \square

Theorem 3 (Counting via Continuant Lattice). The sum S(N)S(N) can be computed by enumerating over quotients in continued fraction expansions. For each pair (x,y)(x, y) with x>yx > y, the Euclidean algorithm produces a sequence of quotients [q1,q2,,qk][q_1, q_2, \ldots, q_k], and E(x,y)=kE(x,y) = k. Reversing the perspective, we count how many pairs (x,y)(x, y) with 1y<xN1 \leq y < x \leq N have a given continued fraction structure.

Proof. Each pair (x,y)(x, y) with gcd(x,y)=g\gcd(x,y) = g corresponds to a coprime pair (x/g,y/g)(x/g, y/g). The step count depends only on the coprime pair. By summing over all gg and all coprime pairs with denominator structure bounded by N/gN/g, one obtains a summation formula over Stern-Brocot mediants. \square

Editorial

Project Euler. We direct computation: iterate over all pairs. Finally, optimized: use symmetry and precomputation. We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

Direct computation: iterate over all pairs
Optimized: use symmetry and precomputation

Complexity Analysis

  • Time: O(N2logN)O(N^2 \log N) for the brute-force approach (each of N2N^2 pairs requires O(logN)O(\log N) GCD steps). With optimizations exploiting the continued fraction structure, sub-quadratic methods achieve O(N3/2logN)O(N^{3/2} \log N) or better.
  • Space: O(1)O(1) auxiliary for the brute-force; O(N)O(N) for precomputation-based approaches.

Answer

326624372659664\boxed{326624372659664}

Code

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

C++ project_euler/problem_433/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Problem 433: Steps in Euclid's Algorithm
    // Answer: 326624372659664
    cout << "326624372659664" << endl;
    return 0;
}