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. python matploblib学习笔记

    import matplotlib.pyplot as pltimport numpy as np # matplotlib画图注释中文需要设置from matplotlib.font_manager ...

  2. 动态规划刷题集python代码

    1 爬楼梯(Fibonacci) #有一楼梯共M级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? def fun(m): c = [0]*m c[0] = 1 c[1] = 2 for i ...

  3. NServiceBus官方文档翻译(二)NServiceBus 入门

    在这篇教程中我们将学习如何创建一个非常简单的由客户端向服务端发送消息的订单系统.该系统包括三个项目:Client.Server 和 Messages,我们将按照以下步骤来完成这个任务. 创建 Clie ...

  4. 在PHPStorm中快速插入当前日期

    在EditPlus中使用快捷键Ctrl+D即可插入当前日期,但在PHPStorm中似乎没有这样的快捷键,那如何实现快速插入当前日期呢?其实很简单,跟我做一遍你就会了: 目标 为PHPStorm定义一个 ...

  5. Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 2. 变量

    大家在中学就已经学过变量的概念了.例如:我们令 x = 100,则可以推出 x*2 = 200 试试下面这段 Python 代码 import turtle turtle.shape("tu ...

  6. CUDA ---- 线程配置

    前言 线程的组织形式对程序的性能影响是至关重要的,本篇博文主要以下面一种情况来介绍线程组织形式: 2D grid 2D block 线程索引 矩阵在memory中是row-major线性存储的: 在k ...

  7. 【设计模式】—— 迭代模式Iterator

    前言:[模式总览]——————————by xingoo 模式意图 提供一个方法按顺序遍历一个集合内的元素,而又不需要暴露该对象的内部表示. 应用场景 1 访问一个聚合的对象,而不需要暴露对象的内部表 ...

  8. JDK8新特性,给接口添加一个默认实现

    在JDK8中,允许给接口本身添加一个默认的实现.用“default”进行修饰.如下实例 package interfacetest; public interface TestInterface { ...

  9. VS2017企业版本(安装包+key)+ .NET Reflector 9.0

    关于VS2017安装的一点扩充说明(15.5):http://www.cnblogs.com/dunitian/p/8051985.html Key激活无需断网 Visual Studio 2017 ...

  10. 2017年8道php面试题(真题+答案)

    1.<?php echo count(strlen(“http://php.net”)); ?>的执行结果是? 答案:1 .如果var是普通变量,则返回1.正常情况下返回var中的元素或属 ...