Yesterday I was solving an interesting question that has been used on the Swedish Computer Science contest – a programming contest, in 2010, which probably is simple, but interesting because it emphasises sequences, et cetera.

The question introduced the Vigenére cipher in a classical way, with two disks containing letters of the English alphabet. In the beginning, we know several things about the encrypted text, which are to help us when we are going to crack the message.

  • The disks might have different number configurations, i.e. there is still a shift, which is unknown.
  • In order to “make it harder” for the enemy, each time we have obtained a letter, we continue by using a new shift, and this shift is increased by a constant value.
  • Fortunately, we get to know that each message contains the word “HEJ” – a Swedish word for “hello”, which might be used as  “bye”, I think.

Also, we get some examples as well, for instance:

Example 1:
Message: HEJVITARENFIKANU – means let’s take a “fika”

Example 2:
Encrypted text: GCGDJJI
Message: HEJHOPP – hello in an optimistic manner

There is also an example of this sequence,in Example 1, which is tells us that the difference is 9, 12, 15, 18… (common difference 3).

I’m solving this using a recurrence, and I’m not simplifying this arithmetic progression in any way, I just add d all the time.

The code below has passed all their tests, which are located at the end of this post.

Example 1

Example 2

Example 3