Re: Number to Letter Counting algorithim needed



On Jun 16, 12:38 pm, "Gary H" <nos...@xxxxxxxxx> wrote:

im trying to convert numbers into a character sequence
as such that A = 1 to Z = 26, but when you get to 27 it
becomes AA all the way to 702 which is ZZ then 703 is AAA

I posted my answer and code earlier but the system have
gobbled it. Let me try again. The first part of my perl
code is the main subroutine. The 2nd part is just to
provide an illustration and test. Each line provides
output for the respective range of 8 integer arguments,
first from 1 to 8, then for the ranges: 26-3 to 26+4,
13*26-3 to 13*26+4, 26*26-3 to 26*26+4, 13*26*26-3 to
13*26*26+4, etc, so that you can see what's going on.
Here is the output:


A B C D E F G H

W X Y Z AA AB AC AD
LW LX LY LZ MA MB MC MD
YW YX YY YZ ZA ZB ZC ZD
LZW LZX LZY LZZ MAA MAB MAC MAD
YZW YZX YZY YZZ ZAA ZAB ZAC ZAD
LZZW LZZX LZZY LZZZ MAAA MAAB MAAC MAAD
YZZW YZZX YZZY YZZZ ZAAA ZAAB ZAAC ZAAD
LZZZW LZZZX LZZZY LZZZZ MAAAA MAAAB MAAAC MAAAD
YZZZW YZZZX YZZZY YZZZZ ZAAAA ZAAAB ZAAAC ZAAAD
LZZZZW LZZZZX LZZZZY LZZZZZ MAAAAA MAAAAB MAAAAC MAAAAD


And below is the code.
Regards,

Wlod

#!/usr/bin/perl
#
# wh, 2007-June

use integer;

@alf =
qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);

sub intlet
{ --$n;
@lab = ();
if ($n<26) { $lab[0]= $alf[$n] }
else
{ do
{ $r = $n % 26;
$n /= 26;
unshift @lab, $alf[$r]
} while($n > 26);
unshift @lab, $alf[--$n]
} }

#==========================#
#=== test test test ===#
#==========================#

sub show
{ for $n ($pow-3..$pow+4)
{ intlet();
print " ", @lab
}
print "\n"
}

### main ###

print "\n";
$pow=4; show();
print "\n";

$pow=13;
for (0..4)
{ $pow *= 2;
show();
$pow *= 13;
show()
}
print "\n\n";




.


Quantcast