Project Euler Problem 1: Multiples of 3 and 5

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.


C++:

#include <iostream>

using namespace std;

int main()
{
    int n, sum;

    while(cin >> n) {
        sum = 0;
        for(int i=1; i<n; i++)
            if(i % 3 == 0 || i % 5 == 0)
                sum += i;
        cout << sum << endl;
    }

    return 0;
}

Python:
print(sum([x for x in range(1000) if x%3==0 or x%5==0]))




posted on 2017-03-17 22:21  海岛Blog  阅读(103)  评论(0编辑  收藏  举报

导航