Created
December 12, 2013 15:04
-
-
Save bmoore/7929429 to your computer and use it in GitHub Desktop.
For Frankie about his base_26 problem.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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