0
$\begingroup$

In the question asked here, task is to convert decimal numbers to alphabets, like

1: A 2: B . . . 26: Z 27: AA . . . 703: AAA 

the method used to generate the alphabets is ::

1: subtract 1 from the number
2: calculate number % 26 where % is remainder operator and
if remainder is 0 print A, for 1 print B .... for 25 print Z.
3: divide the number by 26.
4: repeatedly perform steps 1 to 3, till the number becomes zero.

how this method is working? what is the logic behind this? please explain.

  • 0
    [Possibly relevant](http://math.stackexchange.com/questions/172808/).2012-09-02

2 Answers 2

1

Choose any number $x$ and do this:

  1. calculate $x % 10$ and write it down
  2. divide $x$ by $10$
  3. perform steps 1,2 until $x$ becomes $0$.

Thus what you have is one step away from ordinary numbers (except for $26$ instead of $10$ and ABC... instead of $123$...). Subtracting 1 makes a bit of difference (there is no 0 now) but the idea is the same.

1

Your description difffers from the question you link to in that it goes ..., Z, AA, BA, CA, ... instead of ..., Z, AA, AB, AC, ... A detail that both lists cleverly(?) disguise with dots. Anyway, it is evident that in the intended output the last (or first?) letters should make a repeating pattern A, B, ..., Z, A, B, ... Therefore it is clear that the remainder of $n$ modulo 26 can be used to determine this letter. That is, we write $n =26\, n' + r$ with $n'\in \mathbb N_0$ and $0\le r < 26$. Numbers with the same $n'$ have different $r$ values, hence it is sufficient to construct the remaining letters from $n'$ alone - by using the very same algorithm. This has one little problem: After a while we would rach $n=0$ and output A repeatedly. Subtracting 1 in the first step takes care of the fact that the first 26 codes need no second letter at all. Fortunately, it also makes sure that $n=1$ produces A in the first place. (If you'd want to start with $n=0\mapsto A$, the subtracting of 1 should be step 3a).