Problem 1(Multiples of 3 and 5)
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.
In Python:
sum=0
for i in range(1,1000):
if i%3==0 or i%5==0:
sum+=i
print(sum)
Run time: 0.00024700164794921875 seconds
In Python:
print(sum([i for i in range(1, 1000) if i % 3 == 0 or i % 5 == 0]))
Run time: 0.0010218620300292969 seconds
In Java:
public class Euler1 {
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);
}
}
Run time:
In C#:
using System;
namespace ProjectEuler
{
public class problem01
{
static void Main(string[] args)
{
int sum = 0;
for (int i = 1; i < 1000; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
Console.WriteLine(sum);
}
}
}
solution: 233168
[from Project Euler: https://projecteuler.net/problem=1]