## You're reading: Posts Tagged: Python

### Matrix multiplication doesn’t work like that

Earlier this week I posted a matrix multiplication worksheet on Mastodon.

If you do some of these, you might spot what’s funny about them. For example.

$\Large \begin{bmatrix} \color{navy}{4} & \color{navy}{8}\\ \color{navy}{2} & \color{navy}{3} \end{bmatrix} \begin{bmatrix} \underline{\color{blue}{8}} & \underline{\color{blue}{8}}\\ \underline{\color{blue}{2}} & \underline{\color{blue}{7}} \end{bmatrix} = \begin{bmatrix} \color{navy}{4}\underline{\color{blue}{8}} & \color{navy}{8}\underline{\color{blue}{8}}\\ \color{navy}{2}\underline{\color{blue}{2}} & \color{navy}{3}\underline{\color{blue}{7}} \end{bmatrix}$

That is, the answer to each question can be made by treating the element in the first matrix as the first digit and the corresponding element in the second matrix as the second digit in the answer element. This is not how matrix multiplication works, and ought to be funny if I hadn’t totally over-explained the joke!

I saw one of these in a meme that Katie posted in the Finite Group chat and it got me thinking about how these work.

If we set up the matrices like this

$\begin{bmatrix} a & b\\ c & d \end{bmatrix} \begin{bmatrix} e & f\\ g & h \end{bmatrix} = \begin{bmatrix} 10a+e & 10b+f\\ 10c+g & 10d+h \end{bmatrix}$

Then we establish four equations with eight unknowns.

\begin{align*} ae + bg &= 10a+e\\ af+bh &= 10b+f\\ ce+dg &= 10c+g\\ cf+dh &= 10d+h \end{align*}

Since there are more unknowns than equations, these don’t have a single solution. What I wanted was to find integer solutions with all values single-digits. I wrote some Python code to find these. I removed some that look overly symmetrical – either the rows of the matrix are identical, or the same matrix is repeated. This left 73 items.

From these 73 items, I wrote a second Python script that picks 20 of them at random and builds these into a LaTeX worksheet. For the Mastodon post I reformatted this into the shape and size that I thought would display better on social media, and added in one of the squared matrices for an extra hint something weird is up, hoping people might notice this isn’t just a boring post about matrix multiplication practice!

You can view these scripts and associated files on GitHub.

### $$-e^{i\pi}$$ to Watch: SparksMaths

In this series of posts, we’ll be featuring mathematical video and streaming channels from all over the internet, by speaking to the creators of the channel and asking them about what they do.

We spoke to Ben Sparks, the author of the SparksMaths YouTube channel, who shares recordings of live Geogebra builds illustrating various maths concepts.

### Primes, reversals and concatenations

In the last Finite Group livestream, Katie told us about emirps. If a number p is prime, and reversing its digits is also prime, the reversal is an emirp (‘prime’ backwards, geddit?).

For example, 13, 3541 and 9999713 are prime. Reversing their digits we get the primes 31, 1453 and 3179999, so these are all emirps. It doesn’t work for all primes – for example, 19 is prime, but 91 is $$7 \times 13$$.

In the livestream chat the concept of primemirp emerged. This would be a concatenation of a prime with its emirp. There’s a niggle here: just like in the word ‘primemirp’ the ‘e’ is both the end of ‘prime’ and the start of ’emirp’, so too in the number the middle digit is end of the prime and the start of its emirp.

Why? Say the digits of a prime number are $$a_1 a_2 \dots a_n$$, and its reversal $$a_n \dots a_2 a_1$$ is also a prime. Then the straight concatenation would be $$a_1 a_2 \dots a_n a_n \dots a_2 a_1$$. Each number $$a_i$$ is in an even numbered place and an odd numbered place. Now, since

$10^k \pmod{11} = \begin{cases} 10, & \text{if } k \text{ is even;}\\ 1, & \text{otherwise,} \end{cases}$

it follows that each $$a_i$$ contributes a multiple of eleven to the concatenation. A mismatched central digit breaks this pattern, allowing for the possibility of a prime.

I wrote some code to search for primemirps by finding primes, reversing them and checking whether they were emirps, then concatenating them and checking the concatenation. I found a few! Then I did what is perfectly natural to do when a sequence of integers appears in front of you – I put it into the OEIS search box.

Imagine my surprise to learn that the concept exists and is already included in the OEIS! It was added by Patrick De Geest in February 2000, based on an idea from G. L. Honaker, Jr. But there was no program code to find these primes and only the first 32 examples were given. I edited the entry to include a Python program to search for primemirps and added entries up to the 8,668th, which I believe is all primemirps where the underlying prime is less than ten million. My edits to the entry just went live at A054218: Palindromic primes of the form ‘primemirp’.

The 8,668th primemirp is 9,999,713,179,999.

### Spooky 007 date coincidence

John Bibby points out a numeric coincidence – “Sean Connery died on 31/10/20. If you add up all the numbers in the date = 007”. Spooky, huh? I’m hoping you’re asking ‘is that unlikely?’ John asks “How many other dates this year add up to 007? What about in 2021?”

This is a fine pen-and-paper activity (or for thinking about in the shower, as I tried this morning), but also a nice little brute-force coding exercise.

### How many ways to shuffle a pack of cards?

This is an excerpt from friend of The Aperiodical, Matt Parker’s book, “Things to Make and Do in the Fourth Dimension”, which is out now in paperback.

There’s a lovely function in mathematics called the factorial function, which involves multiplying the input number by every number smaller than it. For example: $\operatorname{factorial}(5) = 5 \times 4 \times 3 \times 2 \times 1 = 120$. The values of factorials get alarmingly big so, conveniently, the function is written in shorthand as an exclamation mark. So when a mathematician writes things like $5! = 120$ and $13! = 6,\!227,\!020,\!800$ the exclamation mark represents both factorial and pure excitement. Factorials are mathematically interesting for several reasons, possibly the most common being that they represent the ways objects can be shuffled. If you have thirteen cards to shuffle, then there are thirteen possible cards you could put down first. You then have the remaining twelve cards as options for the second one, eleven for the next, and so on – giving just over 6 billion possibilities for arranging a mere thirteen cards.

### Wolfram|Alpha can’t. But CP can!

For a while, I’ve been following this cool Twitter account that tweets questions Wolfram|Alpha can’t answer. The genius of it is that the questions all look like things that you could half-imagine the solution algorithm for at a glance, and many of them look like the kinds of questions Wolfram like to give as examples when they’re showing off how clever their system is.

Questions like this:

The answer to that is 278. How do I know that? I know that because I went on a little problem-solving binge answering the questions that Wolfram|Alpha can’t.