斐波那契数列定义: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. 【转】 iOS开发数据库篇—SQLite简单介绍

    开始学SQLite啦, 原文: http://www.cnblogs.com/wendingding/p/3868893.html iOS开发数据库篇—SQLite简单介绍 一.离线缓存 在项目开发中 ...

  2. vs2012快捷键

    (1)自己整理的使用频率最高的快捷键(建议大家亲身体验一下!这样才会在潜移默化中运用得到!) (这里省去了很多大家闭上眼都会操作的什么Ctrl+S 等等操作 给出的大多是不常用但是很有用的快捷键组合! ...

  3. python密码处理(可用于生产模式)

    import os from hashlib import sha256 from hmac import HMAC def encrypt_password(password, salt=None) ...

  4. 文件上传-html

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>文件 ...

  5. extjs中combobox默认显示第一个值

    在进入页面时往往用户希望页面能够显示默认的内容,但是页面中会存在一些选项通过用户选择之后才会加载相应的内容.在这篇文章里面介绍了如何去设置页面中默认的内容,如combobox默认显示第一个值. 页面: ...

  6. wamp虚拟机配置

    1.找到httpd.conf 里面:找到 # Virtual hosts 开启虚拟机Include conf/extra/httpd-vhosts.conf 2  编辑httpd-vhosts.con ...

  7. socket本地模拟UDP 服务器+客户端(三)

    UDP: TCP是建立可靠连接,并且通信双方都可以以流的形式发送数据.相对TCP,UDP则是面向无连接的协议. 使用UDP协议时,不需要建立连接,只需要知道对方的IP地址和端口号,就可以直接发数据包. ...

  8. mongodb3.0 db.addUser报错

    > db.addUser("ydkt","ydkt") --19T09:: E QUERY TypeError: Property 'addUser' o ...

  9. 【HDOJ】1547 Bubble Shooter

    两次BFS,一次扫描关联点.一次扫描可能掉落的情况(即再次扫描所有非爆炸的联通点).余下未被扫描的点均爆炸. #include <cstdio> #include <cstring& ...

  10. Linux下查看进程(程序)启动时的环境变量

    背景: 因最近试安装Linux下的jira,有一个中文插件安装后,一旦设置开机启动后,它是英文,而在终端再重新启动一次后呢,似乎插件生效,它又恢复为正常中文界面,我首先想这这涉及到一个环境变量的问题, ...