Two academics have shocked themselves and the world of mathematics by discovering a pattern in prime numbers.

Primes - numbers greater than 1 that are divisible only by themselves and 1 – are considered the ‘building blocks’ of mathematics, because every number is either a prime or can be built by multiplying primes together - (84, for example, is 2x2x3x7).

Their properties have baffled number theorists for centuries, but mathematicians have usually felt safe working on the assumption they could treat primes as if they occur randomly.

Now, however, Kannan Soundararajan and Robert Lemke Oliver of Stanford University in the US have discovered that when it comes to the last digit of prime numbers, there is a kind of pattern.

Apart from 2 and 5, all prime numbers have to end in 1, 3, 7 or 9 so that they can’t be divided by 2 or 5. So if the numbers occurred randomly as expected, it wouldn’t matter what the last digit of the previous prime was. Each of the four possibilities – 1, 3, 7, or 9 – should have an equal 25 per cent (one in four) chance of appearing at the end of the next prime number.

But after devising a computer programme to search for the first 400 billion primes, the two mathematicians found prime numbers tend to avoid having the same last digit as their immediate predecessor – as if, in the words of Dr Lemke Oliver they “really hate to repeat themselves.”

A prime ending in 1 was followed by a prime ending in 1 only 18.5 per cent of the time, significantly less often than the expected 25 per cent. And, the pair found, primes ending in 3 tended be followed by primes ending in 9 more often than in 1 or 7.

The pattern - already being referred to as ‘the conspiracy among primes’ - has left mathematicians amazed that it could have remained undiscovered for so long. “I was floored,” Ken Ono, a number theorist at Emory University in Atlanta, told Quanta Magazine. “I have to rethink how I teach my class in analytic number theory now.”

Professor Soundararajan himself admitted to New Scientist: “It was very weird. It’s like some painting you are very familiar with, and then suddenly you realise there is a figure in the painting you’ve never seen before.”

Professor Soundararajan’s friend Professor Andrew Granville, of University College, London, told The Independent that the Stanford man’s surprise was such that he initially doubted his own discovery. “He asked me to ‘look over’ his paper four weeks ago, which is code for ‘Am I kidding myself, or have I discovered something?’” Professor Granville added: “He did ask me in November whether I would believe anything like this. Apparently, I looked at him as if he was crazy.”

The two US-based academics argue that the last-digit pattern they have discovered could be explained by the k-tuple conjecture, devised in the early 20th Century to predict how groupings of primes will appear. They also point out that as primes stretch to infinity, they eventually lose the last-digit pattern and start to appear in a random manner.