Problem 1:Find the sum of all the multiples of 3 or 5 below 1000.

2 篇文章 0 订阅
1 篇文章 0 订阅

Problem 1: 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.

Answer1:

As you have solved this problem we do not have to explain that all numbers divisible by 3
and/or by 5 should be counted.
So the first numbers to be added would be:
3, 5, 6, 9, 10, 12, 15 and so on.

A simple way to do this is to go through all numbers from 1 to 999 and test whether they are
divisible by 3 or by 5.
This would result in code like:

target=999
sum=0
for i=1 to target do
if (i mod 3=0) or (i mod 5)=0 then sum:=sum+i
output sum

(In some programming languages the mod operator is written as %)

Python example:

from datetime import datetime
sum=0
c1 = datetime.now()
for i in range(1,10000000):
    if not i%3 or not i%5:
        # print i
        sum=sum+i

print "sum:",sum
c2 = datetime.now()
print 'elapsed time:',(c2-c1)         #elapsed time

Answer2:

Simple enough you might say.
But wait a minute: if we had asked to do the same for all numbers less than 1,000,000,000 that
is going to take quite a while. Perhaps you would like to try out that first (make sure your sum
variable does not overflow).

To get a more efficient solution you could also calculate the sum of the numbers less
than1000 that are divisible by 3, plus the sum of the numbers less than1000 that are divisible
by 5. But as you have summed numbers divisible by 15 twice you would have to subtract the
sum of the numbers divisible by 15.

If we now define a function:

Function SumDivisibleBy(n)
Details to be filled in
EndFunction

Then the answer would be

SumDivisibleBy(3)+SumDivisibleBy(5)-SumDivisibleBy(15)

Let’s look at the details of our function and take as example n=3.
We would have to add:
3+6+9+12+……+999=3*(1+2+3+4+…+333)
For n=5 we would get:
5+10+15+…+995=5*(1+2+….+199)
Now note that 199=995/5 but also 999/5 rounded down to the nearest integer.
In many programming languages there exists a separate operator for that: div or .
If we now also note that 1+2+3+…+p=½p(p+1) our program becomes:

target=999
Function SumDivisibleBy(n)
p=target div n
return n*(p*(p+1)) div 2
EndFunction
Output SumDivisibleBy(3)+SumDivisibleBy(5)-SumDivisibleBy(15)

Python example

target = 10000000
def SumDivisibleBy(x):
    p=target/ x
    return x*(p*(p+1))/2
c1 = datetime.now()
b=SumDivisibleBy(3)+SumDivisibleBy(5)-SumDivisibleBy(15)
print b
c2 = datetime.now()
print 'elapsed time:',(c2-c1)        #elapsed time

output of two answer:

sum: 23333331666668
elapsed time: 0:00:01.873000
23333341666668
elapsed time: 0:00:00
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值