solution()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
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.
4
5
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
6
7
Note: If the number is a multiple of both 3 and 5, only count it once.
8
*/
9
10
function solution($number)
11
{
12
    $sum = 0;
13
    for ($i = 3; $i < $number; $i++) {
14
        if ($i % 3 === 0 || $i % 5 === 0) {
15
            $sum += $i;
16
        }
17
    }
18
    return $sum;
19
}
20
21
// Alternate solution:
22
23
function altSolution($number)
24
{
25
    return array_sum(
26
        array_filter(
27
            range(1, $number-1),
28
            function ($item) {
29
                return $item % 3 == 0 || $item % 5 == 0;
30
            }
31
        )
32
    );
33
}
34