Problem 1: Multiples of 3 and 5

Coding challenges are a great resource for learning coding techniques and improve analytical thinking, this is a collection of challenges from different platforms.

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
}