Skip to content

Instantly share code, notes, and snippets.

@bhaskarkc
Created January 14, 2021 07:17
Show Gist options
  • Save bhaskarkc/3e090df51912eae61e8bdec1f35929f2 to your computer and use it in GitHub Desktop.
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
<?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