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...
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 is , where , , , and
Proof. The winning condition is equivalent to
Substituting , , the unit square in maps to where , and the condition becomes , an annular region.
Since and , the outer disk lies within the square’s upper bounds. The binding constraints are only and .
Define . For (so the disk intersects the first-quadrant region beyond ):
Evaluating the integral using :
At : , so the first term gives , the second gives , and the third gives .
At : appears in the first term as , the second gives , and the third gives .
Combining:
Using :
The probability is , and the expected score is .
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: where . Each iteration involves floating-point operations (including and ).
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Project Euler Problem 285: Pythagorean Odds
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
= [x*sqrt(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.
"""
from mpmath import mp, mpf, sqrt, asin
mp.dps = 25
def circle_quad_area(r, c):
"""Area of {x >= c, y >= c, x^2+y^2 <= r^2}."""
if r * r < 2 * c * c:
return mpf(0)
x_max = sqrt(r * r - c * c)
def F(x):
return x * sqrt(r * r - x * x) / 2 + r * r / 2 * asin(x / r)
return F(x_max) - F(c) - c * (x_max - c)
total = mpf(0)
for k in range(1, 100001):
c = mpf(1) / k
r1 = (mpf(2) * k - 1) / (2 * k)
r2 = (mpf(2) * k + 1) / (2 * k)
total += k * (circle_quad_area(r2, c) - circle_quad_area(r1, c))
print(f"{float(total):.5f}")