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 only can say that the rabbit production cycle of the first March. Assuming the generation cycle becomes April this sequence is definitely not this, or the rabbit has a death cycle, where I am not limited to the rabbit production cycle. A month is more than a production cycle to calculate how many pairs of rabbits can be produced in month months.

Java Bunny Reproductive problems

The Fibonacci sequence is also due to the mathematician Leonardo's · Fibonacci was introduced as a sample of rabbit reproduction. It is also called " Rabbit series ".

Generally speaking, rabbits are born two months after birth. There is reproductive capacity. A pair of rabbits can give birth to a pair of rabbits every one months. Suppose all the rabbits are not dead, so how many rabbits can reproduce in a year?

We'd better take a couple of newborn rabbits and analyze them:

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

Two months later, a pair of small rabbits were born with two pairs in common

Three months later. The old rabbit gave birth to another pair. Because the rabbit has no reproductive capacity. So altogether are three pairs.

------

The following table can be listed in sequence:

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 whole logarithm 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 was proposed by the Italian medieval mathematician Fibonacci in the < Abacus book >. The general formula of this series , in addition to having the properties of a (n+2) =an+a (n+1), can also prove that the general 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, assuming that the generation cycle into the April this sequence is certainly not this, or the rabbit has a death cycle, here I am the rabbit production cycle is not limited, only the month is greater than the production cycle can calculate how many pairs of rabbits the month. In practice it is possible to set the rabbit as a rabbit object, defining its production cycle and death cycle as attributes. Maybe this will have a better effect.


Java Bunny Problem (Fibonacci series) extensions

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.