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. C#程序解读

    阅读下面程序,请回答如下问题: 问题1:这个程序要找的是符合什么条件的数? 问题2:这样的数存在么?符合这一条件的最小的数是什么? 问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间 ...

  2. 微信小程序动画技巧

    用微信小程序自带的wx.createAnimation api可创建动画,该动画效果相比css写的动画更流畅. 栗子与用法,见官网:https://mp.weixin.qq.com/debug/wxa ...

  3. clear & file input & reset & file input

    clear & file input & reset & file input Clear <input type="file"> docume ...

  4. 登录窗口不是系统主窗口 但又需要最先显示 用delphi怎么编写代

    主窗体FormShow事件(主窗体为Form1为例,Form2为登陆窗体)   procedure TForm1.FormShow(Sender: TObject); begin if Form2.S ...

  5. 【题解】 bzoj2460: [BeiJing2011]元素 (线性基)

    bzoj2460,戳我戳我 Solution: 线性基板子,没啥好说的,注意long long 就好了 Code: //It is coded by Ning_Mew on 5.29 #include ...

  6. 使用Rider写一个C#的Hello World程序

    1. 安装Rider 首先到Jetbrains官网下载Rider:https://www.jetbrains.com/rider/ 然后到IntelliJ IDEA 注册码获得注册码. 2. 安装do ...

  7. 【CSS】定义元素的位置

    CSS定义元素的位置html元素的position属性,有4个属性值,分别是static.relative.fixed.absolute static: 1.默认值,一般不显式设置为static 2. ...

  8. Linux shell 日期,时间相关的命令

    在shell脚本中,经常要用到跟获取日期相关的东西,这里记录一下Linux shell 获取日期的方法 获取当前日期:today=`date +"%Y-%m-%d"` 获取昨天的日 ...

  9. 转:iOS-生成Bundle包-引入bundle-使用bundle

    在我们使用第三方框架时,常常看到XXX.bundle的文件. 我们找到该文件,显示包内容,大致看到很多资源文件:图片.配置文本.XIB文件……   什么是Bundle文件? 简单理解,就是资源文件包. ...

  10. vue.js react.js angular.js三者比较

    react和vue有许多相似之处,他们都有:1.使用虚拟DOM2.提供了响应式(reactive)和组件化(composable)的视图组件3.将注意力集中保持在核心库,而将其他功能如路由和全局状态管 ...