Created
December 4, 2012 09:09
-
-
Save Jeffrey04/4202069 to your computer and use it in GitHub Desktop.
Count the number of digits of a number
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
#!/usr/bin/env php | |
<?php | |
function count_digits($number, $base = 10) { | |
return get_n( | |
$base, | |
intval(is_string($number) ? strval($number) : $number, $base) | |
); | |
} | |
function get_n($base, $number, $n = 1) { | |
return $number < pow($base, $n) ? | |
$n | |
: get_n($base, $number, $n + 1); | |
} | |
var_dump(count_digits(0xFFF, 16)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment