An old Chinese poem goes:


When translated into English, this math problem reads:

100 buns and 100 monks,
Big monks eat 3 buns with no shortage,
Little monks share one bun among three,
Big and little monks, how many are there?

Once again, like in the cow and chicken problem, there is no simple calculation you can perform. You need the power of systems of equations.

Your task is to build a program that takes the following inputs, all positive integers:

  • b, the number of buns.
  • m, the number of monks.
  • bl, the number of buns a big monk can eat.
  • ms, the number of small monks who share one bun.

and output l and s, the number of big monks and small monks respectively. ("L" stands for "large").

Again, this problem has various states of solvability, but this time around, only output something if there is a unique solution in the nonnegative integers.

The shortest code to do all of the above in any language wins.

The problem input of the poem is 100 100 3 3, and the solution is 25 75.


R: 81 characters


A brute force solver which takes all possible combinations of non-negative integers and subset the one corresponding to criteria.


F#: 125 characters

printf"%O"(Console.ReadLine().Split()|>Seq.map Int32.Parse|>Seq.toArray|>fun[|b;m;x;y|]->((m-b*y)/(1-x*y),m-(m-b*y)/(1-x*y)))

This is my first attempt at code golf (here anyway), so I'm sure this could be improved. It simply reads the input, parses it, then does a little number crunching to get the correct results.

The result will be formatted like this:

(25, 75)

Not the answer you're looking for? Browse other questions tagged or ask your own question.