search
HomeBackend DevelopmentPHP TutorialPHP and Python implement Project Euler questions 1 and 2

I started learning python recently, so I used Project Euler to practice

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.

Run result: 233168

PHP version:

/**
 * @desc Project Euler 1
 * @Author tina
 * @Date 2015-08-27
 */
$sum = 0;
for($i=0; $ipython version: <p></p><pre name="code">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.

Run 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 <br>Python version: <p></p><pre name="code">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

In fact, it feels generally the same... But after reading some python introductions, I feel that the functions are very powerful. Lists, dictionaries, and set data types can actually handle complex numbers! ! Looking forward to it! (PS: It seems that the great man who invented Python was born in mathematics, no wonder Luo!)

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above introduces the implementation of Project Euler questions 1 and 2 in PHP and Python, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
2 个月不见,人形机器人 Walker S 会叠衣服了2 个月不见,人形机器人 Walker S 会叠衣服了Apr 03, 2024 am 08:01 AM

机器之能报道编辑:吴昕国内版的人形机器人+大模型组队,首次完成叠衣服这类复杂柔性材料的操作任务。随着融合了OpenAI多模态大模型的Figure01揭开神秘面纱,国内同行的相关进展一直备受关注。就在昨天,国内"人形机器人第一股"优必选发布了人形机器人WalkerS深入融合百度文心大模型后的首个Demo,展示了一些有趣的新功能。现在,得到百度文心大模型能力加持的WalkerS是这个样子的。和Figure01一样,WalkerS没有走动,而是站在桌子后面完成一系列任务。它可以听从人类的命令,折叠衣物

如何在 Excel 中对列求和如何在 Excel 中对列求和May 16, 2023 pm 03:26 PM

如何在Excel中快速查看一列的总和如果您只想知道一列的总和而不需要将该信息添加到电子表格中,您可以使用Excel状态栏快速查看一列或任何单元格区域的总和。要使用Excel状态栏查看列的总和:突出显示要汇总的数据。要选择整个列,请单击列名。否则,拖动单元格以选择您的范围。在屏幕底部,您将看到有关您的选择的信息,例如所有值的平均值以及该范围内的数据点数。您还将看到所选单元格中所有值的总和。如何使用AutoSum在Excel中对列求和如果您希望将列的总和添加到电子表格中,在许多情况下

详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.