PHP and Python Implement Project Euler 1, 2 questions

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.