フィボナッチ数列アルゴリズム
1457 ワード
フィボナッチ数列は一般式を有する.
n>2の場合、f(n)=f(n−1)+f(n−2);n=1またはn=2の場合、f(1)=f(2)=1となる.
コード実装:
n>2の場合、f(n)=f(n−1)+f(n−2);n=1またはn=2の場合、f(1)=f(2)=1となる.
コード実装:
package com.lk.C;
public class Test4 {
public static int compute(int index){
if((index == 1)||(index == 2)){
return 1;
}else{
return compute(index-1)+compute(index-2);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(compute(20));
}
}
6765