Circular string logic doubt

118 Views Asked by At

I have a question about circular shift in strings in Python. But since my question is more about the mathematical/logical explanation of it than the programming itself, I posted this question here.

The problem in question refers to a Caesar cipher, which in my case had the following conditions:

  • A n-string of values, going from 0 to n, corresponding to the positions of the letters on this string, (e.g. 0-a, b-1, and so on…);
  • A key value chosen by the user, that is, a value that will shift the position of each letter corresponding to the key value, (e.g. if key value is 5, then the letter a would be 5, the letter b would be 6, and so on…);

Aside from the conditions and explanations above here is the part that I didn’t understand: the final solution to the problem involves n-string steps, e.g. adding each value of the letter positions on the string with the key value and then after this operation, get this result and divide by the total positions on the string (e.g. n-string positions) and find the remainder of this division.

This remainder of this division will be exactly how many positions it would be necessary to each letter position to move corresponding to the key value is given by the user.

Now, doing this operation manually I sincerely found myself completely lost in understanding the logic on this operation, because the quotient on most of the divisions I done give numbers going to the eleventh decimal position, so these are time-demanding calculations if you don’t have a calculator.

Summarizing my question: How can I predict (and what is the logical and mathematical explanation) that the remainder of the division of the result of the sum of the letter position value by the key value by the n-string length will give me exactly how many positions the letter position value should change in a circular fashion on the n-string (e.g. after reaching value n, goes back to 0) based on the key value?

Note: I’m not a math expert, so, if possible, explain me this in a detailed form, thanks.

1

There are 1 best solutions below

1
On

As i said on the comment section above i'm just posting this explanation to expand a little bit Ethan's clock analogy in my own thoughts and in case somebody ever have the same doubt:

Thinking about this analogy i realized how the mathematical operation and its results fit to the logic of the whole problem:

In the division of the result of the sum of the letter position plus the key value by the total letters in the string, the quotient of the division is exactly how many complete spins that was actually made by the clock pointer, and the remainder is how much the pointer made on it’s last incomplete spin. Since the value of the key will always be different than zero, and the letter positions most of the time won't match the length of the string, this means that the value of the remainder of this operation is always an inferior one compared to the total length of the string, therefore it represents an incomplete final spin that points out the value reached by the pointer.