Java Bunny Problem (Fibonacci series) extension

Source: Internet
Author: User

Java Bunny Problem (Fibonacci series) extension

The Fibonacci sequence refers to such a sequence of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For this series can only say that the rabbit production cycle of the first March, if the generation cycle turns into April this sequence is certainly not the case, or the rabbit has a death cycle, where I am not limited to the rabbit production cycle, as long as the month is larger than the production cycle can calculate the month month in the end can produce how many pairs of rabbits.

Java Bunny Breeding problem

The Fibonacci sequence was introduced as an example of the Leonardo's of the mathematician, called the Rabbit series.

In general, the rabbit after two months of birth, there is the ability to reproduce, a pair of rabbits each month to produce a pair of small rabbits. If all rabbits do not die, then a year later can reproduce how many pairs of rabbits.

We might as well take a new-born pair of bunnies to analyze:

The first month the rabbit didn't have the ability to reproduce, so it was a pair.

Two months later, the birth of a pair of small rabbit logarithm of a total of two pairs

Three months later, the old rabbit gave birth to another pair, because the rabbit has not the ability to reproduce, so altogether is three pairs.

------

In turn, you can list the following table:

After the number of months

0

1

2

3

4

5

6

7

8

9

10

11

12

Young logarithm

1

0

1

1

2

3

5

8

13

21st

34

55

89

The logarithm of rabbit into

0

1

1

2

3

5

8

13

21st

34

55

89

144

Total logarithm

1

1

2

3

5

8

13

21st

34

55

89

144

233

The young logarithm = The logarithm of the former moon into the rabbit

Cheng logarithm = logarithm of the previous moon into rabbits and the logarithm of the previous month's young

Total logarithm = logarithm of the rabbit in this month + the logarithm of the young this month

It can be seen that the logarithm of the young, the logarithm of the rabbit, the total logarithm constitute a series. This sequence is concerned with the very obvious feature: the sum of the two adjacent items, which constitute the latter.

This series is the Italian medieval mathematician Fibonacci in the < abacus > in the book, this series of the formula, in addition to the nature of a (n+2) =an+a (n+1), can also prove that the formula is: an= (1/√5) *{[(1+√5)/2]^n-[(1- √5)/2]^n} (n=1,2,3 ...).

Javacode:

/*

* System abbrev:

* System Name:

* Component No:

* Component Name:

* File Name:FabonacciSequence.java

* Author:Peter.Qiu

* Date:aug 25, 2014

* Description: <description>

*/

/* Updation record 1:

* Updation Date:aug 25, 2014

* Updator:Peter.Qiu

* Trace No: <trace no>

* updation No: <updation no>

* Updation Content: <list All contents of updation and all methods updated.>

*/

Package com.qiuzhping.util.interesting;

/**

* <description functions in a word>

* <detail description>

*

* @author Peter.qiu

* @version [version NO, Aug 25, 2014]

* @see [Related classes/methods]

* @since [Product/module version]

*/

public class Fabonaccisequence {

private static fabonaccisequence util = NULL;

/** <default constructor>

*/

Public Fabonaccisequence () {

TODO auto-generated Constructor stub

}

/** <description functions in a word>

* Aug 25, 2014

* <detail description>

* @author Peter.qiu

* @param args [Parameters description]

* @return void [return type description]

* @exception throws [Exception] [exception description]

* @see [Related classes#related methods#related Properties]

*/

public static void Main (string[] args) {

Long month = 8;

Long Product = 4;

Long start = System.currenttimemillis ();

System.out.println ("Fabonacci \trabbit:" +getinstance (). Fabonacci (month));

System.out.println ("Take times =" + (System.currenttimemillis ()-start)/1000);

System.out.println ("--------------------------------------");

System.out.println ("Fabonacci1 \trabbit:" +getinstance (). FABONACCI1 (month));

System.out.println ("Take times =" + (System.currenttimemillis ()-start)/1000);

System.out.println ("--------------------------------------");

System.out.println ("Fabonacci2 \trabbit:" +getinstance (). FABONACCI2 (month,product));

System.out.println ("Take times =" + (System.currenttimemillis ()-start)/1000);

for (long i = product; I <= month; i++) {

SYSTEM.OUT.PRINTLN ("month =" +i+ "\tfabonacci2 \trabbit:" +getinstance (). FABONACCI2 (i,product));

}

}

public static Fabonaccisequence getinstance () {

if (util = = null) {

Util = new Fabonaccisequence ();

}

return util;

}

/** <description functions in a word>

*pruduct month = 3<br>

* Aug 25, 2014

* <detail description>

* @author Peter.qiu

* @param month:how many months.

* @return [Parameters description]

* @return Long [return type description]

* @exception throws [Exception] [exception description]

* @see [Related classes#related methods#related Properties]

*/

Public long Fabonacci (long month) {

if (!) ( Month < 3)) {

for (Long i = 3; I <= month;) {

Return Fabonacci (month-1) + Fabonacci (month-2);

}

}

return 1;

}

/** <description functions in a word>

* Pruduct month = 3<br>

* Aug 25, 2014

* <detail description>

* @author Peter.qiu

* @param month:how many months.

* @return [Parameters description]

* @return Long [return type description]

* @exception throws [Exception] [exception description]

* @see [Related classes#related methods#related Properties]

*/

Public long fabonacci1 (long month) {

Long sum = 1, Lastmonth = 1, lastlastmonth = 1;

if (!) ( Month < 3)) {

for (Long i = 3; I <= month; i++) {

Lastlastmonth = Lastmonth;

Lastmonth = sum;

sum = Lastlastmonth + lastmonth;

}

}

return sum;

}

/** <description functions in a word>

* Aug 25, 2014

* <detail description>

* @author Peter.qiu

* @param month:how many months.

* @param pruductmonth:the production cycle.

* @return [Parameters description]

* @return Long [return type description]

* @exception throws [Exception] [exception description]

* @see [Related classes#related methods#related Properties]

*/

Public long Fabonacci2 (long month, long Pruductmonth) {

Long sum = 1;

if (!) ( Month < Pruductmonth)) {

for (long i = 0,j = PruductMonth-1 I < month-(pruductMonth-1); i++) {

Sum + + fabonacci2 (month-j-I, pruductmonth);

}

}

return sum;

}

}

None of these algorithms have been applied to the object-oriented way of thinking to solve, if the generation cycle turns into April this sequence is certainly not the case, or the rabbit has a death cycle, where I am not limited to the rabbit production cycle, as long as the month is larger than the production cycle can calculate the month month in the end can produce how many pairs of rabbits. In practice, the rabbit can be designated as a Rabbit object, its production cycle, the death cycle is defined as a property, may have a better effect.

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.