All Euler problems
Project Euler

Loops of Ropes

Consider n ropes with 2n ends randomly paired. Let E(n) be the expected number of loops. Find floor(E(10^6)).

Source sync Apr 19, 2026
Problem #0807
Level Level 37
Solved By 155
Languages C++, Python
Answer 0.1091523673
Length 232 words
probabilitysequencebrute_force

Problem Statement

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

Give a circle $C$ and an integer $n > 1$, we perform the following operations.

  • In step $0$, we choose two uniformly random points $R_0$ and $B_0$ on $C$.

  • In step $i$ $\left(1 \leq i < n\right)$, we first choose a uniformly random points $R_i$ on C and connect the points $R_{i-1}$ and $R_i$ with a red rope; then choose a uniformly random point $B_i$ on $C$ and connect the points $B_{i-1}$ and $B_i$ with a blue rope.

  • In step $n$, we first connect the points $R_{n-1}$ and $R_0$ with a red rope; then connect the points $B_{n-1}$ and $B_0$ with a blue rope.

Each rope is straight between its two end points, and lies above all previous ropes.

After step $n$, we get a loop of red ropes, and a loop of blue ropes.

Sometimes the two loops can be separated, as in the left figure below; sometimes they are "linked", hence cannot be separated, as in the middle and right figures below.

Problem illustration

Let $P(n)$ be the probability that the two loops can be separated. For example, $P(3) = \frac{11}{20}$ and $P(5) \approx 0.4304177690$.

Find $P(80)$, rounded to $10$ digit after decimal point.

Problem 807: Loops of Ropes

Mathematical Analysis

By a first-step argument: take one end of rope 1. It pairs with one of 2n12n-1 ends uniformly at random.

  • With probability 12n1\frac{1}{2n-1}, it pairs with the other end of rope 1, forming a loop.
  • Otherwise, it merges two ropes into one, reducing to n1n-1 effective ropes.

This gives E(n)=12n1+E(n1)E(n) = \frac{1}{2n-1} + E(n-1).

Derivation

Unrolling the recurrence with E(1)=1E(1) = 1:

E(n)=k=1n12k1=1+13+15++12n1E(n) = \sum_{k=1}^{n} \frac{1}{2k-1} = 1 + \frac{1}{3} + \frac{1}{5} + \cdots + \frac{1}{2n-1}

This is the sum of reciprocals of odd numbers up to 2n12n-1. Equivalently:

E(n)=H2n12HnE(n) = H_{2n} - \frac{1}{2}H_n

where Hm=k=1m1kH_m = \sum_{k=1}^{m}\frac{1}{k} is the harmonic number.

For large nn: E(n)12ln(2n)+γ2+ln212lnn+12ln2+γ2+ln2E(n) \approx \frac{1}{2}\ln(2n) + \frac{\gamma}{2} + \ln 2 \approx \frac{1}{2}\ln n + \frac{1}{2}\ln 2 + \frac{\gamma}{2} + \ln 2.

Proof of Correctness

The recurrence follows from conditioning on the first pairing. When end 1 of rope 1 pairs with end 2 of rope 1 (probability 1/(2n1)1/(2n-1)), exactly one loop forms and n1n-1 ropes remain. When it pairs with an end of rope j1j \neq 1 (probability (2n2)/(2n1)(2n-2)/(2n-1)), the two ropes merge and effectively we have n1n-1 ropes with no new loop. The expectation formula telescopes correctly.

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity Analysis

Computing E(n)=k=1n12k1E(n) = \sum_{k=1}^{n}\frac{1}{2k-1} is O(n)O(n) in exact arithmetic, or O(1)O(1) using the harmonic number formula with floating-point.

Answer

0.1091523673\boxed{0.1091523673}

Code

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

C++ project_euler/problem_807/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n = 1000000;
    double E = 0.0;
    for (int k = 1; k <= n; k++)
        E += 1.0 / (2*k - 1);
    cout << (long long)E << endl;
    return 0;
}