斐波那契数列(fabnacci)java实现
斐波那契数列定义: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):
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
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实现的更多相关文章
- 几种复杂度的斐波那契数列的Java实现
一:斐波那契数列问题的起源 13世纪初期,意大利数论家Leonardo Fibonacci在他的著作Liber Abaci中提出了兔子的繁殖问题: 如果一开始有一对刚出生的兔子,兔子的长大需要一个月, ...
- 斐波那契数列【java实现】
java 实现斐波那契数列 以下是Java代码实现(递归与递推两种方式): import java.util.Scanner; /** * Fibonacci * * @author tongqian ...
- 斐波那契数列(Java)
一.什么是斐波那契数列 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为& ...
- 剑指Offer-7.斐波那契数列(C++/Java)
题目: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 分析: 斐波那契数列是0,1,1,2,3,5,8,13...也就是当前 ...
- 从斐波那契数列看java方法的调用过程
先看斐波那契数列的定义: 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为 ...
- (转)从斐波那契数列看Java方法的调用过程
斐波那契数列的定义: 斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家列安纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔 ...
- 算法小节(一)——斐波那契数列(java实现)
看到公司的笔试题中有一道题让写斐波那契数列,自己忙里偷闲写了一下 什么是斐波那契数列:斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
- 【斐波那契数列】java探究
题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 解析 (1)递归方式 对于公式f(n) = f(n-1) + f(n ...
- 剑指offer第二版面试题10:斐波那契数列(JAVA版)
题目:写一个函数,输入n,求斐波那契数列的第n项.斐波那契数列的定义如下: 1.效率很低效的解法,挑剔的面试官不会喜欢 使用递归实现: public class Fibonacci { public ...
- HDU 5686 斐波那契数列、Java求大数
原题:http://acm.hdu.edu.cn/showproblem.php?pid=5686 当我们要求f[n]时,可以考虑为前n-1个1的情况有加了一个1. 此时有两种情况:当不适用第n个1进 ...
随机推荐
- [转] 关于UIView
[转载] 原文地址 :http://blog.csdn.net/itianyi/article/details/8982518 UIView是开发中使用得最多的控件了,深入的理解很有必要. UIVie ...
- UIView -> image & 本地时间获取
//UIView 转换为图片 UIGraphicsBeginImageContext(self.rootsView.bounds.size); [_rootsView.layer renderInCo ...
- Hive学习之一 《Hive的介绍和安装》
一.什么是Hive Hive是建立在 Hadoop 上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在 Hadoop 中的大规模数据 ...
- WebService cxf 接口中获得拦截器参数
1. 拦截器中put属性 Message message = PhaseInterceptorChain.getCurrentMessage(); message.put("AuthCode ...
- Javascript模块化编程 require.js使用详解
一.为什么用require.js,产生的背景 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载. & ...
- C# Attribute
Attribute 是C#非常重要的一块内容,需要研究一下. Attribute 的简单使用:简而言之,就是可以自定义通用标志位,而不是在每个所需的类型中分别增加标志位. //class专用attr ...
- mac 下 配置 阿帕奇
1.从 tomcat 官网(http://tomcat.apache.org/download-90.cgi)下载 完整的 tomcat包. 2.将红框中的包下载完,然后解压到任意一个目录,将其命名为 ...
- Android Service 启动和停止服务
activity_main.xml 定义两个Button控件,start_service和stop_service. <LinearLayout xmlns:android="http ...
- Black Box
http://poj.org/problem?id=1442 #include<cstdio> #include<algorithm> #include<queue> ...
- BZOJ 1023 [SCOI2009]生日快乐
1024: [SCOI2009]生日快乐 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1729 Solved: 1219[Submit][Statu ...

(sequence 

