package com.Lucky.base;
/*
斐波那契数列(Fibonacci sequence),又称黄金分割数列,
因数学家莱昂纳多·斐波那契(Leonardo Fibonacci)
以兔子繁殖为例子而引入,故又称为“兔子数列”,
指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,
斐波那契数列以如下被以递推的方法定义:F(0)=0,F(1)=1,
F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N*)
在现代物理、准晶体结构、化学等领域,斐波那契数列都有直接的应用,
*/
public class FibonacciUnion {
public static void main(String[] args) {
System.out.println(test(8));
System.out.println(forTest(8));
}
//递归方法
public static int test(int n){
int result=1;
if(n==1 || n==2){
return result;
}else{
result = test(n-1)+test(n-2);
}
return result;
}
//循环
public static int forTest(int n){
int res=0;
int num1=1;
int num2=1;
for (int i = 3; i <= n; i++) {
res=num1+num2; //第三项
num1=num2; //更改第一项为第二项
num2=res; //更改第二项为第三项
}
return res;
}
}