Just in case the title is a "Three Stooges" reference: Hey Moe!
b(b-1)/(b+r)(b+r-1) = 1/2
2*(b(b-1) + r(r-1)) = (b+r)(b+r-1)
2b^2 - 2b + 2r^2 - 2r = b^2 + r^2 + 2br - b - r
b^2 + r^2 - (b+r) = 2br
b^2 - (2r+1)b + r^2 - r = 0
b = ( (2r+1) ± sqrt( 4r^2 + 4r + 1 - 4r^2 + 4r))/2
b = ( (2r+1) ± sqrt( 8r + 1))/2
Solution to Part 1: R and B are adjacent triangular numbers
-----------
part 2
2*(b(b-1) + r(r-1)) = (b+r+1)(b+r)
2b^2 - 2b + 2r^2 - 2r = b^2 + r^2 + 2br + b + r
b^2 + r^2 - 3(b+r) = 2br
b^2 - (2r+3)b + r^2 - 3r = 0
b = ( (2r+3) ± sqrt( 4r^2 + 12r + 9 - 4r^2 + 12r))/2
b = ( (2r+3) ± sqrt( 24r + 9))/2
part one
0 1 0
1 3 0.5
3 6 0.5
6 10 0.5
10 15 0.5
15 21 0.5
21 28 0.5
28 36 0.5
36 45 0.5
45 55 0.5
55 66 0.5
66 78 0.5
78 91 0.5
91 105 0.5
105 120 0.5
120 136 0.5
136 153 0.5
153 171 0.5
171 190 0.5
190 210 0.5
part two
0 3 0.5
3 9 0.5
9 18 0.5
18 30 0.5
30 45 0.5
45 63 0.5
63 84 0.5
84 108 0.5
108 135 0.5
135 165 0.5
165 198 0.5
198 234 0.5
Solution to Part 2: R and B are adjacent members of the sequence
3n(n+1)/2
"""
def b(r):
ans = ( (2*r+1) + ( 8*r + 1)**.5 )/2
if ans%1 == 0:
ans = int(ans)
return ans
def g(r):
ans = ( (2*r+3) + ( 24*r + 9)**.5 )/2
if ans%1 == 0:
ans = int(ans)
return ans
def prob(r,b,g):
""" if there are r,g,b marbles, what are the odds of picking
2 s.t. both are the same color """
marbles = 'R'*r + 'B'*b + 'G'*g
from itertools import combinations
same = 0
diff = 0
for comb in combinations(marbles,2):
if comb[0] == comb[1]:
same += 1
else:
diff += 1
try:
ratio = same/(same+diff)
except ZeroDivisionError:
return 0
return ratio
print('part one')
for r in range(200):
if b(r)%1 == 0:
print(r, b(r), prob(r,int(b(r)),0))
print('\n','part two')
for r in range(200):
if g(r)%1 == 0:
print(r, g(r), prob(r,int(g(r)),1))