This riddle involves understanding the meaning of the phrase "all but two" in relation to the total number of animals.
Let T represent the total number of animals the farmer has.
Let C be the number of cows, H be the number of horses, and P be the number of pigs.
The statement "all but two were cows" means the number of animals that are *not* cows is 2. The animals that are not cows are horses and pigs. So:
Similarly, "all but two were horses" means the number of animals that are *not* horses is 2. These are cows and pigs.
And "all but two were pigs" means the number of animals that are *not* pigs is 2. These are cows and horses.
We now have a system of three equations:
From equations (1) and (2), since both equal 2:
$ H + P = C + P $
Subtracting P from both sides gives $ H = C $.
From equations (2) and (3), since both equal 2:
$ C + P = C + H $
Subtracting C from both sides gives $ P = H $.
Therefore, we have $ C = H = P $.
Now substitute $ H = C $ into equation (3):
$ C + C = 2 $
$ 2C = 2 $
$ C = 1 $
Since $ C = H = P $, it means $ H = 1 $ and $ P = 1 $.
The total number of animals (T) is the sum of cows, horses, and pigs:
$ T = C + H + P $
$ T = 1 + 1 + 1 $
$ T = 3 $
The farmer had a total of 3 animals.