Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include<iostream>
  5. #include <vector>
  6. #include <mutex>
  7. #include <thread>
  8.  
  9. using namespace std;
  10. std::mutex mtx;
  11. class Conjuctare {
  12. public:
  13. int x;
  14. int y;
  15. int result;
  16. Conjuctare(int x1, int y1, int result1)
  17. {
  18. x = x1;
  19. y = y1;
  20. result = result1;
  21. }
  22.  
  23. };
  24.  
  25.  
  26. bool is_divisible(int, int);
  27. bool is_prime(int);
  28. void goldbach_call(int, int, std::vector<Conjuctare*>& test);
  29. std::vector<Conjuctare*> goldbach_conjuctare(int);
  30. void main()
  31. {
  32. int n = 1000;
  33. int max = 40;
  34.  
  35. std::vector<Conjuctare*> test;
  36. std::vector<Conjuctare*> result;
  37. std::vector<std::vector<Conjuctare*>> wynik;
  38.  
  39. for (int i = 0; i < 4; i++)
  40. {
  41. wynik.push_back(std::vector<Conjuctare*>());
  42. }
  43.  
  44.  
  45. int w = 0;
  46. std::vector<std::thread> vecOfThreads;
  47. auto start = std::chrono::high_resolution_clock::now();
  48. goldbach_call(1000, 2000, test);
  49. auto end = std::chrono::high_resolution_clock::now();
  50. std::chrono::duration<double, std::milli> elapsed = end - start;
  51. std::cout << "Waited Synch " << elapsed.count() << " ms\n";
  52. start = std::chrono::high_resolution_clock::now();
  53. for (int i = 0; i < 4; i++)
  54. {
  55. vecOfThreads.push_back(std::thread(goldbach_call, n, n + 250, wynik[i]));
  56. n += 250;
  57. }
  58. for (int i = 0; i < 4; i++)
  59. {
  60. wynik.push_back(std::vector<Conjuctare*>());
  61. }
  62. for (std::thread& th : vecOfThreads)
  63. {
  64. if (th.joinable())
  65. th.join();
  66. }
  67.  
  68. for (int i = 0; i < wynik.size(); i++)
  69. {
  70. for (int j = 0; j < wynik[i].size(); j++)
  71. {
  72. result.push_back(wynik[i].~vector[j]);
  73. }
  74. }
  75. end = std::chrono::high_resolution_clock::now();
  76. elapsed = end - start;
  77. std::cout << "Waited Async " << elapsed.count() << " ms\n";
  78.  
  79. if (test.size() == result.size())
  80. {
  81. std::cout << "The Vectors has the same size!";
  82. }
  83.  
  84. system("Pause");
  85.  
  86. }
  87.  
  88. void goldbach_call(int min, int max, std::vector<Conjuctare*> & final)
  89. {
  90. for (int i = min; i < max; i += 2) {
  91. std::vector<Conjuctare*> test = goldbach_conjuctare(i);
  92. for (int j = 0; j < test.size(); j++)
  93. {
  94. final.push_back(test[j]);
  95.  
  96. }
  97. }
  98. }
  99.  
  100. std::vector<Conjuctare*> goldbach_conjuctare(int n)
  101. {
  102. int p1;
  103. std::vector<Conjuctare*> dane;
  104. for (p1 = 2; p1 <= n; p1++)
  105. {
  106. if ((is_prime(p1)) && (is_prime(n - p1)))
  107. {
  108.  
  109. Conjuctare* cojn = new Conjuctare(p1, (n - p1), n);
  110. dane.push_back(cojn);
  111.  
  112. }
  113. }
  114. return dane;
  115. }
  116.  
  117. bool is_divisible(int x, int y)
  118. {
  119. if (x % y == 0)
  120. return true;
  121. else
  122. return false;
  123. }
  124.  
  125. bool is_prime(int a)
  126. {
  127. if (a == 1) {
  128. return false;
  129. }
  130. for (int i = a / 2; i > 1; i--)
  131. if (is_divisible(a, i))
  132. return false;
  133. return true;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement