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.

the direct way  the answer :

def isMutiple(target, divider1, divider2):
if (target % divider1 == 0) or (target % divider2 == 0):
return True
return False a = 3
b = 5
total = 0
for i in range(1, 1000):
if isMutiple(i, a, b):
total += i
print("Total result is : ", total)

The solution above needs to iterate everyone from 1 to 1000, Considering that 1 times, 2 times ,3 times of one numbers, we can rewrite it .

Here is folllows:

def getCount(start, end, divider):
startDividend = 0
endDividend = 0
if start%divider == 0:
startDividend = start
else:
startDividend = (int)(start/divider + 1)*divider
if end%divider == 0:
endDividend = end
else:
endDividend = (int)(end/divider)*divider
count = int((startDividend + endDividend)/2*((endDividend-startDividend)/divider + 1)) return count a = 0
b = 999
total = 0
total += getCount(a, b, 3)
total += getCount(a, b, 5)
total -= getCount(a, b, 15) #minus the common mutiples of 3 and 5 print("Total result is : ", total)

Project Euler Problem1的更多相关文章

  1. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  2. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  3. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  4. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

  5. project euler 169

    project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...

  6. 【Project Euler 8】Largest product in a series

    题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...

  7. Project Euler 第一题效率分析

    Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...

  8. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  9. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

随机推荐

  1. final阶段140字评论

    按课堂顺序 1约跑APP ,无论从页面还是从功能来看完整度都很高了.演示也用了能展示的方式.多些趣味性就更赞了. 2礼物挑选小工具,做了能在规定时间内的功能.也算是对礼物固话的一个成本最低的回应. 3 ...

  2. div文本垂直居中(div text vertical aligan)

    .box{ width: 135px;height: 84px;display: block; overflow: hidden; } .container { background:darkcyan ...

  3. 【题解】 bzoj2748 [HAOI2012]音量调节 (动态规划)

    懒得复制,戳我戳我 Solution: 傻逼题目,直接dp就可以了,他是求最后一次的最大值 Code: //It is coded by Ning_Mew on 4.17 #include<bi ...

  4. 51nod1229 序列求和 V2 【数学】

    题目链接 B51nod1229 题解 我们要求 \[\sum\limits_{i = 1}^{n}i^{k}r^{i}\] 如果\(r = 1\),就是自然数幂求和,上伯努利数即可\(O(k^2)\) ...

  5. 《剑指offer》— JavaScript(31)整数中1出现的次数(从1到n整数中1出现的次数)

    整数中1出现的次数(从1到n整数中1出现的次数) 题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12 ...

  6. UDP ------ UDP Broadcast Address

    Related information link : 百度百科---->广播地址 Use restrictions: 1. You can only broadcast on the same ...

  7. java基础基础总结----- RunTime

  8. 如何把你的eclipse编辑器修改成黑色的主题

  9. Hadoop生态圈-Kafka的完全分布式部署

    Hadoop生态圈-Kafka的完全分布式部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要内容就是搭建Kafka完全分布式,它是在kafka本地模式(https:/ ...

  10. element-ui合并行:span-method

    objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { if (rowIndex % 2 ...