본문 바로가기

Programming/Project Euler

Project Euler - Problem 1

Project Euler

프로젝트 오일러!

프로그래밍 공부하시는 분들이라면 언젠가는 한번쯤은 들어보셨을겁니다.

저도 여기저기 소스검색하다가 발견했네요. 문제들이 아주 많습니다. (517개)

언제까지 할지는 모르겠습니다만, 공부도 할 겸 하나씩 하나씩 결과들을 올려볼까합니다.


저는 아마추어 프로그래머로 취미로 하는 만큼 코딩실력도 딸리고, 엉성합니다. (시작한지 이제 4개월차...)

같이 공부하는 것이니만큼 더 좋은 솔루션이 있으면 알려주세요.


1번 문제부터 시작합니다.




Problem 1, (https://projecteuler.net/problem=1)

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.


Python 풀이)


result = 0

for i in range(1000):

    if (i%3 == 0 or i%5 ==0):

        result += i


print(result) 

결과 : 233168


C++ 풀이)


 #include <iostream>

using namespace std;

int main()

{

    int result=0;

    for (int i=0; i<1000; i++)

    {

        if (i%3 == 0 || i%5 ==0)

            result += i;

    }

    cout << result << endl;

    return 0;

}

결과 : 233168



같은 방법으로 Python과 C++로 풀어봤습니다.

역시 Python이 훨씬 간결합니다. (한줄 코딩도 가능하더라구요.)