Skip to content

Commit b01e0fc

Browse files
committed
Multiples of 3 or 5 solutions
1 parent 49ab039 commit b01e0fc

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object MultiplesOf3Or5 {
2+
def solution(number: Long): Long =
3+
(3L until number).filter(i => i % 3L == 0L || i % 5L == 0L).sum
4+
}

multiples-of-3-or-5/Solution.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class Solution {
2+
3+
public int solution(int number) {
4+
int result = 0;
5+
if (number > 3)
6+
{
7+
int min = 3;
8+
int count = (number - 1) / 3;
9+
int max = number - 1;
10+
while (max % 3 != 0)
11+
{
12+
max--;
13+
}
14+
int tempResult = count * (min + max) / 2;
15+
result += tempResult;
16+
}
17+
if (number > 5)
18+
{
19+
int min = 5;
20+
int count = (number - 1) / 5;
21+
int max = number - 1;
22+
while (max % 5 != 0)
23+
{
24+
max--;
25+
}
26+
int tempResult = count * (min + max) / 2;
27+
result += tempResult;
28+
}
29+
if (number > 15)
30+
{
31+
int min = 15;
32+
int count = (number - 1) / 15;
33+
int max = number - 1;
34+
while (max % 15 != 0)
35+
{
36+
max--;
37+
}
38+
int tempResult = count * (min + max) / 2;
39+
result -= tempResult;
40+
}
41+
42+
return result;
43+
}
44+
}

multiples-of-3-or-5/multiples.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function solution(number){
2+
let result = 0;
3+
for (let i = 3; i < number; i++) {
4+
if(i % 3 === 0 || i % 5 === 0) {
5+
result += i;
6+
}
7+
}
8+
return result;
9+
}

0 commit comments

Comments
 (0)