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
are3
,6
and9
.3 + 6 + 9 = 18
- There's only one multiple of
5
which is5
itself, as we are only supposed to count from1
ton - 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
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 := 0for 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
- }
package kata_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "codewarrior/kata" ) var _ = Describe("Multiples of 3 and 5", func() { It("should handle basic cases", func() { Expect(AddMultiples(10)).To(Equal(23)) Expect(AddMultiples(20)).To(Equal(78)) }) It("should handle zeroes", func() { Expect(AddMultiples(0)).To(Equal(0)) Expect(AddMultiples(1)).To(Equal(0)) }) It("should handle large numbers", func() { Expect(AddMultiples(200)).To(Equal(9168)) }) })
- package kata_test
- import (
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- . "codewarrior/kata"
- )
- var _ = Describe("Multiples of 3 and 5", func() {
- It("should handle basic cases", func() {
Expect(Multiple3And5(10)).To(Equal(23))Expect(Multiple3And5(20)).To(Equal(78))- Expect(AddMultiples(10)).To(Equal(23))
- Expect(AddMultiples(20)).To(Equal(78))
- })
- It("should handle zeroes", func() {
Expect(Multiple3And5(0)).To(Equal(0))Expect(Multiple3And5(1)).To(Equal(0))- Expect(AddMultiples(0)).To(Equal(0))
- Expect(AddMultiples(1)).To(Equal(0))
- })
- It("should handle large numbers", func() {
Expect(Multiple3And5(200)).To(Equal(9168))- Expect(AddMultiples(200)).To(Equal(9168))
- })
- })