Advanced  Services C#-Praktikum Back Next Up Home
Eine Klasse mit mathematischen Methoden (Lösung)
// ------------------------------ MathUtil.cs ------------------------------- \\

public class MathUtil
{
   // ------------------------------- Round() --------------------------------- \\
   public static double Round(double x, int stellen)
   {
      return Math.Round(x,stellen) ;
   }

   // -------------------------------- Max() ---------------------------------- \\
   public static int Max(int arr[])
   {
      int max = arr[0] ;

      for(int i=1; i<arr.Length; i++)
         if (max<arr[i]) max = arr[i] ;

      return max;
   }

   // -------------------------------- Max() ---------------------------------- \\
   public static double Max(double arr[])
   {
      double max = arr[0] ;

      for(int i=1; i<arr.Length; i++)
         if (max<arr[i]) max = arr[i] ;

      return max;
   }

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

   // -------------------------------- KgV() ---------------------------------- \\
   public static double KgV(int a, int b)
   {
      if (a*b==0) return 0 ;

      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 ;
   }

   // -------------------------------- GgT() ---------------------------------- \\
   public static double GgT(int a, int b)
   {
      if (a*b==0) return 0 ;

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

   // ------------------------------- Fibo() ---------------------------------- \\
   public static double Fibo(int n)
   {
      if (n < 0 || n > 92 )
         return -1 ;

      if (n<=1) return n;

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

      vor = 0 ; vorvor = 1 ;
      for(int i=0; i<n ; i++ )
      {
         fibo = vor + vorvor ;
         vorvor = vor ;
         vor = fibo;
      }

      return fibo ;
   }

   // -------------------------------- Fak() ---------------------------------- \\
   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;

   }

   // ------------------------------ IsPrime() -------------------------------- \\
   public static bool IsPrime(long p)
   {
      if (p==0) return false;
      if (p<0) p = -p ;
      if (p==1) return false;
      if (p==2) return true;
      if (p%2==0) return false;

      for (int i=3 ; i<=Math.sqrt(p) ; i+=2)
         if (p%i==0)
            return false;

      return true;
   }

   // ------------------------------- Binko() --------------------------------- \\
   public static double Binko(int n, int v)
   {
      if (v>n) throw new Exception(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 MathUtil
Valid XHTML 1.0 Strict top Back Next Up Home