Sunday, 13 April 2025

Maths: Simple codes

We will start with substitution ciphers.

These replace one letter with another. The simplest form is said to date from the time of Julius Caesar, and it involves replacing each letter by another letter, perhaps three along in the alphabet. The computer in the film 2001: A Space Odyssey is called HAL, and some people think this was a substitution cipher for IBM. Think about a one-letter shift…

The trick to solving these ciphers is to do a quick count of frequencies for the different letters. That means longer passages are easier to decode, because e, r, s and t will all be common.
If you find three consecutive letters in the coded text (like J, K and L) that are turn up more than once, they are likely to be r, s and t. Try making two slips of paper like this, noting that each slip has two complete alphabets:


Now all you have to do is slide these past each other (I have an offset of 3 in the second picture above). Once the equivalent letters match up, you can read the message off.

Now let's make it easier: you will need a spreadsheet for this: talk to an adult or older sibling (brother or sister) if you need help. Actually, this is probably more of a teen-level thing, but I know 9-year-olds who can do this.

I used a spreadsheet to generate the substitution cipher messages below. Here, I will give you just a few hints, but the rest will be up to you.

Cell A1 is the offset, the number of characters to shift. For example, if I have 5, an A will become F, B will become G and so on. Then in the rest of column A, I type the letters of the sentence to be coded, leaving out the spaces. That means your sentence becomes a column of letters, starting in A2.

I use all lower-case letters. There is an operator in spreadsheets called CODE that converts letters to numbers: A is 65, a is 97, Z is 90, z is 122. I enter this formula in cell B2: =CODE(A2). Now I need to increase all of these values, so in cell C2 I put the formula =B2+A$1. (The $ is a special trick that means when we copy this formula in other cells later, we will keep looking at cell A1—this is called absolute addressing. Don’t worry about it, just do it!)

The main thing is to copy the formulas exactly.


Three views of the same spreadsheet, the first showing the codes, the other two with different offsets in A1.

The problem: some of the numbers will be too high, because we have gone beyond the range of the alphabet. All of my numbers need to be between 97 and 122. So now, I add a new formula in cell D2: =IF(C2<123,C2,(C2-26)). Once again, don’t worry about this too much: it is just a handy bit of formula that turns numbers outside the range back into numbers inside the range. And now, we are ready to convert the number back to a letter.

In cell E2, we add the formula =CHAR(D2). Now we are ready to copy cells B2 to E2 down as far as we need. Highlight those cells, then drag down to, say, row 100 and use the command FILL DOWN to copy those formulas. Now if you type a sentence, starting A2 and working down, the code will appear in column E.

A sample of the input and output.

(Older readers: I actually get a bit more complicated, using the =CONCATENATE command to pull all of the letters into a single string, but I will leave that for you to play with.)

Challenge: can you use this information to create a spreadsheet that will decode such a cipher? If you are really cunning, you can enter the message in column A of this new design, and scan the 26 columns starting at E, to spot the one that makes sense. Remember that there are always simple solutions and answers in technology and engineering, and sometimes in mathematics. There are definitely areas where we can do science with everyday stuff.

If you can handle this, you are ready for anything else in this blog.

So let's get really tough:

Code challenge

Here is a common sentence that used to be typed by typists when they were learning to type. It is hard to analyse the frequencies, because most of the letters of the alphabet are used.

gurdh vpxoe bjasb kwhzc rqbir egury mlqbt

Here is another sentence often used by typists.

abjvf gurgv zrsbe nyytb bqzra gbpbz rgbgu rnvqb sgurc negl

Beating the frequency counters

As a teen, I had a problem with a relative reading my diary, so I devised a code that was easy to write, because I paired what I classed as ‘similar’ letters and swapped them. For example, A became O and O became A and so on. Here is the full set of the pairs: AO BP CK DT EI FV GX HJ LR MN QW SZ UY.

The result looks very like a foreign language:

Djez ez o zenbri igonbri, or to make it harder, Djeze zozen briig onbri.

To muddy the waters, I would sometimes throw in blocks of random letters or meaningless Morse code. Can you find a better way to confuse nosy family members?

Final note:  You will get some fun at this next link.

https://hci.stanford.edu/winograd/shrdlu/name.html

To search this blog, use this link and then use the search box

Another way: use the index!


 

No comments:

Post a Comment

Sci: A convection snake

Cut a piece of paper into a 6 cm diameter spiral. It doesn’t need to be too neat. I drew a guideline, and only followed it roughly. C...