package ints;
public class Fibonacci{
public static int fibRec(int n){
if (n < 0) throw new ArithmeticException();
else if (n == 0) return 1;
else if (n == 1) return 1;
else return fibRec(n-1) + fibRec(n-2);
}
public static int fibIter(int n){
int aux;
int act = 1;
int pre = 1;
while (n > 1){
aux = act;
act = pre + act;
pre = aux;
n--;
}
return act;
}
public static int[] fibSequence(int n){
if (n < 0) throw new ArithmeticException();
int[] out = new int[n+1];
out[0] = 1;
if (n == 0) return out;
out[1] = 1;
for (int i = 1;i < n;i++)
out[i+1] = out[i] + out[i-1];
return out;
}
/*
public static void main(String[] args){
int[] fs = fibSequence(10);
for (int i = 0;i < fs.length;i++)
System.out.print(fs[i] + " ");
}
*/
}
|