In 1891, retired German schoolteacher Wilhelm von Osten decided to teach his horse Hans mathematics.

Starting with basic addition and subtraction, von Osten soon realised he was on to a winner. Giving his answers by stamping his hoof, Hans proved able to do ever more complicated tasks, including multiplying, dividing, double-digit subtraction and even fractions.

As word of Hans's abilities spread across Europe, people flocked to see him. Eventually, the German board of education decided to investigate the claims, so they formed the 13-strong Hans Commission.

The commission brought in psychologist Oskar Pfungst to investigate the horse's abilities. Repeated testing showed that there was something fishy: the person asking the question had to know the answer.

Pfungst eventually showed that Hans was responding to non-verbal cues from his questioner, such as a slight move of the head or straightening of the back when they approached the right answer.

This didn't convince von Osten, who continued to tour Hans around Germany to excited crowds. But scientists interested in how animals conceive of numbers were sent back to the drawing board.

Since then we have discovered that counting is a combination of several skills, each building on the other. Among animals, its horses for courses: some only possess the simplest of abilities, while others seem to be mathematical prodigies. The question now is: why are some animals better at counting than others?

One of the most basic skills underlying the ability to count is being able to tell if one quantity is smaller or larger than another. Serengeti lions are good at this: they can tell if their group outnumbers another.

After glaring in the direction of the noise, the pride dashed into the foliage on the attack

"Lions are territorial and live in prides, family groups," says Brian Butterworth of University College London in the UK. They attack and defend against other prides, but only if they outnumber them.

Karen McComb of the University of Sussex in Brighton, UK, played Tanzanian lions simulated sounds of other prides roaring, and observed whether the pride attacked or retreated.

In one experiment when five lionesses were together, McComb played the roars of three lions. After glaring in the direction of the noise, the pride dashed into the foliage on the attack.

"Lions were extremely good at weighing up their odds of success in terms of the number of themselves versus the number roaring from a loudspeaker," says McComb.

The hyenas turned out to be particularly numerate

This was particularly impressive, as the lions were seeing their own pride while hearing their attackers. They could integrate the numerical information across both senses.

"It probably broke down at around five or six lions, because the roars starts to sound confusing," says McComb. She now has a doctoral student examining the exact limits of the lions' counting.

Researchers have since used similar experiments to test chimpanzees, monkeys and hyenas, with similar results.

The hyenas turned out to be particularly numerate. The researchers varied both the number of groans and which hyenas were making them.

With apologies to dog lovers, dogs are not the sharpest tools in the shed

The animals were able to count both the number of sounds and the number of individuals making them. "The hyenas reached their total in quite a sophisticated way," says McComb.

Even so, figuring out that one quantity is larger than another isn't quite enough. True counting means understanding "ordinality": the idea that there is a sequence where one is followed by two, two is followed by three, and so on.

On this measure, and with apologies to dog lovers, dogs are not the sharpest tools in the shed.

Krista Macpherson of the University of Western Ontario in London, Canada, tested domestic dogs. She had them watch as different quantities of food were dropped into two opaque bowls.

Wolves can discriminate larger numbers

The dogs could easily tell the difference between 1 and 0 – that is, between some food and no food at all – but they could not discriminate between numbers larger than 1.

However, wolves can discriminate larger numbers, suggesting dogs lost the ability when they were domesticated.

Clearly, dogs don't need to count. That raises the question: what do animals actually use it for?

One thing animals must do is find a mate of the right species. This isn't always easy, as related species can look very similar.

While frogs count to help them mate, bees do so to help them navigate

"Take, for example, the frog," says Butterworth. "The way you can identify your species of frog is the number of pulses in their croak."

That means female frogs need to count the number of pulses, which they can do for phrases up to 10 notes long. They also assess the croaks' duration and volume, as a measure of the male's value as a mate.

While frogs count to help them mate, bees do so to help them navigate.

A worker honeybee will fly from its hive in search of food. Once it finds some, it collects some and returns. In the 1990s, experimenters placed tent structures along bees' routes, and found that bees remembered how far they had travelled by counting the number of these landmarks between the food and the hive.

Perhaps the most skilled bird of all was Alex, an African grey parrot

"You can tell they're using that information by changing the number of landmarks and food source location," says Butterworth.

Birds are also better than most mammals when it comes to counting. If corvids are shown a display containing a certain number of objects, they can then find a display of different objects containing the same quantity.

Similarly, New Zealand robins become demonstrably upset if they feel they've been cheated out of a promised number of mealworms.

But perhaps the most skilled bird of all was Alex, an African grey parrot originally purchased at a pet store in Chicago in the 1970s. Alex, who died in 2007, was the subject of a three-decade-long experiment by animal psychologist Irene Pepperberg.

Pepperberg taught Alex skills never before thought possible for birds, including language, consciousness, mathematics and basic reading. The two formed a deep bond, often saying that they loved one another.

Alex had to understand colours and shapes as well as being able to count

When it came to counting, Alex could get up to six.

To test him, Pepperberg would place a tray with five green blocks, six green balls, four rose-coloured balls and three rose-coloured blocks. She would ask, "how many rose-coloured balls?" and Alex would normally respond with the correct answer.

To do this, Alex had to understand colours and shapes as well as being able to count. That's impressive, but Alex had decades of one-on-one training – yet he still never displayed the capabilities of some other animals.

For instance, chicks seem to be able to count pretty much from the moment they hatch out of their eggs. When just 3 days old, chicks can identify larger or smaller quantities. That's neat, but in January 2015, Rosa Rugani of the University of Padova in Italy presented evidence that chicks' counting goes far beyond that.

Rugani believes that chicks think of numbers on a "number line" running from left to right, just like most humans. This way of thinking had not been observed previously in the animal world.

Chicks associate smaller numbers with the left space and larger numbers with the right space

She showed 60 new-born chicks a card with five dots so that they became familiar with it. Later, if they were shown a card with just two dots, they walked to the left: whereas if they were shown a larger number such as 8, they walked to the right.

The chicks could still do this if the specific numbers were changed. For instance, if they were taught the number 20 and were then shown a card with 8 dots, they went to the left.

"Similarly to humans, chicks associate smaller numbers with the left space and larger numbers with the right space," Rugani wrote. But not everyone is convinced.

In June 2015, Christopher Harshaw of Indiana University in Bloomington argued that Rugani's study was flawed. He points out that newborn chicks often show biases for turning left or right, and this could have skewed the data.

For now it's unclear if chicks really can conceive of a mental number line

Samuel Shaki of Ariel University in Israel is also sceptical. In August 2015 he pointed out that chicks associate dense, complex displays with their right side, and lighter, simpler displays with the left.

Rugani might be able to convince her detractors by training the chicks first on a blank panel and then introducing the dots. But so far she is defending her original experiments.

For now it's unclear if chicks really can conceive of a mental number line. If it turns out that they can, it will suggest that the human ability to imagine a number line might be innate – and that other animals might do so too.

There is evidence that counting is also innate in primates, our closest relatives – and some of them are truly excellent at it.

Tetsuro Matsuzawa of Kyoto University in Aichi, Japan has been studying primates for almost four decades. Like Pepperberg and Alex, Tetsuro has a special relationship with Ai, a 39-year-old female chimpanzee.

The achievement made her a national hero in Japan and a global icon for her species

In the 1980s, Ai became the first animal to count using Arabic numerals: she understood symbols like "1" and "2". She learned to look at a display of dots on a computer screen, and tap on the numeric symbol for the number of dots shown.

To do so, Ai had to understand that the symbols have a value, for instance that "5" represents five physical apples. She also had to know that numbers are sequential and that each number has a place within a number line.

"The achievement made her a national hero in Japan and a global icon for her species," wrote Alex Bellos in his book Alex's Adventures in Numberland.

Chimpanzees can also go a step beyond counting. They can perform basic maths, if there is chocolate in it for them.

In 1987, researchers presented chimps with two pairs of bowls, each containing a certain amount of chocolate.

To receive the sweet reward, the chimps had to count and sum the chocolate in two bowls, compare it with the sum of another two bowls, and correctly judge which was larger. They got it right 90% of the time.

Clearly, plenty of animals can count, and some of them are startlingly good at it. So how do they do it?

In both humans and chimps, says Butterworth, the ability to count resides in a part of the brain called the neocortex. That's the outer layer of the brain, the bit covered in folds. Running along each side is a groove called the intraparietal sulcus, and it is there that we and primates do our counting.

Crows have number-specific neurons in a region known as the endbrain

But this can't be the whole explanation, because only mammals have a neocortex. Frogs count with the auditory part of their brain.

Similarly, a 2015 study found specific neurons in the brains of birds that respond to number. Helen Ditz from the University of Tübingen in Germany found that crows have number-specific neurons in a region known as the endbrain, which humans don't have.

What's more, when it comes to fish, scientists haven't got a clue how they count. But they can do it.

In 2012, Butterworth and his colleagues persuaded 200 guppies to judge which shoal they wanted to be in. Guppies prefer to swim in larger shoals, but to really test the fish's counting, the team got them to judge the shoal's size one fish at a time.

Butterworth says they evolved this ability for their safety. "If you're a small fish subject to predation, it makes sense for you to swim in a large shoal with lots of other fish."

Some zebrafish are inherently better with numbers than others

But while it seems all species can do it, there is no one part of the brain that they all use for counting.

"The same behavioural ability doesn't mean it's done in the same way," says Lars Chittka from Queen Mary, University of London, who demonstrated that honeybees can count. "It could be mediated by entirely different neuronal circuits."

If the animals that count do have something in common, it isn't in their anatomy, but it might be in their DNA. Conceivably there is a bit of shared genetic material, somehow crucial for counting, that dates back to a time when our ancestors were still fish.

Butterworth hopes to find out by studying zebrafish. Some zebrafish are inherently better with numbers than others, just like humans, and he wants to find out what separates these numerate fish at the genetic level.

His goal is to find a common counting mechanism that all animals from fish to humans have inherited.

That shared mechanism may not exist, but what does seem to be clear is that some form of counting is more or less universal.

It's as useful to them as it is to us

"We haven't really come across any animals that can't count," says Butterworth. "Yes, we humans can count higher and [count] more things than animals, but the basic ability to count we share."

That must be because counting is one of those abilities, like seeing or moving around, that is almost always good to possess. "It's as useful to them as it is to us," says Butterworth.

Whether the animal in question is counting potential rivals, deciding which shoal to swim in, or filling out a tax return, a knack for counting is a basic life skill.