Advanced  Services C#-Praktikum Back Next Up Home
Mathematische Methoden (Lösung)
// ------------------------------ MathMethods.cs ------------------------------- \\

public class MathMethods
{

   static void Main(string args[])
   {
      for(int i=0 ; i<10 ; i++)
         Console.WriteLine("Fibo("+i+") = " + Fibo(i) ) ;

      for(int i=0 ; i<10 ; i++)
         Console.WriteLine("Fak("+i+") = " + Fak(i) ) ;

      Console.WriteLine("KgV(6,7) = " + KgV(6,7) ) ;
      Console.WriteLine("KgV(12,15) = " + KgV(12,15) ) ;

      Console.WriteLine("GgT(6,7) = " + GgT(6,7) ) ;
      Console.WriteLine("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