Arranged Probability (100)
In Problem 100, we're asked to work with probabilities and combinatorics.
In a box that contains
In the example, they take
We also get exactly 1/2 when one takes
Rearranging the equation
We can rearrange the equation a little bit such that we don't have to divide:
We can also introduce
This equation looks pretty symmetric, except for the 2 on the left side. That's interesting.
We now want to find the smallest
Triangle numbers
The
We can shift the index and then we introduce
With that, the problem boils down to finding the smallest triangle number
I have done some research on triangle numbers, but it seems that these don't help us so much here.
Pell's equation
What we can do instead is multiply the equation by 4:
Then we complete the square:
Finally we introduce the new variables
That is the negative Pell's equation. We have the case
Instead of using the continued fractions and their convergents, we can actually start with the fundamental solution of
that gives us all other solutions.
The algorithm then becomes extremely simple:
- Start with
and . - Loop …
- Compute
and . - If
, return . - Apply the recursion relation.
- Compute
Formulating the solution in this way removes the need to square any of the numbers and hence we can use 64-bit integers without overflow.