Passed
Push — master ( e32f0a...d0fbd1 )
by Daniel
53:32 queued 01:17
created

altSolution()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 1
dl 0
loc 6
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
15
        if ($i % 3 === 0 || $i % 5 === 0) {   
16
            $sum += $i;
17
        }
18
    }
19
    return $sum;
20
}
21
22
// Alternate solution:
23
24
function altSolution($number)
25
{
26
    return array_sum(
27
        array_filter(
28
            range(1, $number-1), function ($item) {
29
                return $item % 3 == 0 || $item % 5 == 0;
30
            }
31
        )
32
    );
33
}