Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Fibonacci {
- /**
- * Berechnet die n-te Fibonacci-Zahl iterativ.
- * Fuer ungueltige Eingaben wird -1 zurueckgeliefert.
- */
- public static int fibIter(int n){
- int i , x , y , z =0;
- if (n==0)
- return 1;
- if (n==1)
- return 1;
- if (n<0)
- return -1;
- else {
- x =1;
- y =1;
- i = 1 ;
- while ( i < n ) {
- z = x + y ;
- x = y;
- y = z;
- i = i +1;
- }
- return z;}
- }
- /**
- * Berechnet die n-te Fibonacci-Zahl rekusrsiv.
- * Fuer ungueltige Eingaben wird -1 zurueckgeliefert.
- */
- public static int fibRek(int n){
- if (n==0)
- return 1;
- if (n==1)
- return 1;
- if (n<0)
- return -1;
- return fibRek(n-1) + fibRek(n-2);
- }}
Add Comment
Please, Sign In to add comment