PHP implements the Fibonacci series

Source: Internet
Author: User
Introduction

The Fibonacci sequence, also known as the Golden series, refers to such a series: 1, 1, 2, 3, 5, 8, 13, 21 ,...... In mathematics, the Fibonacci series are defined as follows by recursive Methods: f0 = 0, F1 = 1, FN = f (n-1) + f (n-2) (N> = 2, n, N *). I used recursive and iterative methods to implement the Fibonacci series.


Implementation Code (PHP)
<? Phpclass Fibonacci {/*** Description: gets the nth value of the Fibonacci using the iteration method ** @ Param int $ N * @ return int */public static function fib_interation ($ N) {$ fib = array (); // defines the Fibonacci array if ($ n <0) {return 0;} For ($ fib [0] = 0, $ fib [1] = 1, $ I = 2; $ I <= $ N; $ I ++) {$ fib [$ I] = $ fib [$ I-1] + $ fib [$ I-2];} return $ fib [$ N];} /*** Description: Obtain the nth value of Fibonacci using a recursive method. ** @ Param int $ N * @ return int */Public sta TIC function maid ($ n) {if ($ n <= 0) {return 0;} elseif ($ n = 1) {return 1;} else {return self :: fig + self: FIG ($ n-1. "\ n"; $ maid = MAID: Maid (10); echo $ maid. "\ n";?>

Running result

Disadvantages: today, I can see that this blog is still very popular, so I reconstructed the code. The level of the Code half a year ago is indeed poor, and the code looks disgusting. I Want To refactor it today! Secondly, the beauty of programming introduces a sub-governance strategy to find the Fibonacci method, but I am not familiar with it. If you are interested, you can post the implementation code to discuss it. C or PHP is recommended!

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.