in reply to
Base36 numbers: speed and golf
Of course, I can't say whether this is the fastest but it was designed to be fast.
#!/usr/bin/perl -w
use strict;
my %next;
BEGIN {
@next{'0'..'9','A'..'Z'}= ('1'..'9','A'..'Z','0');
}
sub inc36 {
my $add= "";
my $dig;
while( not $dig= $next{chop($_[0])} ) {
$add .= $dig;
if( ! length($_[0]) ) {
$dig= '1';
last;
}
}
$_[0] .= $dig . $add;
}
while( <DATA> ) {
for( split " " ) {
print "$_ + 1 = ";
inc36( $_ );
print $_,$/;
}
}
__END__
0 1 9 A Y Z 10 19 1Z 9Z AZ ZZ YZZ XYZZY XYZZZ
which outputs:
0 + 1 = 1
1 + 1 = 2
9 + 1 = A
A + 1 = B
Y + 1 = Z
Z + 1 = 10
10 + 1 = 11
19 + 1 = 1A
1Z + 1 = 20
9Z + 1 = A0
AZ + 1 = B0
ZZ + 1 = 100
YZZ + 1 = Z00
XYZZY + 1 = XYZZZ
XYZZZ + 1 = XZ000
-
tye
(but my friends call me "Tye")