PHP和Python实现ProjectEuler1、2题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP和Python实现ProjectEuler1、2题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1551字,纯文字阅读大概需要3分钟。
内容图文
最近开始学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 = 0 for i in range(1000): if((i%3 == 0) or (i%5 == 0)): sum += i print 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 = 1 fab2 = 1 sum = 0 while True : fab = fab1+fab2 fab1 = fab2 fab2 = fab if(fab%2 == 0): sum += fab if(fab > 4000000) : break print sum
其实感觉大体上还是差不多的……但看了一些python介绍,感觉功能很强大,什么列表、字典、集合数据类型,居然还可以处理复数!!很期待啊!(PS:好像发明Python的这个大牛就是数学出身的,难怪罗!)
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了PHP 和 Python实现Project Euler 1、2题,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
内容总结
以上是互联网集市为您收集整理的PHP和Python实现ProjectEuler1、2题全部内容,希望文章能够帮你解决PHP和Python实现ProjectEuler1、2题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。