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. Linux基础三(软件安装管理)

    目录: 一.Linux 中软件包的分类 1.源码包 2.二进制包 3.源码包 4.软件安装的选择 二.软件安装之 RPM 1.背景知识 2.准备知识 3.安装升级与卸载 4.查询校验与提取 三.软件安 ...

  2. webgl 模板缓冲

    先思考个问题, 想实现遮罩怎么办? <!doctype html> <html> <head> <meta charset="utf-8" ...

  3. Shell命令——文件目录

    Linux只有一个文件系统树,不同的硬件设备可以挂载在不同目录下. 文件或目录有两种表示方式:  - 绝对路径:从根目录”/”开始  - 相对路径:从工作目录开始,使用”..”指向父目录,”.”指向当 ...

  4. 07.基于IDEA+Spring+Maven搭建测试项目--logback.xml配置

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- 控制台日志 ...

  5. bzoj 2243: [SDOI2011]染色 (树链剖分+线段树 区间合并)

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 9854  Solved: 3725[Submit][Status ...

  6. 【刷题】LOJ 6011 「网络流 24 题」运输问题

    题目描述 W 公司有 \(m\) 个仓库和 \(n\) 个零售商店.第 \(i\) 个仓库有 \(a_i\) 个单位的货物:第 \(j\) 个零售商店需要 \(b_j\) 个单位的货物.货物供需平衡, ...

  7. Java 使用 Enum 实现单例模式

    在这篇文章中介绍了单例模式有五种写法:懒汉.饿汉.双重检验锁.静态内部类.枚举.如果涉及到反序列化创建对象时推荐使用枚举的方式来实现单例,因为Enum能防止反序列化时重新创建新的对象.本文介绍 Enu ...

  8. zabbix server安装(二)

    https://mp.weixin.qq.com/s/ogaqiX4vhtGLepuNf-1ItA zabbix依赖LNMP或LAMP,下面讲解LNMP安装到zabbix web页面的访问. 一.ng ...

  9. python中的functools模块

    functools模块可以作用于所有的可以被调用的对象,包括函数 定义了__call__方法的类等 1 functools.cmp_to_key(func) 将比较函数(接受两个参数,通过比较两个参数 ...

  10. 【CSS】盒子模型的计算

    1.标准盒子的尺寸计算 盒子自身的尺寸:内容的宽高+两侧内边距+两侧边框 盒子在页面中占位的尺寸:内容的宽高+两侧内边距+两侧边框+两侧外边距 <!DOCTYPE html> <ht ...