Problem 1: Multiples of 3 and 5

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.

Find the sum of all the multiples of 3 or 5 below 1000.

Sample 00

input00.txt
10
output00.txt
23

Sample 01

input01.txt
1000
output01.txt
233168

Solution

main.go
package main

import (
  "fmt"
)

func main() {
  var n int

  fmt.Scan(&n)
  n = n - 1
  fmt.Println(SummationBy(n, 3) + SummationBy(n, 5) - SummationBy(n, 15))
}

func Summation(n int) int {
  return n * (n + 1) / 2
}

func SummationBy(n, m int) int {
  return Summation(n/m) * m
}
Privacy policy

This site uses tracking cookies when:

  • Comments are loaded. If you don’t want them, just don’t click «Show comments».

If you use only Open Source products, sorry about using tracking cookies, I will replace Disqus as my comments platform in the future.

If you use private source products, worrying about privacy and using this products is like worrying about global warming and not recycling.. So just don’t.. 😒