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

第一道题的原文如下:

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 = 0
for x in xrange(1000):
    if x % 3 == 0 or x % 5 ==0:
        sum += x
print 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 1000
	mov ecx, 3

	mov esi, 3
	mov edi, 5

	xor ebx, ebx	; sum

_0:	mov eax, ecx
	xor edx, edx
	div esi
	test edx, edx
	je _yes

	mov eax, ecx
	xor edx, edx
	div edi
	test edx, edx
	jne _no

_yes:	add ebx, ecx

_no:	inc ecx
	cmp ecx, 1000
	jne _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

总结:

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


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值