Facebook
From Eryk Bartosik, 4 Years ago, written in C#.
This paste is a reply to Przyzwoity (Z instrumentacją) from Eryk Bartosik - view diff
Embed
Download Paste or View Raw
Hits: 343
  1. using System;
  2. using System.Numerics;
  3. using System.Diagnostics;
  4.  
  5. namespace ProjektAlgorytmy2
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             BigInteger[] tablica = new BigInteger[] { 100913, 1009139, 10091401, 100914061, 1009140611,  10091406133, 100914061337, 1009140613399 }; //tablica z wyrazami
  12.             Stopwatch Stoper = new Stopwatch();
  13.             for( int i = 0 ; i < tablica.Length ; i++ ) //pętla wykonująca funkcję IsPrime dla wszystkich wyrazów tablicy ,,tablica"
  14.             {
  15.                 long counter;
  16.                 Stoper.Start();
  17.                 IsPrime2(tablica[i] , out counter);
  18.                 Stoper.Stop();
  19.                 Console.WriteLine("Czas: "+Stoper.Elapsed);
  20.                 Console.WriteLine("Ilość operacji: "+counter);
  21.                 Stoper.Reset();
  22.             }
  23.         }
  24.  
  25.         static bool IsPrime2( BigInteger Num , out long operacja ) //funkcja przyzwoita
  26.         {
  27.             operacja = 1;
  28.             if (Num < 2) return false;
  29.             else if (Num < 4) return true;
  30.             else if (Num % 2 == 0) return false;
  31.             else
  32.             {
  33.                 for (BigInteger u = 3; u*u <= Num; u += 2)
  34.                     {
  35.                         operacja++;
  36.                         if (Num % u == 0) return false;
  37.                     }
  38.             }
  39.             return true;
  40.         }
  41.     }
  42. }
  43.