Created
January 14, 2021 07:17
-
-
Save bhaskarkc/3e090df51912eae61e8bdec1f35929f2 to your computer and use it in GitHub Desktop.
PHP: return sum of multiple of 3 or 5 from given natural number
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 | |
/** | |
* Returns the sum of all multiples of 3 or 5 | |
* | |
* @param int $number | |
* @return int | |
*/ | |
function multiple_of_3_or_5($number) | |
{ | |
$list = []; | |
for ($i = 1; $i < $number; $i++) { | |
foreach ([3, 5] as $d) { | |
if (0 == $i % $d) { | |
$list[] = $i; | |
break; | |
} | |
} | |
} | |
return array_sum($list); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment