A drawer contains a mixture of red socks and blue socks, at most 2014 in total. It so happens that, when two socks are selected randomly without replacement, there is a probability of precisely 2/3 that both are red or both are blue.
What is the maximum possible number of red socks in the drawer that is consistent with this data?
Let R be the number of red socks and B be the number of blue socks. There are R*(R-1)/2 ways to draw a pair of red socks; there are B*(B-1)/2 ways to draw a pair of blue socks; and there are R*B ways to draw a mismatched pair of socks.
Then to satisfy the 2/3 chance to draw a matching pair we must have R*(R-1)/2 + B*(B-1)/2 = 2*R*B. Playing around with the equation I eventually transformed it into 3*(R-B)^2 = (R+B+1)^2 - 1.
Now I'll take the square root of each side to have sqrt(3)*(R-B) = R+B+1-delta, for some value 0<delta<1. (This is true since the right side is only one less than a square so its square root is only some small value less than an integer.) Then I can apply the floor function to create the approximation Floor(sqrt(3)*(R-B)) = R+B.
Now it will easy to create a rather quick brute force search with just a single loop instead of a double loop like in Charlie's program. Just loop through values of R-B up to 2014/sqrt(3)~=1163, calculate R+B via the approximation and check to see if they satisfy the earlier equation.
Whichever way you go, there are five pairs (R-B,R+B) in the range: (4,6), (15,25), (56,96), (209,361), (780,1350). We want the largest, so R-B=780 and R+B=1350, which yields R=1065 and B=285. So the maximum possible number of red socks in the drawer is 1065.