All Euler problems
Project Euler

Pythagorean Odds

Albert chooses a positive integer k, then picks two reals a, b uniformly from [0,1]. He computes sqrt((ka+1)^2 + (kb+1)^2) and rounds to the nearest integer. If the result equals k, he scores k poi...

Source sync Apr 19, 2026
Problem #0285
Level Level 13
Solved By 1,441
Languages C++, Python
Answer 157055.80999
Length 260 words
geometrygame_theoryprobability

Problem Statement

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

Albert chooses a positive integer $k$, then two real numbers $a, b$ are randomly chosen in the interval $[0,1]$ with uniform distribution.

The square root of the sum $(k \cdot a + 1)^2 + (k \cdot b + 1)^2$ is then computed and rounded to the nearest integer. If the result is equal to $k$, he scores $k$ points; otherwise he scores nothing.

For example, if $k = 6$, $a = 0.2$ and $b = 0.85$, then $(k \cdot a + 1)^2 + (k \cdot b + 1)^2 = 42.05$.

The square root of $42.05$ is $6.484\cdots$ and when rounded to the nearest integer, it becomes $6$.

This is equal to $k$, so he scores $6$ points.

It can be shown that if he plays $10$ turns with $k = 1, k = 2, \dots, k = 10$, the expected value of his total score, rounded to five decimal places, is $10.20914$.

If he plays $10^5$ turns with $k = 1, k = 2, k = 3, \dots, k = 10^5$, what is the expected value of his total score, rounded to five decimal places?

Problem 285: Pythagorean Odds

Mathematical Foundation

Theorem (Winning Probability as Annular Area). The probability of winning round kk is Pk=A(r2,c)A(r1,c)P_k = A(r_2, c) - A(r_1, c), where r1=2k12kr_1 = \frac{2k-1}{2k}, r2=2k+12kr_2 = \frac{2k+1}{2k}, c=1kc = \frac{1}{k}, and

A(r,c)={r22 ⁣(π22arcsincr)c ⁣(r2c2c)+cr2c2cr2c21see belowif rc2,0otherwise.A(r, c) = \begin{cases} \displaystyle\frac{r^2}{2}\!\left(\frac{\pi}{2} - 2\arcsin\frac{c}{r}\right) - c\!\left(\sqrt{r^2 - c^2} - c\right) + c\sqrt{r^2 - c^2} - \frac{c\sqrt{r^2-c^2}}{1}\,\bigg|_{\text{see below}} & \text{if } r \ge c\sqrt{2}, \\[6pt] 0 & \text{otherwise}. \end{cases}

Proof. The winning condition k(ka+1)2+(kb+1)2<12|k - \sqrt{(ka+1)^2+(kb+1)^2}| < \frac{1}{2} is equivalent to

(k12)2(ka+1)2+(kb+1)2<(k+12)2.\left(k - \tfrac{1}{2}\right)^2 \le (ka+1)^2 + (kb+1)^2 < \left(k + \tfrac{1}{2}\right)^2.

Substituting x=a+1/kx = a + 1/k, y=b+1/ky = b + 1/k, the unit square [0,1]2[0,1]^2 in (a,b)(a,b) maps to [c,1+c]2[c, 1+c]^2 where c=1/kc = 1/k, and the condition becomes r12x2+y2r22r_1^2 \le x^2 + y^2 \le r_2^2, an annular region.

Since r2=1+12k<1+cr_2 = 1 + \frac{1}{2k} < 1 + c and r1<r2r_1 < r_2, the outer disk lies within the square’s upper bounds. The binding constraints are only xcx \ge c and ycy \ge c.

Define A(r,c)=Area{(x,y):xc,yc,x2+y2r2}A(r, c) = \text{Area}\{(x,y) : x \ge c,\, y \ge c,\, x^2 + y^2 \le r^2\}. For rc2r \ge c\sqrt{2} (so the disk intersects the first-quadrant region beyond (c,c)(c,c)):

A(r,c)=cr2c2 ⁣(r2x2c)dx.A(r, c) = \int_c^{\sqrt{r^2-c^2}} \!\bigl(\sqrt{r^2 - x^2} - c\bigr)\,dx.

Evaluating the integral using r2x2dx=xr2x22+r22arcsinxr+C\int \sqrt{r^2-x^2}\,dx = \frac{x\sqrt{r^2-x^2}}{2} + \frac{r^2}{2}\arcsin\frac{x}{r} + C:

A(r,c)=[xr2x22+r22arcsinxrcx]x=cx=r2c2.A(r,c) = \left[\frac{x\sqrt{r^2-x^2}}{2} + \frac{r^2}{2}\arcsin\frac{x}{r} - cx\right]_{x=c}^{x=\sqrt{r^2-c^2}}.

At x=r2c2x = \sqrt{r^2 - c^2}: r2x2=c\sqrt{r^2-x^2} = c, so the first term gives cr2c22\frac{c\sqrt{r^2-c^2}}{2}, the second gives r22arcsinr2c2r\frac{r^2}{2}\arcsin\frac{\sqrt{r^2-c^2}}{r}, and the third gives cr2c2-c\sqrt{r^2-c^2}.

At x=cx = c: r2c2\sqrt{r^2-c^2} appears in the first term as cr2c22\frac{c\sqrt{r^2-c^2}}{2}, the second gives r22arcsincr\frac{r^2}{2}\arcsin\frac{c}{r}, and the third gives c2-c^2.

Combining:

A(r,c)=r22 ⁣(arcsinr2c2rarcsincr)cr2c2+c2.A(r,c) = \frac{r^2}{2}\!\left(\arcsin\frac{\sqrt{r^2-c^2}}{r} - \arcsin\frac{c}{r}\right) - c\sqrt{r^2-c^2} + c^2.

Using arcsinr2c2r=π2arcsincr\arcsin\frac{\sqrt{r^2-c^2}}{r} = \frac{\pi}{2} - \arcsin\frac{c}{r}:

A(r,c)=r22 ⁣(π22arcsincr)cr2c2+c2.A(r,c) = \frac{r^2}{2}\!\left(\frac{\pi}{2} - 2\arcsin\frac{c}{r}\right) - c\sqrt{r^2-c^2} + c^2.

The probability is Pk=A(r2,c)A(r1,c)P_k = A(r_2, c) - A(r_1, c), and the expected score is E=k=1NkPkE = \sum_{k=1}^{N} k \cdot P_k. \quad\square

Editorial

Albert picks a,b uniformly in [0,1]. Computes sqrt((ka+1)^2+(kb+1)^2), rounds to nearest integer. Scores k if result = k, else 0. Find expected total for k=1..100000, to 5 decimals. Winning condition: (k-1/2)^2 <= (ka+1)^2 + (kb+1)^2 < (k+1/2)^2 Substituting x=a+1/k, y=b+1/k: circle centered at origin, x in [1/k, 1+1/k], y same. Inner radius r1 = (k-1/2)/k, outer radius r2 = (k+1/2)/k. Area of {x>=c, y>=c, x^2+y^2 <= r^2} where c=1/k: For r^2 >= 2c^2: integral_c^sqrt(r^2-c^2) (sqrt(r^2-x^2) - c) dx = [xsqrt(r^2-x^2)/2 + r^2/2 * arcsin(x/r)] from c to sqrt(r^2-c^2) - c(sqrt(r^2-c^2)-c) Uses mpmath for sufficient precision.

Pseudocode

    total = 0.0
    For k from 1 to N:
        c = 1.0 / k
        r1 = (2*k - 1) / (2.0 * k)
        r2 = (2*k + 1) / (2.0 * k)
        P_k = area(r2, c) - area(r1, c)
        total += k * P_k
    Return round(total, 5)

    If r < c * sqrt(2) then
        Return 0.0
    Return (r^2 / 2) * (pi/2 - 2*arcsin(c/r)) - c*sqrt(r^2 - c^2) + c^2

Complexity Analysis

  • Time: O(N)O(N) where N=100000N = 100000. Each iteration involves O(1)O(1) floating-point operations (including arcsin\arcsin and \sqrt{\cdot}).
  • Space: O(1)O(1).

Answer

157055.80999\boxed{157055.80999}

Code

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

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

typedef long double ld;

// Area of {x >= c, y >= c, x^2+y^2 <= r^2}
// = integral_c^sqrt(r^2-c^2) (sqrt(r^2-x^2) - c) dx
// = [x*sqrt(r^2-x^2)/2 + r^2/2*arcsin(x/r)]_c^sqrt(r^2-c^2) - c*(sqrt(r^2-c^2)-c)
ld circle_quad_area(ld r, ld c) {
    if (r * r < 2 * c * c) return 0.0L;

    ld x_max = sqrtl(r * r - c * c);

    auto F = [&](ld x) -> ld {
        return x * sqrtl(r * r - x * x) / 2 + r * r / 2 * asinl(x / r);
    };

    ld area = F(x_max) - F(c) - c * (x_max - c);
    return max(area, (ld)0);
}

int main() {
    ld total = 0;
    for (int k = 1; k <= 100000; k++) {
        ld c = 1.0L / k;
        ld r1 = (k - 0.5L) / k;
        ld r2 = (k + 0.5L) / k;

        ld a2 = circle_quad_area(r2, c);
        ld a1 = circle_quad_area(r1, c);
        ld area = a2 - a1;

        total += k * area;
    }
    cout << fixed << setprecision(5) << (double)total << endl;
    return 0;
}