小白一枚,python解法,共同学习,一起进步。

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.

解题思路:

找出所有3和5的倍数,不超过最大数n,去重求和即可。

 l = []  # 存放3和5倍数的数
n = 1000 # 最大数
for i in range(1, n):
if 3*i >= n: # 如果大于最大数,终止
break
l.append(3*i)
for i in range(1, n):
if 5*i >= n:
break
l.append(5*i)
l = set(l) # 去重,譬如15 30之类的
print(sum(l))

%time %run ol001.py
233168
Wall time: 2 ms

Problem 1: Multiples of 3 and 5的更多相关文章

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

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

  2. 用PYTHON练练一些算法

    网上一个专门用来给新手练算法的: http://projecteuler.net/problem=1 Multiples of 3 and 5 Problem 1 Published on Frida ...

  3. 2020.4.6--UCF Local Programming Contest 2017的正式赛

    Problem A : Electric Bill 题目大意:进行电量分级制收费,1000kwh及以下一档收费,1000kwh以上按另一档收费,给出每个人的电量总额,问每人应支付多少钱. 思路:基础i ...

  4. (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 ...

  5. 2012Chhengdu K - Yet Another Multiple Problem

    K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  6. Multiples of 3 and 5

    #include<stdio.h> int main(void){ int n1, n2,n3; n1=333*(3+999)/2; n2=199*(5+995)/2; n3=66*(15 ...

  7. algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )

    Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...

  8. Yet Another Multiple Problem(bfs好题)

    Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K ( ...

  9. hdu4474 Yet Another Multiple Problem

    Yet Another Multiple Problem Description There are tons of problems about integer multiples. Despite ...

随机推荐

  1. git误commit大文件导致不能push问题解决

    git push时终端报错: error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Ent ...

  2. Job for docker.service failed because the control process exited with error

    Docker 无法启动 报错信息:Job for docker.service failed because the control process exited with error 找了很久才解决 ...

  3. 小程序之 微信小程序下拉上方出现空白

    往下拉页面后上方出现空白区域  用户需要手动划上去才能消失 方法一:"enablePullDownRefresh":false //这个在page.json中配置 整个页面都不能滑 ...

  4. ES6多层解构

    const info = { person: { name: 'xiaobe', other: { age: 22, } }, song: 'rolling', } // 解构person的内容 co ...

  5. Xilinx Vivado的使用详细介绍(1):创建工程、编写代码、行为仿真

    Xilinx Vivado的使用详细介绍(1):创建工程.编写代码.行为仿真 Author:zhangxianhe 新建工程 打开Vivado软件,直接在欢迎界面点击Create New Projec ...

  6. [python]目录及文件操作

    Python OS模块和shutil模块 获取路径 # 获取当前路径 pwd = os.getcwd() # 获取上级路径 a_pwd = os.path.abspath(os.path.dirnam ...

  7. springboot 启动报错

    有一个警告 :** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the d ...

  8. 内置函数&匿名函数

    1.内置函数     Built-in Functions     abs() dict() help() min() setattr() all() dir() hex() next() slice ...

  9. ECharts前端图形展示

    这次负责慢查询预警,前后端都是自己处理,这次遇到了前端作图的需求,做一个记录以便后续使用: 使用的作图方式是ECharts,相应的example官方有相应的文档和使用方法,比较简单,一下只贴链接: h ...

  10. Spring的事务初见

    一.事务的特性 原子性: 事务是最小的执行单位,不允许分割.事务的原子性确保动作要么全部完成,要么完全不起作用: 一致性: 执行事务前后,数据保持一致: 隔离性: 并发访问数据库时,一个用户的事物不被 ...