Skip to content

Instantly share code, notes, and snippets.

@bmoore
Created December 12, 2013 15:04
Show Gist options
  • Save bmoore/7929429 to your computer and use it in GitHub Desktop.
Save bmoore/7929429 to your computer and use it in GitHub Desktop.
For Frankie about his base_26 problem.
<?php
function base_26($num)
{
$alpha = array('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');
// Get the character
$i = $num%26;
// Get the rest of the characters "power/base" math
$num = ($num - $i)/26;
// Recurse
if ($num > 0)
{
//minus 1 because the index is 0
return base_26($num-1) . $alpha[$i];
}
return $alpha[$i];
}
for ($i = 0; $i<1000; $i++)
{
echo $i.": ";
echo base_26($i);
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment