In Excel, the columns range from A-Z, AA,AB,AZ,BA,..,BZ and so on. They actually each stand for numbers, but rather are encoded as alphabet strings.

In this challenge, you will be given a string of alphabets, and you must calculate the column it corresponds to.

Some tests:

'A' returns 1 (meaning that it is the first column)

'B' returns 2

'Z' returns 26

'AA' returns 27

'AB' returns 28

'AZ' returns 52

'ZZ' returns 702

'AAA' returns 703

You can assume that capital letters will be given only.

Shortest bytes win.

Good luck!

  • So... base 26 with the alphabet? – Jo King Oct 20 at 1:55
  • 1
    It isn't quite base 26 because there's no zero. – J.Doe Oct 20 at 2:03
  • @J.Doe Ah, I guess you're right. I didn't notice since my solution automatically treated Z as 10 anyway – Jo King Oct 20 at 2:56
  • Reverse challenge. – user202729 Oct 20 at 3:01
  • 4
    @JoKing Bijective base. – user202729 Oct 20 at 3:03

28 Answers 28

up vote 8 down vote accepted

Perl 6, 17 bytes

{:26[.ords X-64]}

Try it online!

Anonymous code block that subtracts 64 from each byte value and converts from base 26 with Z overflowing to the next column.

Google Sheets, 21 bytes

(formula evaluates to the result, takes input from cell A1)

  • Just about to post a slightly less golfed version of this. – ATaco Oct 20 at 6:38
  • 1
    I also have a solution in Google Sheets that doesn't rely on builtin COLUMN, check it out. (besides, I feel bad that the solution I put more effort on gets less attention... it's a typical problem with voting anyway, especially when the challenge is on HNQ.) – user202729 Oct 22 at 0:13

R, 48 43 bytes

-5 bytes thanks to @Giuseppe, using the same logic, but as a program that eliminates the nchar call.

for(i in utf8ToInt(scan(,"")))F=F*26+i-64;F

Try it online!

Java (JDK), 39 bytes


Try it online!

Python 2, 52 45 bytes

for c in input():t=26*t+ord(c)%64
print t

Try it online!

05AB1E, 6 bytes


Try it online!

  • Out of curiosity, why use žx instead of just 64? – Kevin Cruijssen Oct 21 at 17:13
  • I don't know, it looked nicer I guess? – Okx Oct 21 at 21:44

Haskell, 38 34 31 bytes


Try it online!

PHP, 41 38 bytes

-3 thanks to Jo King.


run as pipe with -nr

unary output, 34 bytes:

1<?for($c=A;$c!=$argn;$c++)echo 1;

requires PHP 7.1. save to file, run as pipe with -nF.

APL(NARS), 11 chars, 22 bytes



  f¨'A' 'AA' 'AAA'
1 27 703 
  f¨'AB' 'ZZ' 'Z'
28 702 26 

C (gcc), 46, 43 bytes


Try it online!


a; f(int*s)
{  for(a=0;*s;) // Loop through s, which is a null-terminated string.
       a=*s++%64 + a*26; // Multiply accumulated value by 26, and add current char modulo 64 to it.
   s=a;} // Return the accumulated value.

JavaScript (Node.js), 48 bytes


Try it online!

Google Sheets, 100 bytes

(formula evaluates to the result, takes input from cell A1)


All spaces are added for clarity only.


  • I don't know if it's possible to remove the duplication of row(indirect("1:"&len(A1)).
  • Although Google Sheets has a decimal function, the transliteration would takes a lot of bytes.

APL+WIN, 12 bytes

Index origin 1.


Try it online! Courtesy of Dyalog Classic


⎕av⍳⎕ Prompts for input and gets Ascii integer value for each character

¯65+ subtracts 65 to give integers 1-26 for A-Z

26⊥ converts resulting vector from base 26 to single integer

Jelly, 7 bytes


Try it online!

Java (JDK), 92 bytes

static int m(String v){int x=0;for(int i=0;i<v.length();i++)x=x*26+v.charAt(i)-64;return x;}

Try it online!










  • I'm not an expert at golfing Java, but you can golf this down considerably by returning instead of printing, simplifying the for loops, removing whitespace and getting rid of the p and n variables. 92 bytes!. – Jo King Oct 20 at 14:22
  • Wonderful....... – Syed Hamza Hassan Oct 20 at 14:26
  • 1
    You can remove static to gain 7 bytes. You could also make this function a lambda to spare more bytes. I also think that the recursive version might save bytes. In any case, here's my 39 bytes solution. – Olivier Grégoire Oct 20 at 21:36
  • That's Wonderful. – Syed Hamza Hassan Oct 21 at 14:03

MATL, 11 bytes


Try it online!

Kotlin, 36 bytes


Try it online!

Ruby, 18 bytes


Try it online!

Japt -h, 10 bytes


Try it

Or without a flag. The first byte can be removed if we can take input as a character array.

¨c aI̓26

Try it


åÈ             :Cumulatively reduce by passing each character at Y through a function, with an initial total of 0
  *26          :  Multiply current total by 26
     -I        :  Subtract 64
       n       :   Subtracted from
        Yc     :    The codepoint of Y
               :Implicitly output the last element of the resulting array

Ruby -nl, 39 bytes


Try it online!

APL (Dyalog Classic), 11 bytes


Try it online!

J, 20 bytes


Try it online!


                  i.    - indices 
                    ]   - of the characters of the input
                a.      - in the alphabet
             32|        - mod 32
 [:(        )           - apply the following code to the above
         $~             - create a list of (left and right arguments exchanged) 
       26               - the number 26
           #            - repeated the length of the input times
    #.~                 - to base (26)

Charcoal, 10 bytes


Try it online! Link is to verbose version of code. Explanation:

     S      Input string
    E       Map over characters
         ι  Current character
        α   Uppercase alphabet
       ⌕    Find index
      ⊕     Increment
  ²⁶        Literal 26
 ↨          Base conversion
I           Cast to string
            Implicitly print

Kotlin, 29 bytes


Try it online!

Ahead, 22 bytes

^~>O@ ~+*<

Try it online!

J, 11 bytes


Try it online!

How it works

26#.64|3&u:  Monadic verb. Input: a string.
       3&u:  Convert each character to Unicode codepoint
    64|      Modulo 64; maps A -> 1, ... Z -> 26
26#.         Interpret as base-26 digits and convert to single integer

MBASIC, 84 bytes

1 INPUT S$:L=LEN(S$):FOR I=1 TO L:V=ASC(MID$(S$,I,1))-64:T=T+26^(L-I)*V:NEXT:PRINT T


? AZ

? ZZ


x86 machine code, 19 bytes

00000000: 31c0 8b19 83e3 3f41 b21a f7e2 01d8 3831  1.....?A......81
00000010: 75f0 c3                                  u..


section .text
	global func
func:				;this function uses fastcall conventions
	xor eax, eax		;reset eax to 0
		mov ebx, [ecx]	;ecx is 1st arg to this func (in fastcall conventions)
		and ebx, 63	;because 64 is a pwr of 2,n%64=n&(64-1)

		;ecx++		get next char in str by incrementing ptr
		inc ecx
		mov dl, 26	;using an 8bit reg is less bytes
		mul edx
		;eax+=ebx //(eax=(*ecx%64)+(eax*26))
		add eax, ebx

		;if(*ecx!='\0')goto loop
		cmp byte [ecx], dh ;dh==0
		jne loop
	ret			;return value is in eax

Try it online!

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.