您好,欢迎访问一九零五行业门户网

PHP 和 Python实现Project Euler 1、2题

最近开始学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; $ipython版本:
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
php版本:
/** * @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
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的这个大牛就是数学出身的,难怪罗!)
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了php 和 python实现project euler 1、2题,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息