Project Euler 100: Finding the number of blue discs for which there is 50% chance of taking two blue.
Project Euler 100 Problem Description
Project Euler 100: If a box contains twentyone coloured discs, composed of fifteen blue discs and six red discs, and two discs were taken at random, it can be seen that the probability of taking two blue discs, P(BB) = (15/21)×(14/20) = 1/2.
The next such arrangement, for which there is exactly 50% chance of taking two blue discs at random, is a box containing eightyfive blue discs and thirtyfive red discs.
By finding the first arrangement to contain over 10^{12} = 1,000,000,000,000 discs in total, determine the number of blue discs that the box would contain.
Analysis
Given b=blue discs, n=total number of discs then the general equation is:
which is, in fact, a Diophantine Quadratic Equation and can be solved using a program provided on the following website: http://www.alpertron.com.ar/QUAD.HTM
Here is the input to the program for our equation:
[ 2] x^{2} +
[ 0] xy +
[1] y^{2} +
[2] x +
[ 1] y +
[ 0] = 0
Here is the output from the program:
2x^{2} – y^{2} – 2x + y = 0
X_{0} = 0
Y_{0} = 1
and also:
X_{0} = 1
Y_{0} = 1
X_{n+1} = 3X_{n} + 2Y_{n} – 2
Y_{n+1} = 4X_{n} + 3Y_{n} – 3
From here we have two equations that can calculate b and n for each term in the series. Instead of starting at b=1, n=1 we start with the values in the problem description to save a few iterations; b=85, n=120. We continue this series until n>10^{12}.
The equations are: blue disks = 3b + 2n – 2; n disks = 4b + 3n – 3 (also n = int(b/√2/2), but that’s another story).
Project Euler 100 Solution
Runs < 0.001 seconds in Python 2.7.Use this link to get the Project Euler 100 Solution Python 2.7 source.
Afterthoughts

No afterthoughts yet.
Yes it works, but can you please explain how you derived the two linear equations to continue the series. I do not understand where those came from.
Hi Dave, I rewrote the ‘analysis’ section with more information on how to find the equations. Let me know if this answers your question.
Yes very interesting. I did not know of a way to determine solutions that are restricted to integers. The math behind that website that generated the coefficents for the iterative equations is quite impressive and I can’t say I was able to follow it yet. The way these simple iterative equations pop out that gives the answer is almost magical. Very impressive.