Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. // Algorytm optymalny
  2.  
  3.  
  4. Int64 i, j, zakres, dokad;
  5. Int64[] tablica = new Int64[214748364];
  6. zakres = 2147424;
  7. dokad = (Int64)Math.Floor(Math.Sqrt(zakres));
  8.  
  9. //inicjuj tablice
  10. for (i = 1; i <= zakres; i++) tablica[i] = i;
  11.  
  12. //algorytm - sito eratostenesa
  13. for (i = 2; i <= dokad; i++)
  14. {
  15. if (tablica[i] != 0)
  16. {
  17. j = i + i;
  18. while (j <= zakres)
  19. {
  20. tablica[j] = 0;
  21. j += i;
  22. }
  23. }
  24. }
  25.  
  26. for (int p = 0; p < mensuringPoint.Length; p++)
  27. {
  28. Console.ForegroundColor = ConsoleColor.Yellow;
  29. long StartingTime1 = Stopwatch.GetTimestamp();
  30. Console.Write(IsPrimeX(mensuringPoint[p]));
  31. long EndingTime1 = Stopwatch.GetTimestamp();
  32. long ElapsedTime1 = EndingTime1 - StartingTime1;
  33. double ElapsedSeconds1 = ElapsedTime1 * (1.0 / Stopwatch.Frequency);
  34.  
  35.  
  36. Console.WriteLine(" Dla wartości: " + mensuringPoint[p]);
  37. Console.ForegroundColor = ConsoleColor.Red;
  38. Console.WriteLine("Ilośc dzielenia modulo: " + licz);
  39. Console.ForegroundColor = ConsoleColor.Green;
  40. Console.WriteLine("Czas wykonania: " + String.Format("{0:N7}", ElapsedSeconds1));
  41. Console.ResetColor();
  42. Console.WriteLine("-----------------------------------------");
  43.  
  44. licz = 1;
  45. }
  46.  
  47. bool IsPrimeX(Int64 Num)
  48. {
  49.  
  50. if (Num < 2) return false;
  51. else if (Num < 4) return true;
  52. else if (Num % 2 == 0) return false;
  53. else for (int y = 3; tablica[y] < Math.Sqrt(Num); y += 2)
  54. {if (tablica[y] != 0)
  55. { licz++;
  56.  
  57. if (Num % y == 0)
  58. {
  59.  
  60. return false;
  61. }
  62. }
  63. }
  64.  
  65. return true;
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement