PHP 和 Python实现Project Euler 1、2题
来源:互联网 发布:詹姆斯2016赛季数据 编辑:程序博客网 时间:2024/06/07 22:03
最近开始学python,于是就拿Project Euler来练手
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.
运行结果:233168
PHP版本 :
/** * @desc Project Euler 1 * @Author tina * @Date 2015-08-27 */$sum = 0;for($i=0; $i<1000; $i++){ if(($i%3 == 0) || ($i%5 == 0)){ $sum += $i; }}echo $sum;python版本:
sum = 0for i in range(1000): if((i%3 == 0) or (i%5 == 0)): sum += iprint sum
Problem 2
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
运行结果:4613732
/** * @desc : Project Euler 2 * @Author : tina * @Date : 2015-08-27 */$fab1 = 1;$fab2 = 1;$sum = 0;do{ $fab = $fab1+$fab2; $fab1 = $fab2; $fab2 = $fab; if($fab%2 == 0){ $sum += $fab; }}while($fab < 4000000);echo $sum;
python版本:
fab1 = 1fab2 = 1sum = 0while True : fab = fab1+fab2 fab1 = fab2 fab2 = fab if(fab%2 == 0): sum += fab if(fab > 4000000) : breakprint sum
其实感觉大体上还是差不多的……但看了一些python介绍,感觉功能很强大,什么列表、字典、集合数据类型,居然还可以处理复数!!很期待啊!(PS:好像发明Python的这个大牛就是数学出身的,难怪罗!)
0 0
- PHP 和 Python实现Project Euler 1、2题
- Python实现Project Euler 5
- Project Euler Problems 19-22 Python实现
- 【Project Euler】2 第二题
- 【Project Euler】1 第一题
- Project Euler 1-5题
- Python-Project Euler 22
- Python-Project Euler 24
- Python-Project Euler 26
- Python-Project Euler 27
- Python-Project Euler 29
- Python-Project Euler 30
- Python-Project Euler 31
- Python-Project Euler 32
- Python-Project Euler 36
- Python-Project Euler 37
- Python-Project Euler 38
- Python-Project Euler 39
- ScrollView+Fragment+ListView嵌套ListView,麻麻再也不用担心我不会写嵌套
- iOS开发中,修改了Xcode里面的头文件的解决办法
- linux常用操作命令
- Mysqladmin Mysql 管理工具
- 究竟什么样的对象才会是null?
- PHP 和 Python实现Project Euler 1、2题
- 全文检索数据挖掘
- 创业者需要知道的50句话
- oracle 12c rac gi 安装检测有Node Connectivity Failed报错
- NOIP2014 day2第三题 解方程
- 微博案例
- Hbase 0.94(基于hadoop1)迁移数据到hbase 0.98(基于hadoop2)
- DMA驱动程序设计
- swift笔记(2)