/*
* 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;
}
}
|
相关推荐
斐波那契数列 *以兔子繁殖为例子而引入,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、…… * 在数学逻辑上以递归方法定义
编写一个Java程序,用于输出Fibonacci数列的前20项。
java代码实现斐波那契数列 类似1 1 2 3 5 8 输出第n个数 java开发工程师 笔试一般经常考到
斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”。java代码实现该数列
JAVA代码]斐波那契数列GUI
斐波那契数列java的简单实现,很简单明了
Fibonacci数列斐波那契数列PPT学习教案.pptx
包括递归版、迭代版、矩阵快速幂版的各种斐波那契数列的java解决方法。 包括递归版、迭代版、矩阵快速幂版的各种斐波那契数列的java解决方法。 包括递归版、迭代版、矩阵快速幂版的各种斐波那契数列的java解决方法。...
C++斐波那契数列C++斐波那契数列C++斐波那契数列C++斐波那契数列C++斐波那契数列C++斐波那契数列C++斐波那契数列C++斐波那契数列C++斐波那契数列C++斐波那契数列C++斐波那契数列C++斐波那契数列C++斐波那契数列C++...
用Java编写的斐波那契数列并且用文本保存结果
封装的Fibonacci 斐波那契数列。输入想要的斐波那契数列的长度,输出结果数列
Fibonacci(斐波那契)数列的JAVA解法,包含了斐波那契数列常见问题的一些算法。
Java实现斐波那契数列的前n项和.。。。。。。。。
斐波那契数列: 在数学上它以递归的方式进行定义,指这样的一个数列:0、1、1、2、3、5、8、13、21、34、55、89、144……,即前两个数为分别为0和1,从第3项开始,每项的值都等于其前两项之和。斐波那契数列Fib(n)用...
一 生小兔问题引起的二 它们也产生斐波那契数列三 通项的其他表达式四 斐波那契数列是二阶循环数列五 斐波那契数列的数论性质六 斐波那契数列的其他性质七 某些斐波那契数列之和八 斐波那契数列与连分数九 斐波那契...
java中斐波那契数列的简单实现方法.docx
如何利用java求解Fibonacci数列
本代码使用C++语言书写,编译环境VS2013。...斐波那契数列(Fibonacci Sequence)又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、…… 本代码是练习作品,如有错误或修改,请指正,感谢感谢。
使用C++非递归实现fibonacci数列,对正在学习算法的同学应该挺有帮助的
用java语言写的求求fibonacci数列第1000项的值,用的递归算法.是初学java练习递归的好素材.