Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to find the sum of all the multiples of 3 or 5 below 1000 in Go

2 Answers

0 votes
package main

import (
    "fmt"
)

func main() {
    sum := 0

    // Iterate through numbers from 0 to 999
    for x := 0; x < 1000; x++ {
        if x % 3 == 0 || x % 5 == 0 {
            sum += x
        }
    }

    fmt.Println(sum)
}




/*
run:

233168

*/

 



answered Apr 14, 2025 by avibootz
0 votes
package main

import (
    "fmt"
)

func main() {
    // Define the limit
    limit := 999

    // Calculate the upper bounds
    upperForThree := limit / 3
    upperForFive := limit / 5
    upperForFifteen := limit / 15

    // Calculate the sums using arithmetic series formula
    sumThree := 3 * upperForThree * (1 + upperForThree) / 2
    sumFive := 5 * upperForFive * (1 + upperForFive) / 2
    sumFifteen := 15 * upperForFifteen * (1 + upperForFifteen) / 2

    // Calculate the total sum
    totalSum := sumThree + sumFive - sumFifteen

    fmt.Println(totalSum)
}




/*
run:

233168

*/

 



answered Apr 14, 2025 by avibootz
...