Example-PHP source code

Source: Internet
Author: User
Example of upstreaming php code

 Those who have gone to college in this example should all know the famous "Fibonacci" series, which is about the problem of breeding rabbits. I will give a rough explanation of the question. If a pair of rabbits can have one pair of rabbits each month, and each pair of rabbits can have one pair of rabbits in the first 3rd months after their birth, if from a pair of newborn rabbits, how many rabbits can breed in one year? Train of Thought: in fact, we can divide rabbits into "rabbits in January", "rabbits in February", and "rabbits in March". ① Initial time: a pair of big rabbits in January, with a total of 1 pair. ② First month: in January, the Big Bunny became the big rabbit in February, and the total number was still 1. ③ Second month: in February, the Big Bunny became the big rabbit in March, breeding a pair of rabbits, with a total of 2 pairs. ④ The third month: in March, the big rabbit tmd gave birth to a rabbit. in the month of 1st day of last month, the big rabbit became the big rabbit in February, with a total of three pairs. ............ F0 = 1 F1 = 1 F2 = F0 + F1 F3 = F1 + F2 ...... fn = Fn-2 + Fn-1 look at everyone, is not reflected in the "recurrence" of the core idea, the code is also very simple. */$ Month = 12; $ fab = array (); $ fab [0] = 1; $ fab [1] = 1; for ($ I = 2; $ I <$ month; $ I ++) {$ fab [$ I] = $ fab [$ I-1] + $ fab [$ I-2];} for ($ I = 0; $ I <$ month; $ I ++) {echo sprintf ("Rabbit in {% d} months: {% d }", $ I, $ fab [$ I])."
";}?>

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.