Stack OverflowCode Golf: Numeric equivalent of an Excel column name
[+76] [67] Vivin Paliath
[2010-04-14 02:06:45]
[ excel code-golf ]
[ ]

The challenge

The shortest code by character count that will output the numeric equivalent of an Excel column string.

For example, the A column is 1, B is 2, so on and so forth. Once you hit Z, the next column becomes AA, then AB and so on.

Test cases:

A:    1
B:    2
AD:   30
ABC:  731
WTF:  16074
ROFL: 326676

Code count includes input/output (i.e full program).

(19) Code golf is pointless, APL always wins in the end. - BlueRaja - Danny Pflughoeft
(1) When you post a solution, please make sure it works across all the cases above (input/output as run is nice), and note where it does not. Thanks. - user166390
(3) Why the hell is this tagged with rosetta-stone?! - Josh Stodola
(2) J is APL without the Greek. J will always win, no one speaks APL anymore. - Callum Rogers
(1) @BlueRaja: It's interesting that APL is still winning these things in 2010, almost 40 years after it left the mainstream. - Kragen Javier Sitaker
@Kragen: That's because it's the most ridiculously condensed language there is; that's not a good thing. - BlueRaja - Danny Pflughoeft
@BlueRaja: well, I don't program in it, and it seems likely that the mainstream agrees with you (and Dijkstra). Still, I am tempted by the idea of Notation as a Tool of Thought... - Kragen Javier Sitaker
[+339] [2010-04-14 09:11:52] Danko Durbić

Excel, 9 chars :)

Use the right tool for the job:



(166) Use the right language for the job: Portuguese Excel =COL(). 6 characters. (See ) - Debilski
(21) Great! only it does not support ROFL though. - YOU
I'm torn between accepting this and the Ruby solution (which is the shortest). Excel really isn't a "language" but still, this is pretty clever :). SO community - what do you think? - Vivin Paliath
(6) I think you should accept my 12 character J solution, but I might be biased. :) - David
(63) This solution even reproduces the limitations of Excel correctly. - kibibu
I've decided to go with this one even though it's not a true programming language. Mainly because of how clever it is :) - Vivin Paliath
(1) @Vivin: Excel is of course a true programming language.… - kennytm
(18) It doesn't even take a string as input. Doesn't come close to doing what the problem said. It only works if it's in the column that happens to be named after the string in question. Totally not in the spirit of the question. - phkahler
(9) To be fair, Code Golf is made for these kinds of smartass answers. So you get a +1 from me. - Reynolds
[+96] [2010-04-14 03:26:50] David [ACCEPTED]

Perl, 36 34 33 31 30 17 15 11 characters



$ echo -n WTF | perl -ple '$_=()=A..$_'

Reduced to 17 by using echo -n to avoid a chop call.

Reduced to 15 by using say instead of print.

Reduced to 11 by using -p instead of say.

Explanation: A is evaluated in string context and A..$_ builds a list starting at "A" and string-incrementing up to the input string. Perl interprets the ++ operator (and thus ..) on strings in an alphabetic context, so for example $_="AZ";$_++;print outputs BA.

=()= (aka "goatse" operator [1]) forces an expression to be evaluated in list context, and returns the number of elements returned by that expression i.e., $scalar = () = <expr> corresponds to @list = <expr>; $scalar = @list.


(2) Use $_ or $` instead of $n` and call print with no args. - mob
(4) use "-p" and drop print altogether : echo -n WTF | perl -p -e '$_=()=A..$_' Total code : 11 characters, AH AH! - wazoox
(2) switch print to say and drop another 2 characters :) - mpeters
@wazoox, that doesn't work for me - it hangs since there's no \n in the input. - David
(16) Ah I love the goatse operator :) - Ether
So it does... now Perl is the winner (for now) :) - Vivin Paliath
You can drop -n if you use -l here. - jfs
You know, in even in the spirit of the competition, when the command to call your code is longer than the code itself I don't think it counts :-p - Justin
Yeah, I still think the J solution is more elegant. :) - David
(1) There is only a limited amount of 11 character perl programs that will actually compile... makes me wonder what the other ones do. - Dan
Looks like 40 characters to me. - AMissico
[+71] [2010-04-14 13:43:30] David

J [1], 17 12 10 characters



26#.64-~av  'WTF'


  • J parses from right to left.
  • av returns a list of the ascii indexes of each of the characters in its argument, so for example av'ABC' returns 65 66 67.
  • Then we subtract 64 from each element of that list with the verb 64-~.
  • Then we convert the list to base 26 using the #. verb.

(2) After reading a few comments about how the Excel solution really doesn't take a string input, I'm going to go with the solution that is the shortest, and actually takes a string input. - Vivin Paliath
Wow a J solution I can actually understand :) - Callum Rogers
(2) @Brandon: J is made for everything, as long as you don't mind spending years learning how to read it. - David
[+55] [2010-04-14 19:02:40] Tesserex

Brainf*ck, 81 characters (no whitespace)



,[  // get character input into p[0], enter loop if it isn't null (0)
>>>[->>+++++[-<+++++>]<+<] // take what's in p[3] and multiply by 26, storing it in p[4]
>[-<+>] // copy p[4] back to p[3]
<<++++++++[<++++++++>-]< // store 64 in p[1]
[<->-]< // subtract p[1], which is 64, from the input char to get it's alphabetical index
[>>>+<<<-] // add p[0] to p[3]
,] // get another character and repeat
>>> // move to p[3], where our final result is stored

So you'll notice I didn't actually convert the numerical value to an ascii string for printing. That would likely ruin the fun. But I did the favor of moving the pointer to the cell with the result, so at least it's useful to the machine.

Hey, what do you know, I beat C#!

I'm surprised that anyone actually uses Bf in first place - OscarRyz
[+31] [2010-04-14 02:15:26] Mark Rushakoff

Ruby 1.8.7, 53 50 46 44 24 17 characters

p ('A'..$_).count


$ echo -n ROFL | ruby -n a.rb
$ echo -n WTF | ruby -n a.rb
$ echo -n A | ruby -n a.rb

Great, I tested very similar pattern already, but I didnt realize that p ( and p( is different, :( - YOU
use echo -n ... so you can drop the .chop - John La Rooy
@gnibbler: Thanks for the -n idea, that brings my perl solution down to 17 as well. - David
You could get that to 16 by using ?A instead of 'A' - steenslag
[+23] [2010-04-15 03:21:20] Ken


13 characters

Put the value in x:


then compute it with:


The only reason J beat me is because of the parentheses. I'm thinking there should be some way to rearrange it to avoid the need for them, but it's been a long day. Ideas?

(Heh, you perl programmers with your 30+ character solutions are so cute!)

Maybe 26⊥⁻65+⎕aV⍳x instead? That is how you write negative 65 in APL, right? - Kragen Javier Sitaker
I don't have my APL environment in front of me right now, but I think I tried something like that and it didn't work. Off the top of my head (and admittedly I'm the furthest thing from an APL wizard!) ⁻ is equivalent to - and since it evaluates right-to-left it's applied after the +, so you end up with 26⊥⁻(65+(⎕aV⍳x)) instead of 26⊥((⁻65)+⎕aV⍳x), which is what you need here. - Ken
Accepted Perl solution beats your code by two characters... =) - kolistivra
[+14] [2010-04-16 18:56:20] kennytm

Excel (not cheating), 25 chars

Supports up to XFD:



  1. Put the formula in cell A2.


  1. Enter the column string in cell A1.
  2. Read the result at cell A2.

54 chars, plus a lot of instructions

Supports ROFL also:

(A2)  =MAX(B:B)
(B2)  =IFERROR(26*B1+CODE(MID(A$1,ROW()-1,1))-64,0)


  1. Clear the whole spreadsheet.
  2. Put the formula (A2) in cell A2.
  3. Put the formula (B2) in cell B2.
  4. Fill formula (B2) to as far down as possible.


  1. Enter the column string in cell A1.
  2. Read the result at cell A2.

[+13] [2010-04-14 03:09:45] Cameron MacFarland

C# 156 146 118 Chars

using System.Linq;class P{static void Main(string[]a){System.Console.Write(


using System.Linq;
class P
    static void Main(string[] a)
            .Aggregate(0, (t, c) => (t + c - 64) * 26) / 26);

The using C=System.Console shortcut isn't helping you here, because you end up having to say "System." twice. You can save 5 chars if you just do "using System;" and change your code accordingly. But my non-linq version is still shorter. :) - Igby Largeman
You're right, and I can make it shorter still by using the first argument instead of Console.Readline, making it shorter still. :P - Cameron MacFarland
(1) oh crap, I helped you get ahead of me! :P - Igby Largeman
By the way, thanks for fixing my char count. I totally spaced on that. - Igby Largeman
(3) How about Console.Write instead of WriteLine ? - Andreas Grech
You can avoid use of Math.Pow() using Horner's Rule (see my answer) - Daniel Renshaw
You can probably shave off a few chars by invoking Aggregate() as a normal method which has the net effect of eliminating a using keyword. - erikkallen
@erikkallen - Tried that, it's a few characters longer. using < Enumerable - Cameron MacFarland
Think you may write the function itself only, without usings, classes and etc. - Kamarey
[+12] [2010-04-14 11:57:30] John La Rooy

Golfscript - 16 chars


$ echo -n WTF | ./golfscript.rb
$ echo -n ROFL | ./golfscript.rb

[+11] [2010-04-14 13:03:18] kennytm

Haskell, 50 51 56 chars



~:166$ echo -n "ROFL" | ./a.out
~:167$ echo -n "WTF" | ./a.out

[+9] [2010-04-14 04:52:20] Adam Rosenfield

Python, 64 49 characters

for c in raw_input():s=26*s+ord(c)-64
print s

You can also replace raw_input() with input() to reduce the character count by 4, but that then requires the input to contain quotation marks around it.

And here's a subroutine that clocks in at 47 characters:

f=lambda x:len(x)and 26*f(x[:-1])+ord(x[-1])-64

your #2 should be named f. Try running it now, it doesn't work. And it can be made shorter(47 characters) with the help of lambdas and short circuit evaluation: f=lambda x:len(x)and 26*f(x[:-1])+ord(x[-1])-64 - Ponkadoodle
@wallacoloo: Thanks. It's a community wiki, so you can feel free to make edits any time. - Adam Rosenfield
how about dropping len(x) for x alone? becomes 43 chars: f=lambda x:x and 26*f(x[:-1])+ord(x[-1])-64 - Nas Banov
[+9] [2010-04-16 20:33:12] Ciarán

k4 (kdb+), 11 characters



  • k4 parses left of right
  • .Q.A is defined within k4 - it is the vector "ABC...XYZ"
  • ? is the find operator - the index of the first match for items in the y arg within the x arg
  • +1 to offset the index
  • 26/: to convert to base 26

One caveat - this will only work where listed types are passed in:

  26/:1+.Q.A? "AD"

  26/:1+.Q.A? "WTF"


  26/:1+.Q.A? ,"A"

[+8] [2010-04-14 03:01:51] Daniel Vassallo

JavaScript 1.8: 66 characters

function a(p)Array.reduce(p,function(t,d)t*26+d.charCodeAt()-64,0)

Javascript 1.8: 72 characters

function a(p)(t=0,p.replace(/./g,function(d)t=t*26+d.charCodeAt()-64),t)

JavaScript 1.6: 83 characters

function a(p){t=0;p.split("").map(function(d){t=t*26+d.charCodeAt(0)-64});return t}

JavaScript: 95 characters

function a(p){r=0;t=1;l=p.length;for(i=0;i<l;i++){r+=(p.charCodeAt(l-1-i)-64)*t;t*=26}return r}

JavaScript: 105 characters

function a(p,i){i=i||0;l=p.length;return p?(p.charCodeAt(l-1)-64)*Math.pow(26,i)+a(p.slice(0,l-1),i+1):0}


a("A")        // 1
a("B")        // 2
a("AD")       // 30
a("ABC")      // 731
a("WTF")      // 16074
a("ROFL")     // 326676

I shaved 4 more characters from the 1.6 solution using expression closures. You may be able to shrink it a bit more by refactoring the (t?g*26:0) part. - David Murdoch
UPDATE. I just shaved off 9 more characters by using [].reduce - David Murdoch
FWIW, I added an implementation using string#replace - Chetan S
@Chetan, @David: Good job. That's neat! - Daniel Vassallo
[+8] [2010-04-14 08:45:19] Danko Durbić

Powershell, 42 chars


[+6] [2010-04-16 18:08:18] Daniel C. Sobral

Scala, 30 chars



C:\>scala -e "print((0/:args(0))(_*26+_-64))" AD

[+5] [2010-04-14 04:33:28] Adam Rosenfield

C89, 58 characters


The input (stdin) must contain only A-Z, no other characters (including newlines) are allowed.

(1) getchar() returns < 0 on EOF; EOF is not defined as -1, but it's common. - strager
@strager: Good point. Ensuring full portability would then require adding 2 characters (by changing c=getchar()+1 to (c=getchar())>=0 and 65 to 64). But, this should work in almost any C implementation. - Adam Rosenfield
[+5] [2010-04-14 07:38:29] AboutDev

Explanation of Concepts - Excelcification

Nice. I wrote my own version of this with a little more explanation a long time ago at Although it's not quite an optimized version!

FYI. The base 26 arithmetic is called hexavigesimal [1] and Excel's maximum column is XFD which converts to 16383 (using 0 as the first cell) which is coincidentally exactly 2^14 cells.

Can anyone guess as to why it is 2^14??


(5) May be they want to use only 64k memory at that time :-) - YOU
Because they wanted to be able to do offsets (x-y) which requires a sign bit. But that's only 15bits, so what's up with the 16th bit? Is it used as a flag? - phkahler
(1) This is not exactly base-26, because it has no 0. If we let A represent 1, and multiply with 26^n for position n (with n = 0 for the rightmost letter), it all works out as usual. - Thomas
@phkahler: With 1 bit, you can represent all unsigned integers in the range 0..2^0. Thus, with 16 bits, you can represent 0..2^15. Take away 1 bit for the sign, the maximum value will be 2^14-1 (=16383). - stakx - no longer contributing
(1) @phkahler: you don't need a sign bit to represent offsets as long as you don't need a distinguished representation for out-of-bounds values (i.e. you can check the processor overflow bit to see if you're out of bounds). - Kragen Javier Sitaker
@stakx: Nice trick... But 1 bit can represent 0..2^**1**-1. 16 bits is 0..2^**16**-1. Take away 1 bit for the sign, the maximum value will be 2^**15**-1 (=32767). If you don't believe me, try it with 2 bits. 2 bits is not 0..2^1 = 0..2. You have 00, 01, 10, 11 = 0, 1, 2, 3 in base10 respectively. - Ponkadoodle
@wallacoloo: Oops. You're right of course; what was I thinking! - stakx - no longer contributing
[+5] [2010-04-15 01:45:27] Paul Richter

Common Lisp, 103 128 characters

(defun x(s)(reduce(lambda(x y)(+(* 26 x)y))(map 'vector(lambda(b)(-(char-code b)(char-code #\A)-1))s)))

[+5] [2010-04-16 14:54:50] Daniel Renshaw

C#, 117 111 chars

No contest compared to the likes of Perl, Ruby and APL but an improvement on the other C#/Java answers given so far.

This uses Horner's rule [1].

class C{static void Main(string[]a){int t=0;foreach(var c in a[0]){t=(t+c-64)*26;}System.Console.Write(t/26);}}

(1) You can save 3 chars by removing the space between the [] and the 'a', and the foreach braces. - Cameron MacFarland
I didn't know string implemented IEnumerable. Sweet! Unfortunately the page about Horner's rule is way over my head, but clearly a winning strategy. I don't like using Console.Write() though, due to the messy output. +1 - Igby Largeman
[+4] [2010-04-14 02:30:30] user181548

Perl, 34 characters


Thanks to mobrule for several suggestions.

You can say pop=~/./g instead of split//,$ARGV[0] You can omit the $_ in the ord call. You can use $` instead of $s` and then just say print. - mob
[+4] [2010-04-14 07:04:09] YOU

Ruby 1.9, 21 characters



$ echo -n A| ruby x.rb
$ echo -n WTF| ruby x.rb
$ echo -n ROFL| ruby x.rb

You can use chop instead of strip to gain 1 char - JRL
Thanks @JRL, I was something wrong in usage of echo A | , it should be echo A| - YOU
Use echo -n like others and get rid of chop. - Daniel C. Sobral
Thanks @Daniel, I have updated it, but looks like echo -n is not working on Windows. - YOU
[+4] [2010-04-14 17:48:46] Igby Largeman

C#, 148 chars

using System;class P{static void Main(string[]a){var r=0d;int j=0,i=a[0].


using System;
class P
    static void Main(string[] a)
        var r = 0d;
        int j = 0, i = a[0].Length;
        while (i-- > 0)
            r += (a[0][i] - 64) * Math.Pow(26, j++);


[+4] [2010-04-16 16:09:12] user318679

Python - 63 chars

>>> f=lambda z: reduce(lambda x,y: 26*x+y, [ord(c)-64 for c in z])

>>> f('ROFL')


[+4] [2010-04-16 16:48:45] user318719

Common Lisp, 86 characters.

(defun z(s)(let((a 0))(map nil(lambda(v)(setf a(+(* 26 a)(digit-char-p v 36)-9)))s)a))

[+4] [2010-04-16 17:38:24] jimbokun


user> (reduce #(+ (* 26 %1) %2) (map #(- (int %) 64) "AD"))
user> (reduce #(+ (* 26 %1) %2) (map #(- (int %) 64) "ROFL"))

51 characters, plus the number of characters in the input string.

[+4] [2010-04-16 20:13:00] fluffy


int r=0;

String is stored in 'c', value is in 'r'.

[+3] [2010-04-14 07:39:53] Kevin Ross

in VBA I got it down to 98

Sub G(s)
Dim i, t
For i = 0 To Len(s) - 1
    t = t + ((Asc(Left(Right(s, i + 1), 1)) - 64)) * ((26 ^ i))
MsgBox t
End Sub

(3) Surely you don't need the indentation. - user181548
You don't need to declare the sub as Public, and nor do you need to say "Next i" (just use "Next"). Also, I think if you loop from 1 rather than zero you can shave off a character or two. - Gary McGill
@Gary, thanks for the tips, this is my first "round" of code golf I'm sure I will get better after a few more goes - Kevin Ross
[+3] [2010-04-14 11:48:06] Jonas Elfström

Ruby, 20 characters



$ ruby a.rb ABC

[+3] [2010-04-16 14:21:59] Kevin Vaughan

PHP - 73 Chars



php -r '$n=$argv[1];$s=$i=0;while($i<strlen($n))$s=$s*26+ord($n[$i++])-64;echo$s;' AA

> 27

(1) You could lose 10 characters by removing the curly braces around your while function and dumping the variable initialization. - Twipped
[+3] [2010-04-16 14:23:33] Robin

Java: 112 124 characters

class C{public static void main(String[]a){int r=0;for(int b:a[0].getBytes())r=26*r+b-64;System.out.print(r);}}

[+3] [2010-04-16 20:01:04] Dr. Pain

Common Lisp, 81 characters

(defun y(s)(reduce(lambda(x y)(+(* 26 x)(-(char-code y)64)))s :initial-value 0))

Funny that as a new user I can post my own answer but not comment on someone else's. Oh well, apologies if I'm doing this wrong!

I think you will have to register :) Then you'll be able to comment. - Vivin Paliath
[+3] [2010-04-17 04:54:10] rlbond

MATLAB: 24 characters



>> polyval(input('')-64,26)
(after pressing enter) 'WTF'

ans =


Note: You can get it down to 16 characters if you pre-store the string in x, but I kind of thought it was cheating:

>> x = 'WTF'

x =


>> polyval(x-64,26)

ans =


How could I forget about polyval.. +1 for better solution - George
[+2] [2010-04-14 02:19:40] Vivin Paliath

Perl, 120 characters



vivin@serenity ~/Projects/code/perl/excelc
$ echo WTF | perl
vivin@serenity ~/Projects/code/perl/excelc
$ echo ROFL | perl

I'm sure some of the Perl gurus here can come up with something way smaller.

[+2] [2010-04-14 02:19:45] hobbs

Perl, 47 characters (from stdin)

chop($l=<>);$_=A;$.++,$_++while$_ ne$l;die$.,$/

That's a neat trick! Can you explain how you did it? This is actually the first code golf I've ever tried and I'm not familiar with all the tricky stuff you can do in Perl. - Vivin Paliath
It's abusing the magic thing the ++ operator does on strings -- see perldoc perlop and search "little extra builtin magic". I'll write a more thorough explanation after I take care of some business at work. - hobbs
[+2] [2010-04-14 05:34:31] user316178

JavaScript, 93 characters


+1 That has to be the worst abuse of the with statement I have ever seen. (-: - Na7coldwater
[+2] [2010-04-14 15:35:57] gwell

Lua, 61 characters

x=0 for c in(...):gfind(".")do x=x*26-64+c:byte()end print(x)

[+2] [2010-04-16 16:09:06] sabujp


echo -n WTF | perl -ple '$=()=A..$'

This prints a new line so the answer is more readable on the shell.

[+2] [2010-04-16 17:53:13] Paolo Bonzini

Smalltalk, 72

Smalltalk arguments first reverse inject:0into:[:o :e|o*26+e digitValue]

For a moment I thought you'd written an implementation of Smalltalk-72 for this, then I realized the "72" was the number of characters. - Kragen Javier Sitaker
[+2] [2010-04-16 18:24:52] chebur

PHP: 56 55 characters


PHP: 44 43 characters only for uppercase letters


[+2] [2010-04-16 19:11:39] user1330493

Applescript: 188
Here's the requisite applescript in 188 characters, which is a very difficult language to make non-verbose. It also happens to be the longest answer of any language so far. If anyone knows how to shorten it, do share.

on run s  
 set {o, c} to {0, 0}  
 repeat with i in reverse of (s's item 1)'s characters  
  set m to 26 ^ c as integer  
  set c to c + 1  
  set o to o + ((ASCII number of i) - 64) * m  
 end repeat  
end run

osascript /path/to/script.scpt ROFL

[+2] [2010-04-17 01:22:16] user318965

PHP, 38 chars


usage, e.g.

php -r 'for($a=A;++$c,$a++!=$argv[1];);echo$c;' WTF

[+2] [2012-05-19 09:39:06] Dylan Freedman

APL: 7 characters

Store desired string in variable w:


Assuming characters are lowercase:


Assuming characters are uppercase:


Mixed case or unsure of case (14 chars, but could possibly be improved):


[+1] [2010-04-14 02:21:12] ars


import string

letters = string.uppercase
colnum = lambda s: sum((letters.index(let)+1)*26**idx for idx, let in enumerate(s[::-1]))

print colnum('WTF') 
# 16074
print colnum('ROFL')
# 326676

I dunno about you, but I find that a lot easier to understand than the Perl solution that has almost 2x the characters. +1 for Python-love. - BlueRaja - Danny Pflughoeft
[+1] [2010-04-14 03:49:39] Ross

Java, 164 characters

public class A{public static void main(String[] z){int o=0,c=0;for(int i=z[0].length()-1;i>=0;i--,c++)o+=(z[0].charAt(i)-64)*Math.pow(26,c);System.out.println(o);}}

Java, 177 characters

public class A
public static void main(String[] z)
    int m,o=0,c=0;
    for(int i=z[0].length()-1;i>=0;i--,c++)

Assumes an uppercase input (via command line argument). The obvious approach with no tricks.

[+1] [2010-04-15 06:07:10] Carlos Gutiérrez

dc - 20 chars

(does the opposite)

dc can't handle character input, so I coded the opposite: input the column number and output the column name:

dc exccol.dc

dc can handle character input, just set your base to something like 36 with 36i. - Adam Rosenfield
@Adam - It doesn't work (at least on the GNU version) 36i dc: input base must be a number between 2 and 16 (inclusive) - Carlos Gutiérrez
[+1] [2010-04-16 14:38:49] bflesch

My Javascript solution is just 82 characters long and uses Integer.parseInt with Radix 36. It'd be fine if somebody could appen this to the Javascript section of this thread! :-)

a=function(b){t=0;b.split('').map(function(n){t=parseInt(n,36)-9+t*26});return t};

[+1] [2010-04-16 14:46:14] user318594


<?$t=0;$s=str_split($argv[1]);$z=count($s);foreach($s as$v){$z--;$t+=(ord($v)-64)*pow(26,$z);}echo$t?>

usage: php filename.php ROFL

outputs: 326676

[+1] [2010-04-16 19:49:41] amashi

Python (47 chars)

reduce(lambda a,b:a*26+ord(b)-64,raw_input(),0)

works only on uppercase letters

[+1] [2010-04-17 02:12:06] George

Matlab 38 chars

Works only with uppercase letters. Not sure if it has to work with lowercase too (none in example).


If new lines do not count only 37 (omitting semicolon):


I see Matlab beats a lot of languages. Who would expect that.


Input: 'ROFL' (dont forget the '' )
Output: ans = 326676

[+1] [2010-04-17 03:14:54] user319031

Factor: 47 characters

reverse [ 26 swap ^ swap 64 - * ] map-index sum

[+1] [2010-04-17 07:48:51] Michael

Prolog: 49 chars

c([H|T],I,R):-J is H-64+I*26,c(T,J,R).

Using the above code:

| ?- c("WTF",0,R).
R = 16074 ? 
| ?- c("ROFL",0,R).
R = 326676 ? 

[+1] [2010-04-17 13:26:14] elias

php 29 chars:


  • assuming register_globals=On
  • assuming error_reporting=0
  • call via webserver ?i=A&t=ABC

[+1] [2010-04-17 22:21:32] thepandaatemyface

Python: 88 characters

using list comprehensions:

print sum([((26**(len(s)-i-1))*(ord(s[i])-64)) for i in range(len(s))])

[+1] [2010-04-18 01:13:22] jeremy

Josl in 48 characters

main 0 0 argv each 64 - swap 26 * + next print


$ josl numequiv.j A
$ josl numequiv.j ABC
$ josl numequiv.j ROFL

Reading from standard input:

main 0 STDIN read-line each 64 - swap 26 * + next print

[+1] [2010-04-18 02:21:03] intuited

OOBasic: 178 characters, not counting indentational whitespace


This version passes all the test cases. I suspect that it would be more successfully golf if it didn't "take advantage" of the fact that there's a spreadsheet using this numbering system. See the notes on the original version below for info on why that's not particularly useful. I didn't try very hard to cut down the score.

Also note that this will only work when run as a macro from an OO calc spreadsheet, for obvious reasons.

Function C(st as String) as Long
    C = 0
    while len(st)
        C = C*26 + ThisComponent.Sheets(0).getCellRangeByName(left(st,1) &"1").CellAddress.Column+1
        st = mid(st,2)
End Function


OOBasic (OpenOffice Basic), too many characters (124):

Function C(co As String) As Long 
    C = ThisComponent.Sheets(0).getCellRangeByName(co &"1").CellAddress.Column+1
End Function


  • maximum value of co is AMJ (1024 columns). Anything larger results in an error with a completely uninformative error message.
    • This limitation is also present for the COLUMN() cell function. Presumably this is the maximum number of columns in an OOCalc spreadsheet; I didn't bother scrolling over that far or googling to find out.


  • strangely it's not possible to give the variable 'co' a 1-letter name. Not sure what the logic is behind this, but after having spent enough time using OOBasic you stop looking for logic and begin to blindly accept the way things are (perhaps from gazing too long at the Sun).

Anyway entering =C("A"), =C("ABC"), etc. in a cell works for the first four test cases; the last two give errors.

[+1] [2010-04-18 04:51:22] intuited

straight bash

filter: 97 chars

{ read c;i=0;while [ $c ];do eval s=({A..${c:0:1}});i=$((i*26+${#s[@]}));c=${c:1};done;echo $i;}


echo ROFL | { read c;i=0;while [ $c ];do eval s=({A..${c:0:1}});i=$((i*26+${#s[@]}));c=${c:1};done;echo $i;}

function: 98 chars

C(){ i=0;while [ $1 ];do eval s=({A..${1:0:1}});i=$((i*26+${#s[@]}));set -- ${1:1};done;echo $i;}



Explanation of the filter version:

read c;i=0;

Initialize the column and the total.

while [ $c ];do

while there are still column characters left

eval s=({A..${c:0:1}});

${c:0:1} returns the first character of the column; s=({A..Z}) makes s an array containing the letters from A to Z


$((...)) wraps an arithmetic evaluation; ${#s[@]} is the number of elements in the array $s


${c:1} is the characters in $c after the first. done ends the while loop

echo $i

um i forget

better but dubious

Removing the 5 characters "echo " will result in the output for an input of "ROFL" being

326676: command not found

Also the i=0 is probably not necessary if you're sure that you don't have that variable set in your current shell.

[+1] [2010-04-18 16:18:38] J D

F# (37 chars):

Seq.fold (fun n c -> int c-64+26*n) 0

[+1] [2010-04-20 14:58:21] afshin

K 3.2 (13 characters)

26_sv -64+_ic


  26_sv -64+_ic"ROFL"


  • As mentioned above K evaluates from right to left, so the _ic function takes whatever is to its right and converts it to an integer value, this includes both single characters and character vectors
  • -64 is added to each item in the integer vector that to get a set of base values
  • _sv takes two arguments: the one on its left is the numeric base, 26, and the one on its right is the integer vector of offset values

[+1] [2010-04-27 06:32:18] iDevlop

Excel VBA, 19 characters:


[+1] [2010-07-01 06:36:35] Gerhard

Ruby solution in 26 chars

p ("A"..$*[0]).to_a.size

[0] [2010-04-16 23:50:39] user318967

Real VBA, 216 w/o spaces

I fail at real golf too.

Private Sub CB1_Click()
Dim C, S
S = Len(ActiveCell)
x = 0
C = 0
C = (Asc(Mid(ActiveCell, (S - x), 1)) - 64) * (26 ^ x) + C
x = x + 1
Loop Until x = S
ActiveCell.Offset(0, 1) = C
ActiveCell.Offset(1, 0).Activate
Loop Until ActiveCell = ""
End Sub

Uses Column A for input, outputs to Column B, runs off a VB command button click. =D

[0] [2010-04-17 00:37:44] Philip Robinson

Elang, 53/78

Shell, 53 characters:


Module, 78 characters:


[0] [2010-04-17 01:02:21] user318996

F# 92 chars :)

let e2n (c : string) = c |> (fun x -> (int)x - 64) |> Seq.reduce(fun e a -> a*26+e)

[0] [2010-04-17 04:25:36] DaveG

Groovy: 51 Characters

char[] a=args[0];t=0;for(i in a)t=26*t+i-64;print t

Invoke as

groovy *scriptname* ROFL


groovy -e "char[] a=args[0];t=0;for(i in a)t=26*t+i-64;print t" ROFL

This essentially the same as Java. I imagine some possibilities with using ranges and closures, but nothing came to mind for this example. Anyone else see a way to shorten this?

A more groovy-looking version with a closure is a bit longer, unfortunately.

t=0;args[0].toCharArray().each{t=t*26+it-64};print t

[0] [2010-04-17 05:45:22] Stephen Hsu

Go: 106 characters

It's not the shortest of all the languages. But it can be the shortest of C, C++, Java, and C#.

package main
func main(){t:=0
for _,c := range os.Args[1]{t=t*26+c-64}

Formated version:

package main

import (

func main() {
    t := 0
    for _, c := range os.Args[1] {
        t = t*26 + c - 64

[-1] [2010-04-17 03:55:04] RonnieDickson

Excel - 99 characters

Enter as array formula - I am not counting Excel adding { }

=SUM((CODE(MID(A1,ROW(INDIRECT("1:" & LEN(A1))),1))-64)*26^(LEN(A1)-ROW(INDIRECT("1:" & LEN(A1)))))

[-4] [2010-04-16 13:40:20] alvin

how about a new language
with operators defined as

# - will return the =COLUMN() of EXCEL as a stringised number

, - read in a string

. - write out a string

then the program to do that is


(9) I hope future code golfs create a rule saying The language must be Turing complete to stop these rotten jokes. - kennytm
Simple. Define an extension of HQ9+B (which itself is a Turing-complete extension of HQ9+) that has three new operators... - Jouni K. Seppänen
(8) The actual solution is to get chip manufacturers to implement a machine instruction for these problems. - Jonno_FTW
@Kenny: just another perspective in the spirit of ioccc :)… - alvin
even each package/library used implies a meta-linguistic abstraction. so if i wrote a package to solve this problem with one api call, and accidentally that package got included into the standard library :) of any one the turing complete languages, i would be able to solve the problem in just one api call. will it then be counted as a valid answer. so atleast the language should be constrained to the plain base language, without using any libraries. some of the answers use the math or the string libraries. should'nt the source code size of those calls be counted in too? - alvin
(1) This won't work, due to Excel's column count limitations. In fact this problem cannot be solved in your new language. - intuited
@intuited. that is interesting. - alvin
@alvin: now if you add in a function to convert veganhexisimula [sp!] into decimal, well then you're rockin. But it's still making up the rules as you go, and the rules are "I win." - intuited