欧拉计划---0001 Multiples of 3 and 5(1000以下3和5的倍数)

来源:互联网 发布:淘宝定制怎么弄 编辑:程序博客网 时间:2024/06/06 20:08

第一道题的原文如下:

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.


翻译过来应该是

1000以下3和5的倍数

10以下的自然数是3或5的倍数的数字有3、5、6和9,他们的和是23.请找出1000以下所有自然数中,是3和5的倍数的数字的和。


python代码:

sum = 0for x in xrange(1000):    if x % 3 == 0 or x % 5 ==0:        sum += xprint sum



c代码:
#include <stdio.h>int main(){int sum = 0;int i = 1;for(; i < 1000; i++){if (i%3 == 0 || i%5 == 0){sum += i;}}printf("%d\n", sum);return 0;}


c++代码:

#include <iostream>using namespace std;int main(){int sum = 0;for(int i = 1; i < 1000; i++){if (i%3 == 0 || i%5 == 0){sum += i;}}cout << sum << endl;return 0;}


这上面是我自己写的代码,之后的代码是大神写的,屌丝这里先给大神跪了,然后慢慢学习。

一个大神直接使用x86汇编指令写了,代码如下

; for each integer from 1 to 1000mov ecx, 3mov esi, 3mov edi, 5xor ebx, ebx; sum_0:mov eax, ecxxor edx, edxdiv esitest edx, edxje _yesmov eax, ecxxor edx, edxdiv editest edx, edxjne _no_yes:add ebx, ecx_no:inc ecxcmp ecx, 1000jne _0
数学系的大神给出了如下的解法:

He is using a clever improvisation of the formulae for arithmetic progressions. For example, to find the sum of the terms 3,6,9,12,..., you would use (n/2)(a+l), where n is the number of terms, a is the first term, and l is the last term. But to find the last term requires a bit of work. The nth term is given by a+(n-1)d, where d is the common difference. So we need to solve 3+3(n-1)=1000, giving 3(n-1)=997, and n=997/3+1=333.333... However, n must be integer, so int(333.333...)=333, and checking, 3+3(333-1)=999; this is the last term before 1000.In general, a+(n-1)d=x, gives n=int((x-a)/d+1).But for this problem we can improve even further, as a=d we get n=int(x/d-d/d+1)=int(x/d).The nth (last) term, l=a+(n-1)d=d+(int(x/d)-1)*d=d*int(x/d).Combining this to find the sum, S=(n/2)(a+l)=(int(x/d)/2)*(d+d*int(x/d)).Simplifying, S=d*int(x/d)*(1+int(x/d))/2.As the problem asks for the sum of multiples of 3 and 5 we find the sum of each series, but as 3,6,9,... and 5,10,15,... have multiples of 15 in common, we need to subtract the series for 15,30,45,...However, caution is needed. The problem statesbelow then 1000, so we must use 999 in the formula (otherwise it would include 1000 in the sum, as a multiple of 5):T = 3*int(999/3)*(1+int(999/3))/2 + 5*int(999/5)*(1+int(999/5))/2 - 15*int(999/15)*(1+int(999/15))/2Therefore, T = 3*333*(1+333)/2 + 5*199*(1+199)/2 - 15*66*(1+66)/2 = 233168.

另外一种解法如下:

First of all, stop thinking on the number 1000 and turn your attention to the number 990 instead. If you solve the problem for 990 you just have to add 993, 995, 996 & 999 to it for the final answer. This sum is (a)=3983Count all the #s divisible by 3: From 3... to 990 there are 330 terms. The sum is 330(990+3)/2 (b)=163845 Count all the #s divisible by 5: From 5... to 990 there are 198 terms. The sum is 198(990+5)/2 (c)=98505Now, the GCD (greatest common divisor) of 3 & 5 is 1 so the LCM (least common multiple) should be 3x5 15.This means every number that divides by 15 was counted twice and it should be done only once. Because of this, you have an extra set of numbers started with 15 all the way to 990 that has to be removed from (b)&(c).Then, from 15... to 990 there are 66 terms and the sum is 66(990+15)/2 (d)=33165The answer for the problem is: (a)+(b)+(c)-(d) = 233168

大神的代码转载自:点击打开链接

本题的正确答案:233168

总结:

这个题目是基本题目,如果只是考虑基本的做法,那就只锻炼基本语法和基本的思维逻辑。对于使用汇编的大神和数学方向的大神,我们就多学习,多膜拜。编程之路永无止境。。。。


0 0