Created
August 17, 2011 07:38
-
-
Save k-holy/1151044 to your computer and use it in GitHub Desktop.
FizzBuzzとProject Euler Problem 1
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
<pre> | |
<?php | |
// array_map()で解くFizzBuzz | |
echo implode("\n", array_map(function($var) { | |
return ($var % 3 * $var % 5) ? $var : ($var % 3 ? '' : 'Fizz') . ($var % 5 ? '' : 'Buzz'); | |
}, range(1, 100))); | |
?> | |
</pre> |
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
<pre> | |
<?php | |
// Project Euler Problem 1 | |
// 10未満の自然数のうち、3 もしくは 5 の倍数になっているものは 3, 5, 6, 9 の4つがあり、 これらの合計は 23 になる。 | |
// 同じようにして、1,000 未満の 3 か 5 の倍数になっている数字の合計を求めよ。 | |
echo array_sum(array_filter(range(1, 999), function($var) { | |
return !($var % 3 * $var % 5); | |
})); | |
// http://my-rest.icca.jp/blog/2011/01/477/ からインスパイア | |
?> | |
</pre> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment