Project Euler Problem 1: Multiples of 3 and 5

2021年09月15日 阅读数:1
这篇文章主要向大家介绍Project Euler Problem 1: Multiples of 3 and 5,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。
Multiples of 3 and 5

Problem 1python

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.ios

Find the sum of all the multiples of 3 or 5 below 1000.ide


C++:spa

#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]))