Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js
cover image for post 'Project Euler Problem 436 - Unfair Wager'

Project Euler Problem 436 - Unfair Wager

Spoiler Alert! This blog entry gives away the solution to problem 436 of Project Euler. Please don’t read any further if you have yet to attempt to solve the problem on your own. The information is intended for those who failed to solve the problem and are looking for hints or test data to help them track down bugs. It is posted not before the problem has a hundred solvers already.

First I provide references of the major theoretical background that you probably need to know to solve the problem. The last section presents the solution from start to finish.

The post reflects my approach to the problem. Even though the final outcome was accepted by Project Euler doesn’t mean the information is correct or elegant. Algorithms won’t necessarily be the most efficient ones, but they are guaranteed to run within the time limit of one minute.

References

This is a pen and paper problem that requires very little knowledge of maths. You mainly need to calculate conditional probabilities and use the distribution of the sum of uniform variables.

Notation

I’ll use the following notation:

  • ui: The ith uniform number.
  • si: The sum after i draws
  • n: The number of draws to get the sum over 1
  • m: The number of draws to get the sum over 2
  • p: The first sum over 1, i.e. p=sn
  • x: The number that the first player gets.
  • y: The number that the second player gets.

The capital letters denote the corresponding random variables and fX(x) denotes the probability density function of X at x .

Solution

I use the following steps to solve the problem:

  1. Sum after i picks
  2. First Player – Distribution of x
  3. Second Player – Distribution of y
  4. Joint distribution of x and y
  5. Probability of y bigger than x

Sum after i picks

Let

Si(s)=i1UiUiU(0,1)

The probability density function (pdf) for the sum after one step corresponds to the pdf of the continuous uniform distribution:

fS1(s)=1[0,1]

where 1A denotes the indicator function.

For each subsequent pick, the sum Sn is obtained by adding a uniform random variable to Sn1 . The uniform random variable is independent of the sum Sn1 , and therefore the pdf of Sn is obtained by convolution of fSn1 and fU :

fSn(s)=fSn1+U(s)=(fSn1fU)(s)=fSn1(t)fU(st)dt=0fSn1(t)1dt

By induction we get
fSn(s)={sn1(n1)!0s1

(We will only need the distribution below one).

First Player – Distribution of x

Let n denote the number of picks it takes to get the sum over 1. Consequently, sn is the first sum over 1, i.e., the sum where the second player takes over. The last summand of sn is the number x(n) that the first player gets if she draws n numbers:

sn1+x(n)=snsn11sn>1

Because

sn1=snx(n)

and because sn11 and sn>1 , we know that

1<sn1+x(n)

so the joint probability density distribution of x(n) and Sn is

fX(n),Sn(x,s)={fSn1(sx)1<s1+x0otherwise={(sx)n2(n2)!1<s1+x0otherwise

The previous density applies to a given n . Let x be the number of the first player, and let p be the first sum over 1. To get the unconditional probability fP,X, we sum up fX(n),Sn(x,s) (2) over all potential n :

fP,X(p,x)=n=2(px)n2(n2)!1<p1+x=n=0(px)nn!1<p1+x=epx1<p1+x

Second Player – Distribution of y

The first player leaves the sum at p an the second player needs to get the sum over 2. I prefer to shift the values to the point of origin: Let the second player start at 0 and let y be the first value over 2p . Also let si be the sum of the first i number that the second player picked. In other words, if the first player picked n numbers that add up to sn , and the second player picks m numbers that bring the total to sn+m , then sm=sn+msn .

We already know the distribution of si below 2p from the first player from Equation 1:

$$ \begin{equation} f_{S’i}(s) = \frac{s^{n-1}}{(n-1)!} \mathbf{1}{[0,2-p]} \end{equation} $$

To get to the distribution of y we have to distinguish two fundamentally different cases.

Case 1: y is less or equal than 2-p

If y is smaller than 2p , then the second player needed at least two picks to get the sum over 2p , and the penultimate sum is at greater than 2py (since adding y brings the sum over 2p ) and at most 2p (because if the sum would be bigger than that, it would be the last sum already).

Assume that the second player needs m>1 picks and his last number is y(m) . Therefore, we have

fY(m)|P,y>2p,m2(y)=2p2pyfSm1ds=2p2pysn2(s2)!ds=sm1(m1)!|2p2py=(2p)m1(m1)!(2py)m1(m1)!

Hence, the pdf of y is:

fY|P,y>2p(y)=m=2(2p)m1(m1)!(2py)m1(m1)!=m=1(2p)mm!m=1(2py)mm!=e2pe2py

Case 2: y is larger than 2-p

The second case is special, because this time the second player has a chance to get her number with the first pick. We can cover this case by defining

fS0(s):=δ(s)

which means the sum of zero uniform random value is always 0 and the corresponding pdf is 1 at 0, and 0 for any other value.

Secondly, the lower bound of the integration needs to be adjusted: since 2py is always smaller than zero, and the pdfs of Sn are zero for negative values, we start integrating at 0.

The rest of the calculation is the same, except this time we include m=1 :
As before, let’s assume that the second player needs m>1 picks and his last number is y(m) . Therefore, we have

fY(m)|P,y2p,m1(y)=2p0fSm1ds={2p0sn2(s2)!dsm22p0δ(s)dsm=1=1+(sm1(m1)!|2p0)=(2p)m1(m1)!

Hence, the pdf of y is:

fY|P,y2p=m=1(2p)m1(m1)!=m=0(2p)mm!=e2p

So all in all the pdf of y given P is:

fY|P={e2pe2pyy<2pe2py2p=e2pe2py1[0,2p[(y)

Joint distribution of x and y

The joint distribution of the picks of the first and second player is:

fX,Y(x,y)=1+x1fX,P(x,p)fY|P(y|p)dp=1+x1epx(e2pe2py1[0,2p[(y))dp

Since L1+x we have

2p2(1+x)=1x

The indicator is therefore always one if y<1x and

fX,Y|Y<1X(x,y|y<1x)=1+x1epx(e2pe2py)dp=e2xy(1+ey)p|1x1=e2xy(1+ey)x

If, on the other hand, x+y0 , then the indicator is only one if L<2y :

fX,Y|Y1X(x,y|y1x)=1+x1epx(e2p)dp+,,,2y1epx(e2py)dp=e2xxe2xy(1y)

And we have:

fX,Y(x,y)={xe2x(1ey)x+y<1e2xy(xey1+y)x+y1

Probability of y bigger than x

Player two wins if y greater than x :

P[yx]=101xfX,Y(x,y)dydx

First, we tackle the case x+y<1 . Since also y>x we have 0x<1/2 and therefore:

P[yx|x+y<1]=1/201xx(e2xx(1ey))dydx=18(5+40e26e)e

There are two possibilities for the case x+y1 :

  1. 0x1/2 and 1xy1
  2. 1/2x1 and xy1

Therefore,

P[yx|x+y1]=1/2011x(xe2x(1y)e2xy)dydx,,,+11/21x(xe2x(1y)e2xy)dydx=14+23e85e3/2+2e2

So finally we have from Equations 3 and 4:

P[yx]=P[yx|x+y<1]+P[yx|x+y1]=18(5+40e26e)e+14+23e85e3/2+2e2=14(1+14e5e2)

Archived Comments

Note: I removed the Disqus integration in an effort to cut down on bloat. The following comments were retrieved with the export functionality of Disqus. If you have comments, please reach out to me by Twitter or email.

euler Sep 24, 2013 06:06:46 UTC

We hope that you enjoyed solving this problem. Please do not deprive others of going through the same process by publishing your solution outside Project Euler. If you want to share your insights then please go to thread 436 in the discussion forum.

Thomas Sep 24, 2013 21:58:59 UTC

I got a good deal of the way into this myself, but I got in over my head about 65% of the way through. Your solution is elegant and I commend you.

While I was working through the math over the last week, I quickly came to the conclusion that this isn't a CS problem but a math problem. A computer isn't need, nor is an algorithm. A simple calculator can easily give this answer once you have the probability distribution equation solved. So, my question is: how does one use a computer to solve this without using the equation? A Monte Carlo simulation cannot realistically achieve more than the 7th decimal place of precision. Since it is a continuous probability distribution, brute force is also (seemingly) out of the question. Is there a pure algorithm approach or is the Euler Project just a joke on CS people by math professors?

Reginald Sep 27, 2013 18:18:45 UTC

I also tried to solve this algorithmically. The chances that the sum of more than 10 random variables is larger than 1 is neglectable, so I tried to work with fixed number of draws. It got ugly very quickly and it was a mess. If you check the forum you see a solution that does a similar thing with numerical integration.

This was a very frustrating problem because there doesn't seem to be elegant way to solve it. Even the differential equation approach kinda sucks.

Please folks at Euler give us more programming challenges! I wish there were an other website like euler but with more focus on programming.

CuongVM Oct 01, 2013 20:58:24 UTC

(sorry if my bad English annoy you :) )
can't say that I admire your work when I actually did :) I used Monte Carlo simulation for about half billion times but to produce 12 decimal digits is something that it could never reach... I first thing it could relate to Lesbeg integral but I couldn't find the way to solve it... Thanks to your work, everything is clear now!