-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.php
More file actions
36 lines (29 loc) · 830 Bytes
/
1.php
File metadata and controls
36 lines (29 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/**
* If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these
* multiples is 23.
* Find the sum of all the multiples of 3 or 5 below 1000.
*/
function findSumOfMultiplesOf3Or5Below(int $limit): int
{
return findSumOfMultiplesOf3Below($limit) + findSumOfMultiplesOf5ButNot3Below($limit);
}
function findSumOfMultiplesOf3Below(int $limit): int
{
$sum = 0;
for ($multiple = 3; $multiple < $limit; $multiple += 3) {
$sum += $multiple;
}
return $sum;
}
function findSumOfMultiplesOf5ButNot3Below(int $limit): int
{
$sum = 0;
for ($multiple = 5; $multiple < $limit; $multiple += 5) {
if ($multiple % 3 != 0) {
$sum += $multiple;
}
}
return $sum;
}
echo findSumOfMultiplesOf3Or5Below(1000);