Advanced   Java   Services JAVA-Praktikum Back Next Up Home
Rekursive Methoden (Lösung)
// ------------------------------- Aufgabe 1 ------------------------------- \\
public class FibonacciRekursiv
{
   public static void main(String args[])
   {
      for(int i=0 ; i<10 ; i++)
         System.out.println("fibo("+i+") = " + fibo(i) ) ;
   }

   public static double fibo(int n)
   {
      if (n<=1) return n;
      if (n>92) return Double.POSITIVE_INFINITY;

      return fibo(n-1) + fibo(n-2) ;
   }
}  // end class


// ------------------------------- Aufgabe 2 ------------------------------- \\
public class Binomial
{
   public static void main(String args[])
   {
      for(int n=0 ; n<10 ; n++)
      {
         System.out.print(n + " : ") ;
         for(int v=0; v<=n; v++)
            System.out.print(binkoff(n,v) + "\t" ) ;
         System.out.println() ;
      }
      System.out.println(binkoff(4,5));
   }

   public static double binkoff(int n, int v)
   {
      if (v>n) throw new IllegalArgumentException(n +" >= " + v + " is false") ;
      if (v==0) return 1 ;
      if (v==n) return 1 ;

      return binkoff(n-1,v) + binkoff(n-1, v-1);
   }
}  // end class


// ------------------------------- Aufgabe 3 ------------------------------- \\
public class GGT
{
   public static void main(String args[])
   {
      System.out.println( ggt(6,8));
      System.out.println("-----------------");
      System.out.println( ggt(-75,100));
      System.out.println("-----------------");
      System.out.println( ggt(75,-100));
      System.out.println("-----------------");
      System.out.println( ggt(-75,-100));
      System.out.println("-----------------");
      //System.out.println( ggt(0,17));
      System.out.println("-----------------");
      //System.out.println( ggt(17,0));
   }

   public static int ggt(int a, int b)
   {
      if(a==0 || b==0) throw new ArithmeticException("Both arguments must not be 0");

      return (a%b==0) ? b : ggt(b, a%b) ;
   }
}  // end class
Valid XHTML 1.0 Strict top Back Next Up Home