Ad

Statement

Given a number n, the function, add all the multiples 3 and 5 in the range [1, n).

Example:

Say, n <- 10

In the range [1, 10):

  • multiples of 3 are 3, 6 and 9.
    • 3 + 6 + 9 = 18
  • There's only one multiple of 5 which is 5 itself, as we are only supposed to count from 1 to n - 1.

Total sum is 18 + 5 = 23.

Note: if a number is a multiple of both 3 and 5, it should be added only once.

Input:

10

Output:

23

Code
Diff
  • package kata
    
    func AddMultiples(n int) (sum int) {
      for i := 1; i < n; i++ {
        if i % 3 == 0 || i % 5 == 0 {
          sum += i 
        }    
      }
      return
    }
    • package kata
    • func Multiple3And5(number int) int {
    • sum := 0
    • for i:= 1;i< number;i++{
    • if i%3==0||i%5==0{
    • sum += i
    • func AddMultiples(n int) (sum int) {
    • for i := 1; i < n; i++ {
    • if i % 3 == 0 || i % 5 == 0 {
    • sum += i
    • }
    • }
    • return sum
    • return
    • }