# Project Euler solution: Multiples of 3 and 5

## Problem - 1

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.

Result: 233168

## Solution in multiple languages

PHP:

``````<?php
\$sum = 0;
for (\$n = 0; \$n < 1000; \$n++)
{
if (!(\$n % 5) || !(\$n % 3))
{
\$sum += \$n;
}
}
echo \$sum;
?>
``````

Python:

``````n = 0
for i in range(1,1000):
if not i % 5 or not i % 3:
n = n + i
print(n)
``````

Java

``````public final class eu_p001_sol {

public static void main(String[] args) {
System.out.println(new eu_p001_sol().run());
}
public String run() {
int sum = 0;
for (int x = 0; x < 1000; x++) {
if (x % 5 == 0 || x % 3 == 0)
sum += x;
}
return Integer.toString(sum);
}
}
``````

Ruby:

``````#!/usr/bin­/env ruby
sum = 0
1000.times­ do |n|
if n % 3 == 0 or n % 5 == 0
sum += n
end
end
puts sum
``````

C:

``````#include <stdio.h>

int main(void)
{
int zx, nn3 = 0, nn5 = 0, nn15 = 0;
for (zx = 0; zx < 1000; zx++)
{
if (zx % 3 == 0) {
nn3 += zx;
}
if (zx % 5 == 0) {
nn5 += zx;
}
if (zx % 15 == 0) {
nn15 += zx;
}
}
printf("%d\n", nn3 + nn5 - nn15);
return 0;
}

``````

JavaScript:

``````function euler_project_sol_001()
{
var sum = 0, x = 1000;

while (x --)
{
if ( x % 3 === 0 || x % 5 === 0 )
{
sum += x;
}
}

return sum;
}
console.log(euler_project_sol_001(1000))
``````

Go:

``````package main

func main() {
n, sum := 0, 0
for n < 1000 {
if n%3 == 0 || n%5 == 0 {
sum += n
}
n++
}
println(sum)
}
``````
﻿

New Exercises:R Programming