The cow and chicken problem is a traditional problem for introducing young students to the concept of systems of equations. It goes as follows:
A farmer raises cows and chickens on his farm. His animals have a total of 24 heads and 68 legs. How many cows and how many chickens does he have?
The numbers of heads
h and legs
l can vary, but the idea of the problem is the same. This problem can't be solved by plugging in a one-variable formula; both variables (the number of cows
k, and the number of chickens
x) will always be involved in each formula that you can derive directly.
That being said, for this version of the problem, there is a formula
k = l/2 - h that you can readily use to obtain the solution of
10 cows and
14 chickens, so it's not too interesting.
Here's a slightly different problem, this time involving cows and chickens who can have a different number of heads and legs.
Your task is to build a program that takes the following inputs (all positive integers):
- hk, the number of heads a cow has.
- lk, the number of legs a cow has.
- hx, the number of heads a chicken has.
- lx, the number of legs a chicken has.
- h, the total number of heads.
- l, the total number of legs.
: and output
x, how many cows and chickens the farmer has.
However, depending on the input given, the problem has several states of solvability:
- There is a solution in the nonnegative integers. The input
1 4 1 2 24 68(which is the problem above) has the solution
- There is no solution in the nonnegative integers, but there is one in the nonnegative fractional numbers. The input
1 4 1 2 24 67is an example of this, with a solution of
- There is no solution in the nonnegative numbers, but one in the integers in general. The input
1 4 1 2 24 36is an example of this, with a solution of
- There is no solution in either the nonnegative fractional numbers or the integers in general, but one in the real numbers in general. The input
1 4 1 2 24 97is an example of this, with a solution of
- There is no solution at all. The input
2 4 1 2 24 68is an example of this.
- There are multiple solutions in the positive integers (and therefore infinitely many solutions in the real numbers). The input
2 4 1 2 24 48is an example of this. If there is exactly one solution in the positive integers but infinitely many solutions in the real numbers (as is the case with the input
6 9 4 6 10 15with a solution of
1 1), your solution can treat it as a case of this state.
Your program, given the inputs, must first output which state of solvability the problem has. Only if the problem is in the first state of solvability should you output how many cows and chickens there are, as any other solution is considered an "invalid problem".
The shortest code to do all of the above in any language wins.