Last active
August 29, 2015 14:22
-
-
Save boscoworks/cc8b6230d2690545446d to your computer and use it in GitHub Desktop.
PHPでfizzbuzz問題にトライした話 ref: http://qiita.com/boscoworks/items/215a0804228bf4514588
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 | |
for($num = 1; $num <= 100; $num++) { | |
$flag = false; | |
$sum = $num; | |
while($sum > 9) { | |
$sum = array_sum(array_map('intval', str_split((string)$sum, 1))); | |
} | |
if(in_array($sum, array(3, 6, 9), true)) { $flag = true; echo 'fizz'; } | |
if(in_array(substr((string)$num, -1), array('0', '5'), true)) { $flag = true; echo 'buzz'; } | |
if(!$flag) echo $num; | |
echo "\n"; | |
} |
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 | |
for($num = 1; $num <= 100; $num++) { | |
$flag = false; | |
$sum = $num; | |
while($sum > 9) { | |
$sum = array_sum(array_map('intval', str_split((string)$sum, 1))); | |
} | |
if(in_array($sum, array(3, 6, 9), true)) { $flag = true; echo 'fizz'; } | |
if(in_array(substr((string)$num, -1), array('0', '5'), true)) { $flag = true; echo 'buzz'; } | |
if(!$flag) echo $num; | |
echo "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment