Problem #1 easy
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.
Additional Notes
This is one of the simplest Project Euler problems, often used as an introduction to programming challenges. The solution demonstrates basic loop iteration and conditional logic.
The mathematical approach can be optimized using the formula for the sum of an arithmetic series, but the brute force approach shown here is sufficient for small inputs like 1000.
Implementations
O(n) time complexity
#include <iostream>int sum_natural_35(size_t upper){ unsigned int sum(0); for( int i = upper ; --i; ) { if( 0 == i % 3 ) { sum += i; } else if ( 0 == i % 5 ) { sum += i; } } return sum;}
int main(int argc, char* argv[]){ std::cout << sum_natural_35(1000) << std::endl;}public class Euler001 { public static void main(String[] args) { int sum = 0; for (int i = 1; i < 1000; i++) { if (i % 3 == 0 || i % 5 == 0) { sum += i; } } System.out.println(sum); }}function euler001() { let sum = 0; for (let i = 1; i < 1000; i++) { if (i % 3 === 0 || i % 5 === 0) { sum += i; } } return sum;}
console.log(euler001());<?php$sum = 0;for ($i = 1; $i < 1000; $i++) { if ($i % 3 == 0 || $i % 5 == 0) { $sum += $i; }}echo $sum;?>#!/usr/bin/env rubydef euler001_solution sum = 0 (0..999).each do |num| if num % 3 == 0 || num % 5 == 0 sum += num end end sumend
puts euler001_solutionpackage main
import "fmt"
func main() { sum := 0 for i := 1; i < 1000; i++ { if i%3 == 0 || i%5 == 0 { sum += i } } fmt.Println(sum)}fn main() { let mut sum = 0; for i in 1..1000 { if i % 3 == 0 || i % 5 == 0 { sum += i; } } println!("{}", sum);}