Skip to content

Instantly share code, notes, and snippets.

@rjhilgefort
Created June 1, 2013 03:18
Show Gist options
  • Save rjhilgefort/5689176 to your computer and use it in GitHub Desktop.
Save rjhilgefort/5689176 to your computer and use it in GitHub Desktop.
Multiple ways to generate any number of nines (or any digit for that matter) via PHP
<pre>
<?php
//By @pkspencer
function generateNines_One($numNines)
{
$result = 0;
for ($i=0; $i < $numNines; $i++) {
$result += pow(10, $i);
}
$result = $result * 9;
return $result;
}
//By @collintbrown
function generateNines_Two($numNines)
{
return pow(10, $numNines)-1;
}
//By @rjhilgefort
function generateNines_Three($numNines)
{
return str_pad('', $numNines, "9");
}
print "||| Solution One |||\n";
print "x=5: " . generateNines_One(5) . "\n";
print "x=10: " . generateNines_One(10) . "\n\n";
print "||| Solution Two |||\n";
print "x=5: " . generateNines_Two(5) . "\n";
print "x=10: " . generateNines_Two(10) . "\n\n";
print "||| Solution Three |||\n";
print "x=5: " . generateNines_Three(5) . "\n";
print "x=10: " . generateNines_Three(10) . "\n\n";
/******* OUTPUT *************************************
||| Solution One |||
x=5: 99999
x=10: 9999999999
||| Solution Two |||
x=5: 99999
x=10: 9999999999
||| Solution Three |||
x=5: 99999
x=10: 9999999999
****************************************************/
?>
</pre>
@rjhilgefort
Copy link
Author

Thanks for the contributions:

Solution One
https://twitter.com/pkspencer

Solution Two
https://twitter.com/collintbrown

Solution Three
https://twitter.com/rjhilgefort

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment