#! /usr/bin/perl -w
use strict;
use Math::BaseCalc;
my $b36 = Math::BaseCalc->new( digits => [0..9, 'a' .. 'z'] );
my $b64 = Math::BaseCalc->new( digits => [0..9, 'a' .. 'z', 'A' .. 'Z'
+, '-', '_' ] );
my $num;
for $num( @ARGV ) {
print "$num\n";
print "\t in base 36 = ", $b36->to_base($num), "\n";
print "\t in base 64 = ", $b64->to_base($num), "\n\n";
}
The above code produces the following results:
% ./b36 1 10 1000 1000000 100000000 1000000000
1
in base 36 = 1
in base 64 = 1
10
in base 36 = a
in base 64 = a
1000
in base 36 = rs
in base 64 = fE
1000000
in base 36 = lfls
in base 64 = 3Q90
100000000
in base 36 = 1njchs
in base 64 = 5Zu40
1000000000
in base 36 = gjdgxs
in base 64 = XCIE0
Which means that somewhere before 100 000 000 you can shave off a whole character!
long silly update
Over the lunch hour I pondered about the sh?aving of one character in the URLs. For certain values, the base-64 representation will catch up to the base-36 representatio. For instance, for n=1 679 616, the base-36 repesentation is 10 000 (5 characters long) and the base-64 representation is only 4 characters long (6q40).
But bump the counter along for a while until we get to
n=16 777 216, and the base-64 representation is the same length as the base-36 representation (10000 and 9xlds respectively).
So I started wondering what value of n will have a representation in base-36 that is evermore longer than the representation in base-64?
Just before heading out to lunch, I whipped up the following code and let it run:
my @prev = (0,0,0);
my @n = (0,0,0);
my $num = 0;
{
$n[0] = ++$num;
$n[1] = $b36->to_base($num);
$n[2] = $b64->to_base($num);
if( grep { $prev[$_] < length($n[$_]) } 0..2 ) {
print join( ' ', @n ), "\n";
}
$prev[$_] = length($n[$_]) for 0..2;
redo;
}
This is an absolute prolifligate waste of cycles to search for the results. After an hour and a half it had gotten as far as a dozen or so lines of output:
1 1 1
10 a a
36 10 A
64 1s 10
100 2s 1A
1000 rs fE
1296 100 kg
4096 35s 100
10000 7ps 2sg
46656 1000 bp0
100000 255s oqw
262144 5m9s 1000
1000000 lfls 3Q90
1679616 10000 6q40
10000000 5yc1s C9q0
16777216 9zlds 10000
60466176 100000 3CGg0
100000000 1njchs 5Zu40
Clearly, something better than brute force was required. So then I figured a much smarter way of going about this would be to calculate the powers of 10, 36 and 64 and print them out. I then bump the power of the smallest result and start again:
#! /usr/local/bin/perl -w
use strict;
use Math::BaseCalc;
use List::Util qw/min/;
my @base = (10, 36, 64);
my @cvt = (
Math::BaseCalc->new( digits => [0..9] ),
Math::BaseCalc->new( digits => [0..9, 'a' .. 'z'] ),
Math::BaseCalc->new( digits => [0..9, 'a' .. 'z', 'A' .. 'Z', '-',
+ '_' ] ),
);
my @power = (0, 0, 0);
my @result = (0, 0, 0);
{
my $min = min( @result );
print join( ' ', map { $cvt[$_]->to_base($min)
. '(' . length($cvt[$_]->to_base($min)) . ')' } 0..2 ), "\n";
for( my $j = 0; $j < @power; ++$j ) {
if( $result[$j] == $min ) {
$power[$j]++;
$result[$j] = $base[$j] ** $power[$j];
}
}
redo;
}
This quickly runs off the end of 64-bit integer support on my machine but nevertheless calculates (I added the length of the representation in parentheses because it was easier on the eyeballs) the following results:
0(1) 0(1) 0(1)
10(2) a(1) a(1)
36(2) 10(2) A(1)
64(2) 1s(2) 10(2)
100(3) 2s(2) 1A(2)
1000(4) rs(2) fE(2)
1296(4) 100(3) kg(2)
4096(4) 35s(3) 100(3)
10000(5) 7ps(3) 2sg(3)
46656(5) 1000(4) bp0(3)
100000(6) 255s(4) oqw(3)
262144(6) 5m9s(4) 1000(4)
1000000(7) lfls(4) 3Q90(4)
1679616(7) 10000(5) 6q40(4)
10000000(8) 5yc1s(5) C9q0(4)
16777216(8) 9zlds(5) 10000(5)
60466176(8) 100000(6) 3CGg0(5)
100000000(9) 1njchs(6) 5Zu40(5)
1000000000(10) gjdgxs(6) XCIE0(5)
1073741824(10) hra0hs(6) 100000(6)
2176782336(10) 1000000(7) 21LN00(6)
10000000000(11) 4ldqpds(7) 9k2-g0(6)
68719476736(11) vkhsvls(7) 1000000(7)
78364164096(11) 10000000(8) 18-TA00(7)
100000000000(12) 19xtf1ts(8) 1t8tKw0(7)
1000000000000(13) cre66i9s(8) ezkFh00(7)
2821109907456(13) 100000000(9) F3ngg00(7)
4398046511104(13) 1k4fnc6ps(9) 10000000(8)
10000000000000(14) 3jlxpt2ps(9) 2hxesG00(8)
100000000000000(15) zg3d62r5s(9) mLcguA00(8)
101559956668416(15) 1000000000(10) n5V59000(8)
281474976710656(15) 2rrvthnxts(10) 100000000(9)
1000000000000000(16) 9ugxnorjls(10) 3znWANE00(9)
3656158440062976(16) 10000000000(11) c_k6V4000(9)
10000000000000000(17) 2qgpckvng1s(11) zxL9LMg00(9)
All of which lets us deduce the most important fact: if you have more than 78 364 164 096 URLs to represent, use a base-64 representation will give you a consistent 1-character gain in your resulting URLs. The importance of this finding should not be underestimated.
- another intruder with the mooring of the heat of the Perl
|