Advertisement
Guest User

Untitled

a guest
May 26th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Diagnostics;
  4.  
  5. namespace Prime
  6. {
  7.     class Program
  8.     {
  9.         private static int operations;
  10.         private static double time;
  11.  
  12.         public static void Test()
  13.         {
  14.             long[] numbers =
  15.             {
  16.                 100913,
  17.                 1009139,
  18.                 10091401,
  19.                 100914061,
  20.                 1009140611,
  21.                 10091406133,
  22.                 100914061337,
  23.                 1009140613399
  24.             };
  25.  
  26.             Console.WriteLine(
  27.                 "{0, 20}{1, 20}{2, 20} {3, 20}",
  28.                 "N",
  29.                 "Prime?",
  30.                 "Operations",
  31.                 "Time");
  32.  
  33.             foreach (long n in numbers)
  34.             {
  35.                 bool p = IsPrimeMarcin(n);
  36.                 Console.WriteLine(
  37.                     "{0, 20}{1, 20}{2, 20}{3, 20}ms",
  38.                     n,
  39.                     p,
  40.                     operations,
  41.                     time.ToString("0.000"));
  42.             }
  43.         }
  44.  
  45.         static double GetElapsedTime(long start)
  46.         {
  47.             return (Stopwatch.GetTimestamp() - start) * (1000.0 / Stopwatch.Frequency);
  48.         }
  49.  
  50.         // wersja naiwna
  51.         static bool IsPrimeNaive(long n)
  52.         {
  53.             long start = Stopwatch.GetTimestamp();
  54.             operations = 0;
  55.             if (n < 2)
  56.             {
  57.                 operations++;
  58.                 time = GetElapsedTime(start);
  59.                 return false;
  60.             }
  61.             else if (n < 4)
  62.             {
  63.                 operations++;
  64.                 time = GetElapsedTime(start);
  65.                 return true;
  66.             }
  67.             else if (n % 2 == 0)
  68.             {
  69.                 operations++;
  70.                 time = GetElapsedTime(start);
  71.                 return false;
  72.             }
  73.             else
  74.             {
  75.                 for (long u = 3; u < n / 2; u += 2)
  76.                 {
  77.                     operations++;
  78.                     if (n % u == 0)
  79.                     {
  80.                         time = GetElapsedTime(start);
  81.                         return false;
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             time = GetElapsedTime(start);
  87.             return true;
  88.         }
  89.  
  90.         // wersja efektywna
  91.         static bool IsPrime(long n)
  92.         {
  93.             long start;
  94.             start = Stopwatch.GetTimestamp();
  95.             operations = 0;
  96.             if (n < 2)
  97.             {
  98.                 operations++;
  99.                 time = GetElapsedTime(start);
  100.                 return false;
  101.             }
  102.             else if (n < 4)
  103.             {
  104.                 operations++;
  105.                 time = GetElapsedTime(start);
  106.                 return true;
  107.             }
  108.             else if (n % 2 == 0)
  109.             {
  110.                 operations++;
  111.                 time = GetElapsedTime(start);
  112.                 return false;
  113.             }
  114.  
  115.             long[] values = { 0, 2, 6, 8, 12, 14, 18, 24, 26 };
  116.  
  117.             for (long u = 5; u * u <= n - 30; u += 30)
  118.             {
  119.                 foreach (long value in values)
  120.                 {
  121.                     operations++;
  122.                     if (n % (u + value) == 0)
  123.                     {
  124.                         time = GetElapsedTime(start);
  125.                         return false;
  126.                     }
  127.                 }
  128.             }
  129.  
  130.             time = GetElapsedTime(start);
  131.             return true;
  132.         }
  133.  
  134.         // wersja naiwna
  135.         static bool IsPrimeMarcin(long n)
  136.         {
  137.             long start;
  138.             start = Stopwatch.GetTimestamp();
  139.  
  140.             // Corner cases
  141.             if (n <= 1)
  142.             {
  143.                 operations++;
  144.                 time = GetElapsedTime(start);
  145.                 return false;
  146.             }
  147.             if (n <= 3)
  148.             {
  149.                 operations++;
  150.                 time = GetElapsedTime(start);
  151.                 return true;
  152.             }
  153.  
  154.             // This is checked so that we can skip
  155.             // middle five numbers in below loop
  156.             if (n % 2 == 0 || n % 3 == 0)
  157.             {
  158.                 operations++;
  159.                 time = GetElapsedTime(start);
  160.                 return false;
  161.             }
  162.  
  163.             for (long i = 5; i * i <= n; i += 6)
  164.             {
  165.                 operations++;
  166.                 time = GetElapsedTime(start);
  167.                 if (n % i == 0 || n % (i + 2) == 0)
  168.                     return false;
  169.             }
  170.  
  171.             return true;
  172.         }
  173.  
  174.         static void Main(string[] args)
  175.         {
  176.             Test();
  177.             Console.ReadKey();
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement