Java classic algorithm-001 Fibonacci Series

Source: Internet
Author: User

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 in a recursive method as follows: f0 = 0, F1 = 1, FN = f (n-1) + f (n-2)
(N> = 2, nε N *) in modern physics, quasi-crystal structure, chemistry, and other fields, the Fibonacci series are directly applied. For this reason, American mathematics
From the 1960 s onwards, the journal of the Fibonacci series will be published to detail research results in this area.

Question: classical question: there is a rabbit. From the first 3rd months after birth, a rabbit is born every month. After the fourth month, a rabbit is born every month. If the Rabbit does not die, what is the total number of rabbits per month?

Program analysis: the rabbit rule is a sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21 ....

Package WZS. arithmetics; // [Procedure 1] Title: classical problem: a rabbit has been born every month since 3rd months after birth, when the rabbit grows to the fourth month, another rabbit is born every month. // If the Rabbit does not die, how many rabbits are there every month? // 1. program analysis: the rabbit rule is a sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21 .... public class test_wzs1 {public static void main (string [] ARGs) {test_wzs1 test_wzs1 = new test_wzs1 (); // display the number of rabbits in the first 20 months for (INT I = 0; I <20; I ++) {system. out. println ("nth" + (I + 1) + "months total Rabbit" + test_wzs1.f (I + 1) + "pair. ") ;}}/**** returns the number of rabbits per month * @ Param X number of months * @ return returns the number of rabbits in the current month */Public int F (int x) {If (x = 1 | x = 2) {return 1 ;}else {return f (x-1) + f (x-2 );}}}

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.