Implement ProjectEuler1 and 2 in PHP and Python

Source: Internet
Author: User
: This article mainly introduces the implementation of ProjectEuler1 and 2 in PHP and Python. if you are interested in the PHP Tutorial, refer to it. I recently started to learn python, so I started to train my hands with 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.

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(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.

Result: 4613732

PHP version:

/** * @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 version:

fab1 = 1fab2 = 1sum = 0while True :    fab = fab1+fab2    fab1 = fab2    fab2 = fab    if(fab%2 == 0):        sum += fab    if(fab > 4000000) : breakprint sum

In fact, it's basically the same ...... But after reading some python introduction, I feel that the function is very powerful. what lists, dictionaries, and collection data types can actually process the plural !! Looking forward! (PS: It seems that the big guy who invented Python is born in mathematics. no wonder !)


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the implementation of Project Euler 1 and question 2 in PHP and Python, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.