斐波那契数列定义:From Wikipedia, the free encyclopedia

http://en.wikipedia.org/wiki/Fibonacci_number

In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:[2][3]

or (often, in modern usage):

 (sequence A000045 in OEIS).

By definition, the first two numbers in the Fibonacci sequence are 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

with seed values[2][3]

or[4]

本例以后一种为例:

最简单的一种:两层递归

     public static long fibonacci(int n){
if(n==0) return 0;
else if(n==1) return 1;
else
return fibonacci(n-1)+fibonacci(n-2);
}

问题是:随着n的数值逐渐增多,时间和空间耗费太大,读者可以自行实验。在我的机器上n=50时就不能忍受了。

考虑优化:一层递归

    public static void main(String[] args) {
long tmp=0;
// TODO Auto-generated method stub
int n=10;
Long start=System.currentTimeMillis();
for(int i=0;i<n;i++){
System.out.print(fibonacci(i)+" ");
}
System.out.println("-------------------------");
System.out.println("耗时:"+(System.currentTimeMillis()-start));
} public static long fibonacci(int n) {
long result = 0;
if (n == 0) {
result = 0;
} else if (n == 1) {
result = 1;
tmp=result;
} else {
result = tmp+fibonacci(n - 2);
tmp=result;
}
return result;
}

递归时间减少了到不到50%

最好的方式,不使用递归的方式来做。

    public static long fibonacci(int n){
long before=0,behind=0;
long result=0;
for(int i=0;i<n;i++){
if(i==0){
result=0;
before=0;
behind=0;
}
else if(i==1){
result=1;
before=0;
behind=result;
}else{
result=before+behind;
before=behind;
behind=result; }
}
return result;
}

斐波那契数列(fabnacci)java实现的更多相关文章

  1. 几种复杂度的斐波那契数列的Java实现

    一:斐波那契数列问题的起源 13世纪初期,意大利数论家Leonardo Fibonacci在他的著作Liber Abaci中提出了兔子的繁殖问题: 如果一开始有一对刚出生的兔子,兔子的长大需要一个月, ...

  2. 斐波那契数列【java实现】

    java 实现斐波那契数列 以下是Java代码实现(递归与递推两种方式): import java.util.Scanner; /** * Fibonacci * * @author tongqian ...

  3. 斐波那契数列(Java)

    一.什么是斐波那契数列 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为& ...

  4. 剑指Offer-7.斐波那契数列(C++/Java)

    题目: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 分析: 斐波那契数列是0,1,1,2,3,5,8,13...也就是当前 ...

  5. 从斐波那契数列看java方法的调用过程

    先看斐波那契数列的定义: 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为 ...

  6. (转)从斐波那契数列看Java方法的调用过程

    斐波那契数列的定义: 斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家列安纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔 ...

  7. 算法小节(一)——斐波那契数列(java实现)

    看到公司的笔试题中有一道题让写斐波那契数列,自己忙里偷闲写了一下 什么是斐波那契数列:斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

  8. 【斐波那契数列】java探究

    题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 解析 (1)递归方式 对于公式f(n) = f(n-1) + f(n ...

  9. 剑指offer第二版面试题10:斐波那契数列(JAVA版)

    题目:写一个函数,输入n,求斐波那契数列的第n项.斐波那契数列的定义如下: 1.效率很低效的解法,挑剔的面试官不会喜欢 使用递归实现: public class Fibonacci { public ...

  10. HDU 5686 斐波那契数列、Java求大数

    原题:http://acm.hdu.edu.cn/showproblem.php?pid=5686 当我们要求f[n]时,可以考虑为前n-1个1的情况有加了一个1. 此时有两种情况:当不适用第n个1进 ...

随机推荐

  1. OC中的单例设计模式及单例的宏抽取

    // 在一个对象需要重复使用,并且很频繁时,可以对对象使用单例设计模式 // 单例的设计其实就是多alloc内部的allocWithZone下手,重写该方法 #pragma Person.h文件 #i ...

  2. windows下安装CI框架

    CI框架是一个非常流行的 mvc框架, CI框架如何安装和使用,在CI中文网已经讲的比较详细了 ,这里记录下几个需要注意的地方. 一. index.php问题 把压缩包下载解压到项目根目录即可运行里面 ...

  3. Hibernate 一对多单向关联Demo

    以Classes[班级]和Student[学生]为例的Demo Classes .java public class Classes implements Serializable { private ...

  4. 重拾C++ 基础知识总结(一)

    1.使用gcc编译c++文件报错 proc1.cc:(.text+0x14): undefined reference to `std::cout' C++程序使用gcc命令只能编译,不能链接库文件 ...

  5. 【USACO 2.1.3】三值的排序

    [题目描述] 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌排序的时候.在这个任务中可能的值只有三种1,2和3.我们用交换的方法把他排 ...

  6. js循环便利json数据

    var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name: ...

  7. JavaScript错误处理

    JavaScript 错误 - Throw.Try 和 Catch JavaScript 测试和捕捉 try 语句允许我们定义在执行时进行错误测试的代码块. catch 语句允许我们定义当 try 代 ...

  8. C语言对数组取地址

    #include <stdio.h> main() { ] = {,,,,}; printf("a=%p\n" , a); printf("a=%p\n&qu ...

  9. Python学习笔记:07异常

    异常 Python用异常对象(Exception Object)来表示异常情况,当异常未被捕获时,就会产生回溯(Traceback) 异常分类 內建异常类:Exception,AttributeErr ...

  10. Linux 下安装Python框架django建立与mysql的连接

    0.基本环境说明: a. Ubuntu 14.04 64bit b. python 2.7.6 c. django 1.8 d. django-registration e. django-widge ...