# Ifan investigates… Happy primes

**Welcome to the first in my series of articles looking at some of the science and math featured in Doctor Who. I love mathematics and get quite excited when it’s used in the show, so for anyone who’s interested, I’m going to try and explain it.**

In 42, there’s a clip which refers to happy primes. In this post, I’ll attempt to explain what happy primes are, and their mathematical properties. All you need to know is what “squaring” is and what’s a “prime number”.

Squaring involves multiplying a number with itself, so squaring y is y × y. This can also he shown as 7^2 (7 × 7 = 49)

A prime number is a number that only has 1 and itself as factors. So 7 is a prime as the only way to multiply two whole numbers to make 7 is 1 × 7 = 7. 10 is not a prime number because 2 × 5 = 10.

The clip I’m referring to is here:

What is the Doctor actually talking about here? And how is 313, 331, 367, 379 a sequence?

We’ll use 331 as it was in the episode, and find out if it’s a happy number. We do this by squaring each digit and then adding them up.

331 → 3^2 + 3^2 + 1^2 = 9 + 9 + 1 = 19

19 → 1^2 + 9^2 = 1 + 81 = 82

82 → 8^2 + 2^2 = 64 + 4 = 68

68 → 6^2 + 8^2 = 36 + 64 = 100

100 → 1^2 + 0^2 + 0^2 = 1 + 0 + 0 = 1

1 → 1^2 = 1 = 1 😌

Thus, the sequence ends when it comes to one and 331 is a happy number because it ended on 1. It’s also a prime number which means its only factors are 1 and 331.

So, what if it doesn’t ends on 1? Then the number is unhappy or sad.

Let’s take 29 as an example:

29 → 2^2 + 9^2 = 4 + 81 = 85

85 → 8^2 + 5^2 = 64 + 25 = 89

89 → 8^2 + 9^2 = 64 + 81 = 145

145 → 1^2 + 4^2 + 5^2 = 1 + 16 + 25 = 42

42 → 4^2 + 2^2 = 16 + 4 = 20

20 → 2^2 + 0^2 = 4 + 0 = 4

4 → 4^2 = 16 = 16

16 → 1^2 + 6^2 = 1 + 36 = 37

37 → 3^2 + 7^2 = 9 + 49

58 → 5^2 + 8^2 = 25 + 64 = 89

We already had the number 89, (3rd in the list). This means that it will forever cycle through 89, 145, 42, 20, 4, 16, 37, 85 and thus will never reach 1. This makes it an unhappy or sad number. ☹

Every number on the left side of the 331 example are also happy numbers (only 18 is prime) and every number on the left side of the 29 example are also unhappy numbers. As they’re all part of the same sequence.

So how do you find out if a number is happy?

The definition is:

Take any positive integer (whole number) then replace it by the sum of the square of its digits. Then calculate that number and continue until you reach one (😌) or loop endlessly (☹).

This is what the the Doctor was talking about, and if that number is also prime, then it’s a happy prime.

Then, 313, 331, 367, 379 is a sequence of 4 happy primes increasing from 313.

Interestingly, if you change the order (a permutation) of a happy number, the number would still be happy (As 1^2 + 9^2 is the same as 9^2 + 1^2.) However you cannot guarantee that the permutation would be a happy prime as while 19 is prime, 91 is not. (7 × 13 = 91)

Any prime of the form 10^n + 3 or 10^n + 9 is guaranteed to be a happy prime. Where n is any number greater than 0.

10^n means that the number must start with 1, 10^2 = 100, 10^3 = 1000 etc. And the number at the end is either a 3 or a 9. Hence all other numbers are 0 which doesn’t affect the sum (as 0^2 = 0).

19 is a happy number

19 → 1^2 + 9^2 = 1 + 81 = 82

82 → 8^2 + 2^2 = 64 + 4 = 68

68 → 6^2 + 8^2 = 36 + 64 = 100

100 → 1^2 + 0^2 + 0^2 = 1 + 0 + 0 = 1

1 → 1^2 = 1 = 1

And 13 is too:

13 → 1^2 + 3^2 = 1 + 9 = 1

10 → 1^2 + 0^2 = 1 + 0 = 1

1 → 1^2 = 1 = 1

Thus, you can add any number of zeros between the 1 and 3 (or 9) and the number would still be happy.

Which means all of these are happy:

13 19

103 109

1003 1009

10003 10009

100003 100009

1000003 1000009

[…] […]

Thus, if a prime is of the form 10^n + 3 or 10^n + 9, it must also be a happy prime.

As a bonus, we’ve also proven from this logic that there are infinitely many happy numbers, as n can be any number greater than 0 (so there’s an infinite number of n to choose from.) This doesn’t prove that there are infinitely many happy primes, as not every 10^n + 3 or 10^n + 9 is a prime number. You would have to prove that there’s an infinitely many primes of the form 10^n + 3 or 10^n + 9.

It also can be proven that there’s an infinite number of prime numbers. To prove this, we’ll suppose that it’s false and take the largest prime, let’s call it p. Then we multiply p by every number below it until reaching 1. This can be represented as p! where the exclamation mark is the factorial function. So this number would be able to be divided by every number below p (as we’ve multiplied every number below p.)

Then we add 1 and everything falls into place. This new number (p! + 1) can’t be divided to give a whole number by any number below p as it will always give reminder 1. This means that either p! + 1 has a prime factor greater than p, or p! + 1 is a prime itself. By following this logic, you find a contradiction as p was supposed to be the largest prime number. Thus, our number ‘p’ cannot exist, there is no largest prime number, thus there must be an infinite number of prime numbers.

That means there’s an infinite number of primes and an infinite number of happy numbers. The question is, are there infinitely many happy primes? Who knows…

As a note, happy numbers are not limited to base 10. (Base 10 is our counting system 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 etc.) In fact, every number in base 2 is a happy number, which makes base 2 a very happy base. Base 2 is binary and formed of 2 numbers, 1 and 0. (It goes 0, 1, 10, 11, 100, 101, 110, 111, 1000 etc.)

To conclude here are some interesting happy primes:

10^150006 + 7426247×10^75000 is not only a happy prime but also a palindromic one. (Same number when read backwards and forwards).

2^42623801 – 1 is the largest happy prime so far. This type of prime is called a Mersenne prime as it’s in the form 2^n – 1.