https://projecteuler.net/problem=1
2016-02-14 13:52
615 查看
自己做题玩
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 *(1+... + 333) = 3的倍数和
5 * (1+...+199) = 5的倍数和
15 * (1 + ... 66) = 15的倍数和
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.
#最笨的办法,直接去算
</pre><pre name="code" class="python">def sum_multiples(): sum_m = 0 for i in range(3,1000): if i % 3 == 0 or i % 5 == 0: sum_m += i return sum_m print(sum_multiples())
或者
3 *(1+... + 333) = 3的倍数和
5 * (1+...+199) = 5的倍数和
15 * (1 + ... 66) = 15的倍数和
target = 999 def sumDivisibleBy(n): p = target // n return n * p * (p + 1) / 2 print(sumDivisibleBy(3) + sumDivisibleBy(5) - sumDivisibleBy(15))
相关文章推荐
- C#高性能TCP服务的多种实现方式
- hrbust 哈理工oj 2038 联系网络【MST】【最小生成树】
- Quick-cocos2d-x v3.3 SocketTCP链接
- TCP/IP学习与总结-3(TCP/IP 协议)
- 详解网络传输中的三张表,MAC地址表、ARP缓存表以及路由表
- 一张图搞定TCP/UDP
- iOS网络状态模拟——Network Link Conditioner
- exchange IE登录提示http 500 内部服务器错误
- TCP协议的三次握手
- iOS开发之网络编程--中文转码
- VMware网络连接三种模式bridged、host-only、NAT
- OKHttp使用简介
- HTTP与HTTPS的区别
- iOS开发使用GDataXML框架解析网络数据
- jQuery全屏滚动插件fullPage(转自https://github.com/powy1993/fullpage)
- scapy-掌控网络-窃取email认证
- scapy-网络掌控者-基本
- Android 网络编程--socket tcp/ip udp http之间的关系
- iOS开发使用XML解析网络数据
- iOS开发使用JSON解析网络数据