Advanced   Java   Services JAVA-Praktikum Back Next Up Home
Mathematische Methoden (Lösung)
// ------------------------------ MathMethods.java ------------------------------- \\

public class MathMethods
{

   public static void main(String args[])
   {
      for(int i=0 ; i<10 ; i++)
         System.out.println("fibo("+i+") = " + fibo(i) ) ;

      for(int i=0 ; i<10 ; i++)
         System.out.println("fak("+i+") = " + fak(i) ) ;

      System.out.println("kgV(6,7) = " + kgV(6,7) ) ;
      System.out.println("kgV(12,15) = " + kgV(12,15) ) ;

      System.out.println("ggT(6,7) = " + ggT(6,7) ) ;
      System.out.println("ggT(12,15) = " + ggT(12,15) ) ;
   }

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

      double vor = 1 , vorvor = 0 , fibo = 0 ;

      for(int i=1; i<n; i++)  //  int geht bis 46 einschließlich, long bis 92 einschl.
      {
         fibo = vorvor + vor ;
         vorvor   = vor;
         vor      = fibo;
      }

      return fibo;
   }

   public static double fak(int n)
   {
      if (n<0) return Double.NaN ;
      if (n<=1) return 1;
      if (n>92) return Double.NaN ;

      double fak=1 ;

      for(int i=1; i<n; i++)
         fak = fak*(i+1) ;

      return fak;

   }

   public static int sgn(double x)
   {
      return x>0 ? 1 : x<0 ? -1 : 0 ;
   }


   public static double kgV(int a, int b)
   {
      double max = a < b ? b : a ;
      double min = a < b ? a : b ;
      int i;

      for(i=1 ; (i*max)%min != 0 ; i++)
         ;

      return i*max ;
   }

   public static double ggT(int a, int b)
   {
       if (a*b==0) return 0 ;

       return a*b/ kgV(a,b) ;
    }

}  // end class
Valid XHTML 1.0 Strict top Back Next Up Home