We have a question here that belongs on stackoverflow, but there isn't a choice in the close box to migrate it. Seems like this has come up before, but it would be nice to have. I searched, but couldn't find this question.
Just flag it for moderator attention; we can migrate questions to any site. We should clear the migration with the SO mods anyway. I will handle this one now :-)
The mods on SO said this looks like a better fit either on Programmers.SE or CompSci.SE. It depends on whether Ravi Joshi wants a design/pseudocode answer (go to Programmers.SE) or a mathematical answer (go to CompSci.SE).
Because the answers here are not a good fit for Programmers.SE, it might be better simply to ask it again there instead of migrating with answers.
However, it seems that the answer by Dilip Sarwate may satisfy Ravi. In which case, there is no need to migrate.
Outcome: Computer Science has taken it.