6
2
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 k
and 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 solution10 14
. - There is no solution in the nonnegative integers, but there is one in the nonnegative fractional numbers. The input
1 4 1 2 24 67
is an example of this, with a solution of9.5 14.5
. - There is no solution in the nonnegative numbers, but one in the integers in general. The input
1 4 1 2 24 36
is an example of this, with a solution of-6 30
. - 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 97
is an example of this, with a solution of24.5 -0.5
. - There is no solution at all. The input
2 4 1 2 24 68
is 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 48
is 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 input6 9 4 6 10 15
with a solution of1 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.
1
must first output which state of solvability the problem has first
This is the hardest part; I've been trying for a while and I still can't even think of a way to start doing it :P – Doorknob – 2013-08-22T14:42:14.213@primo No, it's not. Note that
2 4 1 2
means that there are always twice as much legs as heads, no matter how many cows or chicken are involved. Thus there is no way to have 24 heads but 68 legs. – Howard – 2013-08-23T07:41:47.897Sorry, I had my lk and lx switched. – primo – 2013-08-23T07:42:16.237
1
See also this problem over the rationals.
– Peter Taylor – 2013-08-27T10:16:05.013