Algorithm for series

algorithm

Solution

Treat those strings as numbers in base 26 with `A=0`. It's not quite an exact translation because in real base 26 `A=AA=AAA=0`, so you have to make some adjustments as necessary.

Here's a Java implementation:

static String convert(int n) {
    int digits = 1;
    for (int j = 26; j <= n; j *= 26) {
        digits++;
        n -= j;
    }
    String s = "";
    for (; digits --> 0 ;) {
        s = (char) ('A' + (n % 26)) + s;
        n /= 26;
    }
    return s;
}

This converts `0=A, 26=AA, 702=AAA` as required.

Problem

A, B, C,…. Z, AA, AB, ….AZ, BA,BB,…. , ZZ,AAA, …., write a function that takes a integer n and returns the string presentation. Can somebody tell me the algorithm to find the nth value in the series?

Original source

Related problems