Problem 99

Arranged probability

Problem 101

Arranged probability

Problem 100

If a box contains twenty-one 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 eighty-five blue discs and thirty-five red discs.
By finding the first arrangement to contain over 1012 = 1,000,000,000,000 discs in total, determine the number of blue discs that the box would contain.
from math import sqrt


def run(limit=1e12):
    found = 0
    m = 1e6

    while not found:
        n1 = sqrt(2 * m * m + 1) - m
        n2 = sqrt(2 * m * m - 1) - m
        if n1 == round(n1):
            if 2 * m * n1 > limit:
                found = (m * m + n1 * n1 + 1) / 2
        elif n2 == round(n2):
            if m * m - n2 * n2 > limit:
                found = (m * m + n2 * n2 + 1) / 2
        m += 1

    return round(found)