Build a solver for the cow and chicken problem 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 solution 10 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 of 9.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 of 24.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 input 6 9 4 6 10 15 with 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.
Mathematica 359 314
About 200 characters went into determining which condition applies.
f@{a_, b_, c_, d_, i_, l_} :=
 Module[{s, t, z = &quot;invalid problem&quot;, y = IntegerQ},
  t@n_ := y@n \[And] (n &gt; 0);
  p@n_ := n \[Element] Rationals \[And] n &gt; 0;
  q@n_ := n \[Element] Reals;
  w = Flatten@Quiet@Solve[{d*x + b*k == l, c*x + a*k == i}, {k, x}];
  s = {k, x} /. w;
  Which[w == {}, {5, z},
   Length[w] != 2, {6, z},
   True, If[And @@ (t /@ s), {1, s},
    Which[
     And @@ (p /@ s), {2, z},
     And @@ (y /@ s), {3, z},
     And @@ (q /@ s), {4, z},
     3 == 3, {7, z}]]]]


f[{1, 4, 1, 2, 24, 68}]
f[{7, 14, 9, 6, 25, 18}]
f[{1, 4, 1, 2, 24, 67}]
f[{1, 4, 1, 2, 24, 36}]
f[{1, 4, 1, 2, 24, 97}]
f[{2, 4, 1, 2, 24, 68}]
f[{2, 4, 1, 2, 24, 48}]
f[{1, 4, 1, 2, -23, (3 + I)^2/(5 - I)}]
f[{6, 9, 4, 6, 10, 15}]


{1, {10, 14}}
{2, &quot;invalid problem&quot;}
{2, &quot;invalid problem&quot;}
{3, &quot;invalid problem&quot;}
{4, &quot;invalid problem&quot;}
{5, &quot;invalid problem&quot;}
{6, &quot;invalid problem&quot;}
{7, &quot;invalid problem&quot;}
{6, &quot;invalid problem&quot;}  

How it works
The  equations d*x+b*k==land c*x+a*k==i express the constraints in the general cow and chickens problem,where

a is &quot;the number of heads per cow&quot;
b is &quot;the number of legs per cow&quot;
c is &quot;the number of heads per chicken&quot;
d is &quot;the number of legs per chicken&quot;
i is &quot;the number of  heads&quot;
l is &quot;the number of legs&quot;
k is &quot;the number of cows&quot;
x is &quot;the number of chickens&quot;.

Solve returns the real - valued solutions, if any exist, for the cows and chickens, as a list of 2 substitution rules, e.g.
{k-&gt;10, x-&gt; 14}
If no solutions exist, an empty list is returned.
If the data cannot be processed, Solve will return a different number of elements.
The tests are performed in the following order.

If there are 0 solutions, condition 5 is satisfied

If the list of &quot;solution&quot;s has anything other than 2 elements, then condition 6 is satisfied .

If there are 2 solutions, then

if function t yields True, the solution is a pair of non-negative integers, and condition 1 is satisfied.
if function p yields True, the solution is a pair of  non negative rationals and condition 2 is satisfied.
if function q yields True, the solution is a pair of  non negative rationals and condition 3 is satisfied.
if function y yields True, the solution is a pair of  integers and condition 4 is satisfied.
Otherwise, condition 7 is satisfied.



Minor Variant of f
g@{a_, b_, c_, d_, i_, l_} := 
 Module[{s, t, z = &quot;invalid problem&quot;, y = IntegerQ}, 
  t@n_ := y@n \[And] (n &gt; 0);
  p@n_ := n \[Element] Rationals \[And] n &gt; 0;
  q@n_ := n \[Element] Reals;
  w = Flatten@Quiet@Solve[{d*x + b*k == l, c*x + a*k == i}, {k, x}];
  s = {k, x} /. w;
  Which[w == {}, {5, z, w}, Length[w] != 2, {6, z, w}, True, 
   If[And @@ (t /@ s), {1, s, w}, 
    Which[And @@ (p /@ s), {2, z, w}, And @@ (y /@ s), {3, z, w}, 
     And @@ (q /@ s), {4, z, w}, 3 == 3, {7, z, w}]]]]

g is f` with the additional property that it returns the output of Solve.
g[{1, 4, 1, 2, 24, 68}]
g[{7, 14, 9, 6, 25, 18}]
g[{1, 4, 1, 2, 24, 67}]
g[{1, 4, 1, 2, 24, 36}]
g[{1, 4, 1, 2, 24, 97}]
g[{2, 4, 1, 2, 24, 68}]
g[{2, 4, 1, 2, 24, 48}]
g[{1, 4, 1, 2, -23, (3 + I)^2/(5 - I)}]
g[{6, 9, 4, 6, 10, 15}]


{1, {10, 14}, {k -&gt; 10, x -&gt; 14}}
{2, &quot;invalid problem&quot;, {k -&gt; 1/7, x -&gt; 8/3}}
{2, &quot;invalid problem&quot;, {k -&gt; 19/2, x -&gt; 29/2}}
{3, &quot;invalid problem&quot;, {k -&gt; -6, x -&gt; 30}}
{4, &quot;invalid problem&quot;, {k -&gt; 49/2, x -&gt; -(1/2)}}
{5, &quot;invalid problem&quot;, {}}
{6, &quot;invalid problem&quot;, {x -&gt; 24 - 2 k}}
{7, &quot;invalid problem&quot;, {k -&gt; 615/26 + (19 I)/26, x -&gt; -(1213/26) - (19 I)/26}}
{6, &quot;invalid problem&quot;, {x -&gt; 5/2 - (3 k)/2}}

Problems falling under condition 6 have multiple solutions, as evidence by the equations.  The problem that falls under condition 5 returns an empty set (no solutions).
Note: element and and are single characters in Mathematica.