Recently began to learn python, so I took project Euler to practiced hand
Problem 1
If we list all the natural numbers below is 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.
Run Result: 233168
PHP Version:
/** * @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 version:
sum = 0for i in range: if ((i%3 = = 0) or (i%5 = = 0)): sum + = IPrint sum
Problem 2
Each of the new term in the Fibonacci sequence was generated by adding the previous. By starting with 1 and 2, the first terms would be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values does not exceed four million, find the sum of the Even-value D terms.
Run Result: 4613732
PHP Version:
/** * @desc: Project Euler 2 * @Author: Tina * @Date: 2015-08-27 */$fab 1 = 1; $fab 2 = 1; $sum = 0;do{ $fab = $fab 1+ $fab 2; $fab 1 = $fab 2; $fab 2 = $fab; if ($fab%2 = = 0) { $sum + = $fab; }} while ($fab < 4000000); Echo $sum;
Python version:
FAB1 = 1FAB2 = 1sum = 0while True: fab = fab1+fab2 fab1 = fab2 fab2 = Fab if (fab%2 = = 0): sum + = fab< C5/>if (Fab > 4000000): Breakprint sum
In fact, the feeling is basically the same ... But read some Python introduction, feel very powerful, what list, dictionary, collection data type, incredibly can also handle complex numbers!! Very much looking forward to Ah! (PS: It seems that the invention of Python is a mathematical origin of the Daniel, no wonder Luo!) )
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the PHP and Python implementation project Euler 1, 2, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.