project euler 1

来源:互联网 发布:红梅白雪知歌词 编辑:程序博客网 时间:2024/05/04 13:26

题目:

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.

求小于1000的自然数中3或者5的倍数的总和

思路:

这很简单,直接循环判断1000内的每个数字是不是3或者5的倍数,然后求和。用python可以写出超级简短的代码

代码:

python:

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

c++:

#include <bits/stdc++.h>using namespace std;int main(){    int sum = 0;    for(int i = 0; i < 1000; ++i)        if(i % 3 == 0 || i % 5 == 0)            sum += i;    printf("%d\n", sum);    return 0;}