Java Bunny Problem (Fibonacci series) extensions

Source: Internet
Author: User

Java Bunny Problem (Fibonacci series) extensions

The Fibonacci sequence refers to 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 into the April this sequence is certainly not the case, or the rabbit has a death cycle, here I am the rabbit production cycle is not limited, as long as the month is greater than the production cycle can be calculated how many pairs of rabbits the month.

Java Bunny reproduction problem

The Fibonacci sequence is also due to the mathematician Leonardo's · Fibonacci is introduced as an example of rabbit reproduction and is called the " Rabbit sequence ."

In general, after two months of birth, rabbits have the ability to reproduce, a pair of rabbits can produce a pair of rabbits each month. If all the rabbits are not dead, how many pairs of rabbits can be bred in a year?

We may as well take the new born pair of rabbits to analyze:

The first month the Little Bunny had no reproductive capacity, so it was a couple.

Two months later, a couple of rabbits were born, a total of two pairs

Three months later, the old rabbit gave birth to another pair, because the rabbit has no reproductive capacity, so altogether is three pairs.

------

You can list the following tables in turn:

After number of months

0

1

2

3

4

5

6

7

8

9

10

11

12

The logarithm of a young boy

1

0

1

1

2

3

5

8

13

21st

34

55

89

Into the rabbit logarithm

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

logarithm of the cub = the logarithm of the rabbit before the moon

Cheng logarithm = The logarithm of the previous month into a rabbit and the logarithm of the young in the previous month

Total logarithm = This month becomes a rabbit logarithm + this month the logarithm of the cub

It can be seen that the logarithm of the young, the logarithm of the rabbit and the total logarithm all constitute a series. This series is concerned with the very obvious characteristics, that is: the sum of the preceding two adjacent, constitute the latter.

This series is the Italian medieval mathematician Fibonacci in the < Abacus book > proposed, this series of the general formula, in addition to having a (n+2) =an+a (n+1) of the nature, 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, 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>

* 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>

* 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>

* 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>

* 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;

}

}

These algorithms are not applied to object-oriented thinking to solve, if the generation cycle into the April this sequence is certainly not the case, or the rabbit has a death cycle, here I am the rabbit production cycle is not limited, as long as the month is greater than the production cycle can be calculated how many pairs of rabbits in the month. In practice, the rabbit can be set for a rabbit object, its production cycle, death cycle defined as a property, perhaps this can have a better effect.


Java Bunny Problem (Fibonacci series) extensions

Related Article

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.