Skip to content

Instantly share code, notes, and snippets.

@boscoworks
Last active August 29, 2015 14:22
Show Gist options
  • Save boscoworks/cc8b6230d2690545446d to your computer and use it in GitHub Desktop.
Save boscoworks/cc8b6230d2690545446d to your computer and use it in GitHub Desktop.
PHPでfizzbuzz問題にトライした話 ref: http://qiita.com/boscoworks/items/215a0804228bf4514588
<?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";
}
<?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