import React, { useMemo } from 'react'; const StrategyComparison: React.FC = () => { const data = useMemo(() => { const points = []; const maxN = 100; for (let n = 5; n <= maxN; n += 5) { // Naive strategy const naiveProb = Math.pow(0.5, n); // Loop strategy: 1 - sum(1/k for k from n/2+1 to n) let sum = 0; for (let k = Math.floor(n / 2) + 1; k <= n; k++) { sum += 1 / k; } const loopProb = 1 - sum; points.push({ n, naiveProb, loopProb }); } return points; }, []); const chartWidth = 700; const chartHeight = 350; const padding = { top: 20, right: 100, bottom: 50, left: 70 }; const width = chartWidth - padding.left - padding.right; const height = chartHeight - padding.top - padding.bottom; // Only show loop strategy on linear scale (naive is too small to see) const maxProb = 0.5; const getY = (prob: number) => { return height - (prob / maxProb) * height; }; const getX = (n: number) => { return (n / 100) * width; }; // Create paths for both strategies const loopPath = data.map((point, i) => { const x = getX(point.n); const y = getY(point.loopProb); return `${i === 0 ? 'M' : 'L'} ${x} ${y}`; }).join(' '); // For naive, we'll just show it near zero const naivePath = data.map((point, i) => { const x = getX(point.n); const y = height - 2; // Near the bottom (representing ~0) return `${i === 0 ? 'M' : 'L'} ${x} ${y}`; }).join(' '); return (
Each prisoner randomly selects 50 boxes.
n=10: P ≈ {(Math.pow(0.5, 10) * 100).toExponential(2)}%
n=20: P ≈ {(Math.pow(0.5, 20) * 100).toExponential(2)}%
n=50: P ≈ {(Math.pow(0.5, 50) * 100).toExponential(2)}%
n=100: P ≈ {(Math.pow(0.5, 100) * 100).toExponential(2)}%
Essentially impossible for any reasonable n!
Each prisoner follows the cycle starting at their number.
n=10: P ≈ {calcProb(10)}%
n=20: P ≈ {calcProb(20)}%
n=50: P ≈ {calcProb(50)}%
n=100: P ≈ {calcProb(100)}%
> ); })()}Converges to 1 - ln(2) ≈ 30.69% as n → ∞
Key Insight: The loop strategy transforms an impossible problem (probability ≈ 10-30) into a reasonable one (probability ≈ 31%). This astronomical improvement comes from exploiting the mathematical structure of permutations rather than relying on independent random trials.