Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. This evening, I found a nice probabilistic puzzle on http://www.futilitycloset.com/A bag contains 16 billiard balls, some white and some black. You draw
two balls at the same time. It is equally likely that the two will be
the same color as different colors. What is the proportion of colors
within the bag?

To be honest, I did not understood the answer on the blog, but if we write it down, we want to solve Let us count: if is the total number of balls, and if is the number of white balls then I.e. we want to solve a polynomial equation (of order 2) in , or to be more precise, in  If is equal to 16, then is either 6 or 10. It can be visualized below

`> balls=function(n=16){+ NB=rep(NA,n)+ for(k in 2:(n-2)){+ NB[k]=(k*(k-1)+(n-k)*(n-k-1))+ }+ k=which(NB==n*(n-1)/2)+ if(length(k)>0){+ plot(1:n,NB,type="b")+ abline(h=n*(n-1)/2,col="red")+ points((1:n)[k],NB[k],pch=19,col="red")}+ return((1:n)[k])}> balls()  6 10` But more generally, we can seek other ‘s and other pairs of solutions of
such a problem. I am not good in arithmetic, so let us run some codes.
And what we get is quite nice: if admits a pair of solutions, then is the squared of another integer, say . Further, the difference between and is precisely . And will be one of the answers when the total number of balls will be . Thus, recursively, it is extremely simple to get all possible answers. Below, we have , , and the difference between and ,

`> for(s in 4:1000){+ b=balls(s)+ if(length(b)>0) print(c(s,b,diff(b)))+ } 9 3 6 3 16  6 10  4 25 10 15  5 36 15 21  6 49 21 28  7 64 28 36  8 81 36 45  9 100  45  55  10 121  55  66  11 144  66  78  12 169  78  91  13 196  91 105  14 225 105 120  15 256 120 136  16 289 136 153  17 324 153 171  18 361 171 190  19 400 190 210  20 441 210 231  21 484 231 253  22 529 253 276  23 576 276 300  24 625 300 325  25 676 325 351  26 729 351 378  27 784 378 406  28 841 406 435  29 900 435 465  30 961 465 496  31`

Thus, given , consider an urn with balls. We draw two balls at the same time. It is equally likely that the two will be the same color as different colors. Then the number of colors within the bag are respectively Finally, observe that the ‘s are well known, from Pascal’s triangle, also known as triangular numbers, Maths can be magic, sometimes…