国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目次
PHP とロジックのフィボナッチ數(shù)列
PHP のロジック
2 つのアプローチによるフィボナッチ印刷のための PHP シリーズ
1.非再帰的な方法
2. The Recursion Way

フィボナッチ數(shù)列 PHP

Aug 29, 2024 pm 01:12 PM
php

素人言葉で言うと、フィボナッチ數(shù)列とは、必要なシリーズ サイズが得られるまで、前の 2 つの要素を追加して次の要素を形成するときに形成または取得される一連の要素です。通常、フィボナッチ數(shù)列は 0 と 1 から始まります。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語(yǔ)、ソフトウェア テスト、その他

一度形成されたシリーズは次のように表示されます:

0、1、1、2、3、5、8、13、21、34

上で述べたように、次の數(shù)値は前の 2 つの數(shù)値を加算することで得られます。

  • 上記の系列の 4 番目 位置 (n 番目 位置) の「2」は、その前の 2 つの數(shù)値を加算することによって取得されます[ [n-1]そして n-2])、1.
  • 「3」は、その前の 2 つの數(shù)字 2 を加算することで得られます。
  • 「5」は、その前の 2 つの數(shù)字 3 を加算することで得られます。
  • など

PHP とロジックのフィボナッチ數(shù)列

ここでは、PHP 環(huán)境で作業(yè)しながらフィボナッチ數(shù)列を取得する方法を具體的に見ていきます。違いは、コーディングする形式、つまり、PHP スクリプトの開始タグとその終了タグの使用です。

<?php
…;
…;
…;
?>

これは、反復(fù)方法と再帰方法という 2 つの方法を使用して、PHP でこのフィボナッチ數(shù)列がどのように生成されるかを理解し、學(xué)習(xí)するのに役立ちます。

數(shù)値、つまり系列サイズである「n」が與えられると、指定された數(shù)値までのフィボナッチ數(shù)列を見つけようとします。

たとえば、n=5 のフィボナッチを作成する必要がある場(chǎng)合、第 5 項(xiàng)までの要素を表示します。

例 1

  • 入力: n = 9
  • 出力: 0 1 1 2 3 5 8 13 21

例 2

  • 入力: n=13
  • 出力: 0 1 1 2 3 5 8 13 21 34 55 89 144

PHP のロジック

ロジックは上記と同じです。ここでは n=10 としました。つまり、n 番目の項(xiàng)までの要素を見つける必要があります。したがって、シリーズ內(nèi)の用語(yǔ)が n 個(gè)になるまでロジックに従い続けます。

上記の例の 1 つを見てみましょう。

上記の例の 1 つでは、n=9 があり、ロジックは次のように言います:

  • 最初の數(shù)値を 0 として初期化します。
  • 2 番目の數(shù)値を 1 として初期化します。
  • 最初と 2 番目の數(shù)字を出力します。
  • ループはここから始まります。
  • シリーズの次の要素、つまり 3 番目 要素 [n 番目 要素] については、その直前の 2 つの數(shù)字 [(n-1) と (n- 2)] ここのように、シリーズの次の數(shù)値を取得します (0 + 1 = 1)。

n=3 の場(chǎng)合

  • n – 1 = 3 – 1 = 系列の 2 番目の要素 = 1
  • n – 2 = 3 – 2 = 系列の最初の要素 = 0 3rd 要素 = (n-1) + (n-2) = 1 + 0 = 1

したがって、シリーズの 3 番目の要素は 1 です。

  • 同様にロジックによれば、系列の 4 番目 要素 [n] を取得するには、その前の數(shù)値 e を加算する必要があります。 (n-1) および (n-2) 要素。

この時(shí)點(diǎn)で、「n」は「4」と等しくなります。

  • n – 1 = 4 – 1 = 系列の 3 番目の要素 = 1
  • n – 2 = 4 – 2 = 系列の 2 番目の要素 = 1 4番目 要素 = (n-1) + (n-2) = 1 + 1 = 2

したがって、4番目要素は 2 として取得されます。

したがって、「n」が 9 に等しい場(chǎng)合、上で説明したのと同じロジックに従って、フィボナッチ數(shù)列は 0 1 1 2 3 5 8 13 21 となります

2 つのアプローチによるフィボナッチ印刷のための PHP シリーズ

PHP でフィボナッチ數(shù)列を出力するプログラムを作成する方法については、基本的に 2 つの有名なバージョンがあります。

  • 再帰なし
  • 再帰あり

PHP の通常どおり、「echo」ステートメントを使用して出力を表示します。

1.非再帰的な方法

反復(fù)の使用としても知られています。これは、シリーズを 0 と 1 で開始するアプローチです。その後、最初と 2 番目の數(shù)値を出力します。次に、ループを使用した反復(fù)を開始します。ここでは while ループを使用しています。

最初の 10 個(gè)のフィボナッチ數(shù)列要素を出力するための PHP スクリプト。

コード:

<?php
function Fibonacci($n)
{
$num1= 0;
$num2= 1;
$counter= 0; while($counter < $n)
{
echo ' '.$num1;
$num3= $num2 + $num1;
$num1= $num2;
$num2= $num3;
$counter= $counter+1;
}
}
//for a pre defined number for Fibonacci.
$n=10; Fibonacci($n);
?>

コードの説明:

  1. Here n is defined as equal to 10, so the logic will run till nth element e. Until we have n=10 elements in the series.
  2. First element is initialized to 0 and second element is initialized to 1, e. num1 = 0 and num2 = 1.
  3. The two elements i.e. num1 and num2 are printed as the first and second elements of the Fibonacci series.
  4. The logic we discussed will be used from here on and our iteration loop starts.
  5. According to our logic, to get num3, we need to add num1 and num2. Since currently num1 = 0 and num2 = 1, num3 comes out as 1.
  6. Now new number obtained is placed in num2 variable and num2 is saved in num1 variable. Basically simple swapping is taking place so that, now num1 is equal to ‘1’ and num2 = newly obtained num3 i.e. ‘1’.
  7. So when the next iteration happens and num3 is obtained by adding current values of num1 and num2, which, according to our PHP script, are as follows:
      • num1 = 1
      • num2 = 1
      • num3 = num1 + num2 = 1 + 1 = 2

Thus we get our next number in the Fibonacci Series.

  1. Similarly, the iteration keeps on till we achieve n = 10, size of the series that was defined in the program itself.

When the above program is executed, we get the output as follows:

フィボナッチ數(shù)列 PHP

2. The Recursion Way

By recursion, we mean the way where the same function is called repeatedly until a base condition is achieved or matched. At this point, recursion is stopped.

The said “function is called repeatedly” phrase points to the section in your code where we will define our logic for the Fibonacci Series.

Below is an example of generating Fibonacci Series in PHP, using If-Else conditions giving way for our recursive approach.

Here is the PHP Scripts for printing the first 15 elements for Fibonacci Series.

<?php
function Fibonacci($num)
{
//If-Else IF will generate first two numbers for the series if($num == 0)
return 0;
else if($num == 1) return 1;
// This is where Recursive way comes in.
//recursive call to get the rest of the numbers in the series else
return(Fibonacci($num -1) + Fibonacci( $num -2));
}
//For a given n=15
$num =15;
for($counter = 0; $counter < $num; $counter++)
{
echo Fibonacci($counter).' ';
}
?>

Code Explanation:

This is the recursive way, which means our function that contains our logic is called again and again for generating the next element in the series until our condition for achieving a specific series size is obtained.

In Iterative approaches, the First and Second element is first initialized and printed. Here we allow a For Loop to give us our first and second elements starting with 0 and 1.

  1. We are using a For loop having a ‘counter’ variable that starts with 0. The For loop works up till the given ‘n’, size for the series.
  2. when loop starts, with the counter variable as 0, we use the recursion approach and call our defined function, Fibonacci ( ).
  3. Here code starts, with If and Else IF condition.
  4. First IF condition checks if ‘num’ variable holds value as ‘0’, if yes, then the code prints or returns ‘0’ as the element for the series.
  5. Similarly, second Else-If condition checks for the value ‘1’, if the ‘num’ variable holds ‘1’ as its value, the program returns it as it is.
  6. Next Else condition recursively calls the Fibonacci function for’ num’ values other than ‘0’ and ‘1’, continuing to reading the values from the For loop counter.

This is where our Fibonacci Logic comes into work and the next number in the sequence is obtained by adding its previous two numbers. Because this is the recursive method, we need to give a counter value to count the recursions equal to nth value, which is being handled by our For Loop.

When the above program or code is executed, the following output is displayed.

フィボナッチ數(shù)列 PHP

The Fibonacci Series does not only appear in mathematics or science calculations but in nature too, have you ever noticed Yellow chamomile flower head.

The Fibonacci Series if plotted on a graph, it forms a spiral called Fibonacci Spiral. It is also one of the gems given by Indian soil. It is found in Indian Mathematics as early as 200 BC in the works done by the mathematician, Pingala. Later Fibonacci introduced the sequence to European countries in his book Liber Abacci in 1200s.

以上がフィボナッチ數(shù)列 PHPの詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國(guó)語(yǔ) Web サイトの他の関連記事を參照してください。

このウェブサイトの聲明
この記事の內(nèi)容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰屬します。このサイトは、それに相當(dāng)する法的責(zé)任を負(fù)いません。盜作または侵害の疑いのあるコンテンツを見つけた場(chǎng)合は、admin@php.cn までご連絡(luò)ください。

ホットAIツール

Undress AI Tool

Undress AI Tool

脫衣畫像を無料で

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード寫真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

寫真から衣服を削除するオンライン AI ツール。

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡(jiǎn)単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中國(guó)語(yǔ)版

SublimeText3 中國(guó)語(yǔ)版

中國(guó)語(yǔ)版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強(qiáng)力な PHP 統(tǒng)合開発環(huán)境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

最新のPHP開発とベストプラクティスを最新の狀態(tài)に保つにはどうすればよいですか? 最新のPHP開発とベストプラクティスを最新の狀態(tài)に保つにはどうすればよいですか? Jun 23, 2025 am 12:56 AM

postaycurrentwithpdevellyments andbest practices、follow keynewsourceslikephp.netandphpweekly、egagewithcommunitiessonforums andconferences、keeptooling and gradivallyadoptnewfeatures、andreadorcontributeTopensourceprijeprijeprijeptrijeprijeprests.

PHPとは何ですか、そしてなぜそれがWeb開発に使用されるのですか? PHPとは何ですか、そしてなぜそれがWeb開発に使用されるのですか? Jun 23, 2025 am 12:55 AM

PhpBecamepopularforwebdevelopmentduetoitseaseaseaseaseasease、SeamlessintegrationWithhtml、widespreadhostingsupport、andalargeecosystemincludingframeworkelavelandcmsplatformslikewordspresspressinsinsionsisionsisionsisionsisionsionsionsisionsionsionsisionsisions

PHPタイムゾーンを設(shè)定する方法は? PHPタイムゾーンを設(shè)定する方法は? Jun 25, 2025 am 01:00 AM

tosettherighttimezoneInphp、usedate_default_timezone_set()functionthestthestofyourscriptwithavalididentifiersiersuchas'america/new_york'.1.usedate_default_timezone_set()beforeanydate/timefunctions.2.2.Altertentally、confuturethephp.inifilebyset.

PHPでのユーザー入力を検証して、特定の基準(zhǔn)を満たすことを確認(rèn)するにはどうすればよいですか? PHPでのユーザー入力を検証して、特定の基準(zhǔn)を満たすことを確認(rèn)するにはどうすればよいですか? Jun 22, 2025 am 01:00 AM

tovalidateuserinputinphp、usebuilt-validationfunctionslikefilter_var()andfilter_input()、applyRegularexpressionsforcustomformatsusususussusorphoneNumbers、checkdatatypesfornumerueSlikeageorpricepriceprice

PHP(serialize()、unserialize())のデータシリアル化とは何ですか? PHP(serialize()、unserialize())のデータシリアル化とは何ですか? Jun 22, 2025 am 01:03 AM

thephpfunctionSerialize()andunserialize()areusedtoconvertcomplexdatastructostorestorestorustorasandabackagain.1.serialize()c onvertsdatalikecarraysorobjectsraystringcontainingtainingtainingepeandStructureinformation.2。

HTMLファイルにPHPコードを埋め込むにはどうすればよいですか? HTMLファイルにPHPコードを埋め込むにはどうすればよいですか? Jun 22, 2025 am 01:00 AM

PHPコードをHTMLファイルに埋め込むことができますが、ファイルに.phpの拡張機(jī)能があることを確認(rèn)して、サーバーが正しく解析できるようにします。標(biāo)準(zhǔn)タグを使用してPHPコードをラップし、HTMLのどこにでも動(dòng)的コンテンツを挿入します。さらに、同じファイルでPHPとHTMLを複數(shù)回切り替えて、條件付きレンダリングなどの動(dòng)的関數(shù)を?qū)g現(xiàn)できます。短いラベル、引用マークエラー、または省略されたエンドラベルによって引き起こされる問題を回避するために、サーバーの構(gòu)成と構(gòu)文の正確性に注意してください。

クリーンで保守可能なPHPコードを書くためのベストプラクティスは何ですか? クリーンで保守可能なPHPコードを書くためのベストプラクティスは何ですか? Jun 24, 2025 am 12:53 AM

清潔で維持しやすいPHPコードを書くための鍵は、標(biāo)準(zhǔn)、合理的な構(gòu)造に従って、コメント、テスト能力を適切に利用する明確な命名にあります。 1。$ userDataやcalculatetotalprice()などの明確な変數(shù)、関數(shù)、クラス名を使用します。 2。PSR-12標(biāo)準(zhǔn)統(tǒng)一コードスタイルに従ってください。 3.責(zé)任に従ってコード構(gòu)造を分割し、MVCまたはLaravelスタイルのカタログを使用して整理します。 4.麺スタイルのコードを避け、単一の責(zé)任でロジックを小さな関數(shù)に分割します。 5.キーポイントにコメントを追加し、インターフェイスドキュメントを書き込み、パラメーター、返品値、例外を明確にします。 6.テスト可能性を改善し、依存関係を採(cǎi)用し、グローバルな狀態(tài)と靜的な方法を減らします。これらのプラクティスは、コードの品質(zhì)、コラボレーション効率、メンテナンス後の容易さを改善します。

See all articles