-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiplesof3or5.cs
50 lines (40 loc) · 1.46 KB
/
Multiplesof3or5.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
// Multiples of 3 or 5 ( 6 Kyu )
// 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.
// Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
// "Note: If the number is a multiple of both 3 and 5, only count it once. Also, if a number is negative, return 0(for languages that do have them)"
// Courtesy of projecteuler.net
// ALGORITHMS, MATHEMATICS, NUMBERS
namespace CodeWars
{
public static class Kata
{
public static int Solution(int value)
{
int sum = 0;
for (int i = 0; i < value; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
return sum;
}
}
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Kata.Solution(10));
// i % 3 = 0 => 3, 6, 9 < (10)
// i % 5 = 0 => 5 < (10)
// sum = 3 + 5 + 6 + 9 = 23, so the output is 23
Console.WriteLine(Kata.Solution(22));
// i % 3 = 0 => 3, 6, 9, 12, 15, 18, 21 < (22)
// i % 5 = 0 => 5, 10, 15, 20 < (22)
// sum = 3 + 6 + 9 + 12 + 15 + 18 + 21 + 5 + 10 + 15 + 29 = 119, so the output is 119
Console.WriteLine(Kata.Solution(30));
}
}
}