Java 兔子問題(斐波那契數列)擴展篇
Java 兔子問題(斐波那契數列)擴展篇
斐波那契數列指的是這樣一個數列 0, 1, 1, 2,3, 5, 8, 13, 21, 34, 55, 89, 144, ...對於這個數列僅僅能說將兔子生產周期第為3月。假設生成周期變成4月這個數列肯定不是這種,或者說兔子還有死亡周期,在這裏我是對兔子生產周期沒有限定。僅僅要月份大於生產周期都能夠計算出第month月份究竟能產生多少對兔子。
Java兔子生殖問題
斐波那契數列又因數學家列昂納多·斐波那契以兔子生殖為樣例而引入。故又稱為“兔子數列”。
一般而言,兔子在出生兩個月後。就有生殖能力。一對兔子每一個月能生出一對小兔子來。假設全部兔子都不死,那麽一年以後能夠生殖多少對兔子?
我們最好還是拿新出生的一對小兔子分析一下:
第一個月小兔子沒有生殖能力,所以還是一對
兩個月後,生下一對小兔對數共同擁有兩對
三個月以後。老兔子又生下一對。由於小兔子還沒有生殖能力。所以一共是三對。
------
依次類推能夠列出下表:
經過月數 |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
幼仔對數 |
1 |
0 |
1 |
1 |
2 |
3 |
5 |
8 |
13 |
21 |
34 |
55 |
89 |
成兔對數 |
0 |
1 |
1 |
2 |
3 |
5 |
8 |
13 |
21 |
34 |
55 |
89 |
144 |
整體對數 |
1 |
1 |
2 |
3 |
5 |
8 |
13 |
21 |
34 |
55 |
89 |
144 |
233 |
幼仔對數=前月成兔對數
成兔對數=前月成兔對數+前月幼仔對數
整體對數=本月成兔對數+本月幼仔對數
能夠看出幼仔對數、成兔對數、整體對數都構成了一個數列。這個數列有關十分明顯的特點,那是:前面相鄰兩項之和,構成了後一項。
這個數列是意大利中世紀數學家斐波那契在<算盤全書>中提出的。這個級數的通項公式,除了具有a(n+2)=an+a(n+1)的性質外,還能夠證明通項公式為: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; } } |
以上這些算法都沒有應用到面向對象的思維方式去解決,假設生成周期變成4月這個數列肯定不是這種,或者說兔子還有死亡周期,在這裏我是對兔子生產周期沒有限定,僅僅要月份大於生產周期都能夠計算出第month月份究竟能產生多少對兔子。實際中能夠將兔子定於為一個Rabbit對象,將其生產周期、死亡周期定義為屬性。可能這個能有更好的效果。
Java 兔子問題(斐波那契數列)擴展篇