Using maths to crack a code

Syllabus links

Key Stage 2 (age 7 to 11)

Can you crack the secret code?

Purpose

This experiment gives children an opportunity to discover the relative frequency of letters in English.

The child will learn how to create a frequency table and use it to decode a secret message.

You need to know

You will need

a sheet of paper
some paper
a pencil
a pencil
a book
a book
a calculator
a calculator (optional)

Steps

  1. Draw a grid on paper with six boxes across and six boxes down. This will form a frequency table.
  2. Write the letters A to Z in the top left of the first 26 boxes (there will be some boxes left over).
  3. Open your book at a random page. You are going to count the number of each letter. This will help you to crack the code.

    For each letter, draw a small line in the box with that letter. When you get to five, draw a line across to form a gate. This will make it easier to count them up in the end.

  4. Use your frequency chart to count the letters in your book. Keep going until there are only two or three letters not yet found.
  5. Go through each letter in your chart and count up the ticks. Write the total against each letter.
  6. Add together all the counts to give a grand total. Write this down.
  7. For each letter in your chart, write down its percentage of the total, to two decimal places.
  8. Finally, write down a list of letters from the most common (highest percentage) to the least common (lowest percentage), with their percentages.
  9. Hint

    A substitution code is where you substitute one set of letters for another.

    For example:

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

    O D P I R H Z A V Q G K N U Y E W S J C L M T X F B

    To encode a word, find the letter in the first row and write the letter from the second row. For example, SECRET becomes JRPSRC.

    To decode a word, do the reverse: find the letters in the second row and write the matching letter from the first row.

Questions

  1. You have received the following coded message:

    RQI RKIJFAKI WF QWTTIB JR RQI PHRRHN HO RQI CJKTIB.

    TH BHR UHHS OHK WR WB RQI TJERWNI PIVJAFI EHA JKI PIWBC OHUUHGIT.

    For each letter, write down how many times it appears in the coded message.

  2. Write down the total number of letters.
  3. For each letter, write down the percentage it is of the total, to two decimal places.
  4. Sort the coded letters into order from most common to least common.
  5. Can you decode the message using your frequency chart to guess which letters are represented by each letter in the code?
  6. Hint

    Try to fill in the most common letters first. Remember that your list is only a guide and won't match the coded message exactly.

    When you work out what a word is, you can fill in those letters in the rest of the phrase.

Expected answers

  1. The letter percentages from the book will depend on the book, but the following are typical values:

    A=8.17% B=1.49% C=2.78% D=4.25% E=12.70% F=2.23% G=2.02% H=6.09% I=6.97% J=0.15% K=0.77% L=4.03% M=2.41% N=6.75% O=7.51% P=1.93% Q=0.10% R=5.99% S=6.33% T=9.06% U=2.76% V=0.98% W=2.36% X=0.15% Y=1.97% Z=0.07%

    The letter counts for the question are as follows:

    A=3 B=5 C=2 D=0 E=2 F=3 G=1 H=11 I=14 J=6 K=5 L=0 M=0

    N=2 O=3 P=3 Q=5 R=11 S=1 T=6 U=3 V=1 W=6 X=0 Y=0 Z=0

  2. There are 93 letters in total.

  3. A=3.23% B=5.38% C=2.15% D=0.00% E=2.15% F=3.23% G=1.08% H=11.83% I=15.05% J=6.45% K=5.38% L=0.00% M=0.00% N=2.15% O=3.23% P=3.23% Q=5.38% R=11.83% S=1.08% T=6.45% U=3.23% V=1.08% W=6.45% X=0.00% Y=0.00% Z=0.00%

  4. I=15.05% H=11.83% R=11.83% J=6.45% T=6.45% W=6.45% B=5.38% K=5.38% Q=5.38% A=3.23% F=3.23% O=3.23% P=3.23% U=3.23% C=2.15% E=2.15% N=2.15% G=1.08% S=1.08% V=1.08% D=0.00% L=0.00% M=0.00% X=0.00% Y=0.00% Z=0.00%

  5. The following substitution code was used:

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

    J P V T I O C Q W D S U N B H X Z K F R A M G Y E L

    The decoded message is as follows:

    THE TREASURE IS HIDDEN AT THE BOTTOM OF THE GARDEN. DO NOT LOOK FOR IT IN THE DAYTIME BECAUSE YOU ARE BEING FOLLOWED.

    Solving this problem is quite challenging, so you might have to help the child (or at least check that they haven't gone wrong).

Explore further (optional)

  1. How could you make an even more accurate frequency chart for decoding messages like this?
  2. Practise sending and receiving coded messages with your friends. Create a new substitution code each time, and try to use the letter frequencies to crack it.

Tips for further exploration

  1. One way to create a more accurate version of the frequency chart would be to use more words to get the frequencies.

    Another approach would be to count the frequencies of pairs of letters instead of single letters. This should make it easier to decode messages.

    You can find frequency lists of pairs of letters online.

  2. Very short messages will be difficult to crack because the word frequencies might be very different from the averages.