Guest User

Untitled

a guest
May 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. public class Fibonacci {
  2.  
  3. /**
  4. * Berechnet die n-te Fibonacci-Zahl iterativ.
  5. * Fuer ungueltige Eingaben wird -1 zurueckgeliefert.
  6. */
  7. public static int fibIter(int n){
  8. int i , x , y , z =0;
  9. if (n==0)
  10. return 1;
  11. if (n==1)
  12. return 1;
  13. if (n<0)
  14. return -1;
  15. else {
  16. x =1;
  17. y =1;
  18. i = 1 ;
  19. while ( i < n ) {
  20. z = x + y ;
  21. x = y;
  22. y = z;
  23. i = i +1;
  24. }
  25. return z;}
  26. }
  27.  
  28. /**
  29. * Berechnet die n-te Fibonacci-Zahl rekusrsiv.
  30. * Fuer ungueltige Eingaben wird -1 zurueckgeliefert.
  31. */
  32. public static int fibRek(int n){
  33. if (n==0)
  34. return 1;
  35. if (n==1)
  36. return 1;
  37. if (n<0)
  38. return -1;
  39. return fibRek(n-1) + fibRek(n-2);
  40.  
  41. }}
Add Comment
Please, Sign In to add comment