Number of iterations required for a transposition cipher to yield the original input

77 Views Asked by At

Suppose a function $f$, taking one string of text $str$ as input, is defined so that it outputs another string of text $res$ which is the transposed characters of $str$ according to the following algorithm:

  1. Let $i_1 = 1$, $i_2 = $ the number of characters in $str$, and $res = ""$ (empty string)
  2. While $i_2 - i_1 > 1$:

    a. Append the character $str[i_1]$ to $res$

    b. Append the character $str[i_2]$ to $res$

    c. Increment $i_1$ by $1$

    d. Decrement $i_2$ by $1$

  3. If $i_1 = i_2$, then append the character $str[i_1]$ to $res$; otherwise, append the characters $str[i_1]$ followed by $str[i_2]$ to $res$.

  4. Return $res$

Assume that string indices begin at one, i.e. "ABC"$[1] = $ "A" and that each character of $str$ is unique.

For example, $f($"ABCDEF"$)$ would equal "AFBECD", and $f($"AFBECD"$)$ would equal "ADFCBE". We can keep iterating $f \circ f \circ f(x)$, yielding "AEDBFC", "ACEFDB", and finally "ABCDEF", which was our original input. All in all, for any string $str$ of 5 characters, $f(str)$ = 4, since it takes four iterations including the original string.

Let $g(n)$ represent the number of iterations required to transpose a string of $n$ characters back into itself according to $f$.

Following this formula for strings of 1 through 10 characters, there is unusual variation in the number of iterations required: $g(2) = 2$, $g(3) = 3$, $g(4) = 4$, $g(5) = 4$, $g(6) = 6$, $g(7) = 7$, $g(8) = 5$, $g(9) = 5$, and $g(10) = 10$. I have calculated these values up to $g(30)$: $g(28) = 21$, $g(29) = 10$, and $g(30) = 30$.

Plotting these discrete points on a graph, we can find several linear functions which collectively fit some of the points: $y = x$ fits some, $y = 0.5x + 1.5$ fits others, while $y = 0.25x + 2.5$ fits still others, while there are many more data pairs not accounted for.

Does anybody know what a definition of $g(n) \space | \space n \in \Bbb I, n > 0$ would be?