이경수 선생님의 수학실험실

Problem 1(Multiples of 3 and 5) 본문

Project Euler

Problem 1(Multiples of 3 and 5)

(이경수) 2019. 2. 6. 14:05

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]



'Project Euler' 카테고리의 다른 글

Problem 6(Sum square difference)  (0) 2019.02.08
Problem 5(Smallest multiple)  (0) 2019.02.08
Problem 4(Largest palindrome product)  (0) 2019.02.07
Problem 3(Largest prime factor)  (0) 2019.02.06
Problem 2(Even Fibonacci numbers)  (0) 2019.02.06
Comments