Algorithm to find out on which position ZX is?

46 Views Asked by At

I am having the following problem. Lets consider the alphabet. From A-Z there are 26 letters. If its for example AA, then its the 27 letter this goes on until for example AWZ which is the 1300 letter in this alphabet. One can visualize an excel spreadsheet for this alphabet(the columns). Lets consider we want to know the position of the the letter(column in excel) XX or BD. Is there any algorithm to calculate this without counting it?

I appreciate your answer!

PS.: Problem is not out of any book. Just asked the question myself, after I used excel ;P

1

There are 1 best solutions below

4
On BEST ANSWER

$\boxed{\text{Val}(\alpha \beta) = 26\times\text{Val}(\alpha) + \text{Val}(\beta)}$

Examples

$\text{Val}(AA) = 26 \times \text{Val}(A) + \text{Val}(A) = 26 \times 1 + 1 = 27\\ \text{Val}(BD) = 26 \times \text{Val}(B) + \text{Val}(D) = 26 \times 2 + 4 = 56\\ \text{Val}(XX) = 26 \times \text{Val}(X) + \text{Val}(X) = 26 \times 24 + 24 = 648$

More generally:
$\boxed{\text{Val}(\alpha_n \alpha_{n-1} \ldots \alpha_0) = 26^{n-1} \times \text{Val}(\alpha_n) + 26^{n - 2} \times \text{Val}(\alpha_{n-1}) + \cdots + \text{Val}(\alpha_0)}$

Example

$\text{Val}(AAAAA) = 26^4\times 1 + 26^3 \times 1 + 26^2 \times 1 + 26 \times 1 + 1 = 475255\\ \text{Val}(KARE) = 26^3 \times 11 + 26^2 \times 1 + 26 \times 18 + 5 = 194485$