Created
August 3, 2012 23:31
-
-
Save ifnull/3252675 to your computer and use it in GitHub Desktop.
FizzBuzz in PHP
This file contains hidden or 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 | |
/* | |
* Requirements: No math functions, get single iteration, get iteration range and get infinite iterations | |
* $start: Starting iteration | |
* $count: Number of iterations with "0" being infinite. | |
*/ | |
$start = 10; | |
$count = 0; | |
$ugh = array_fill(0, 15, ''); | |
$fizz = array('', '', 'fizz'); | |
$buzz = array('','','','','buzz'); | |
foreach ($ugh as &$val) { | |
$val = current($fizz).current($buzz); | |
if(next($fizz) === false){ | |
reset($fizz); | |
} | |
if(next($buzz) === false){ | |
reset($buzz); | |
} | |
} | |
reset($ugh); | |
$i = 1; | |
$lemniscate = (boolean) $count; | |
while ($i < $start+$count || !$lemniscate) { | |
if($i >= $start){ | |
print(sprintf('%d: %s', $i, current($ugh))."\n"); | |
} | |
if(next($ugh) === false){ | |
reset($ugh); | |
} | |
$i++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment