Advertisement
bipping

diviseur commun

Feb 16th, 2023
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | Science | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <thread>
  6. #include <array>
  7. #include <functional>
  8. #include <mutex>
  9. #include <chrono>
  10. #include <ctime>
  11. #include <condition_variable>
  12. #include <list>
  13.  
  14. struct Parametre {
  15.     const double div1_min = 11300;
  16.     const double div1_max = 11200;
  17.     const double div1_step = 1;
  18.     const double div1_target = 11250;
  19.     const double div2_min = 150000;
  20.     const double div2_max = 300000;
  21.     const double div2_step = 1000;
  22.     const double div2_target = 250000;
  23.     const double div_time_min = 1;
  24.     const double div_time_max = 1800000000;
  25.     const uint8_t max_results = 10;
  26.     const uint8_t results_run = 20;
  27.     const uint8_t max_threads = 4;
  28. };
  29.  
  30. // stockage des données de calcul
  31. struct Resultat {
  32.     int index;
  33.     double div1;
  34.     double div2;
  35.     double div_time;
  36.     double div1et2;
  37. };
  38.  
  39. std::vector<Resultat> results;
  40. std::mutex mtx_results;
  41. std::condition_variable cv_results;
  42. bool is_results_full = false;
  43.  
  44. void calculate_collision_time(double div1, double div2, int index, Parametre params) {
  45.     for (double div_time = params.div_time_min; div_time <= params.div_time_max; div_time++) {
  46.         if (fmod(div_time, div1) == 0 && fmod(div_time, div2) == 0) {
  47.             Resultat result;
  48.             result.index = index;
  49.             result.div1 = div1;
  50.             result.div2 = div2;
  51.             result.div_time = div_time;
  52.             result.div1et2 = sqrt(pow(div1 - params.div1_target, 2) + pow(div2 - params.div2_target, 2));
  53.  
  54.             std::unique_lock<std::mutex> lock(mtx_results);
  55.             results.push_back(result);
  56.             lock.unlock();
  57.             cv_results.notify_one();
  58.             break;
  59.         }
  60.     }
  61. }
  62.  
  63. int main() {
  64.     int index = 0;
  65.     std::vector<std::thread> threads;
  66.     Parametre params;
  67.  
  68.     for (double div1 = params.div1_min; div1 <= params.div1_max; div1 += params.div1_step) {
  69.         for (double div2 = params.div2_min; div2 <= params.div2_max; div2 += params.div2_step) {
  70.             while (threads.size() >= params.max_threads) {
  71.                 std::unique_lock<std::mutex> lock(mtx_results);
  72.                 cv_results.wait(lock, [params] { return results.size() >= params.results_run || is_results_full; });
  73.  
  74.                 lock.unlock();
  75.  
  76.                 if (results.size() >= params.results_run) {
  77.                     for (int i = 0; i < params.results_run; i++) {
  78.                         Resultat result = results[i];
  79.                         double time_microseconds = fmod(result.div_time, 1000000);
  80.                         double time_milliseconds = (fmod(result.div_time, 1000)) / 1000;
  81.                         double time_seconds = result.div_time / 1000000;
  82.                         std::cout << result.index << ": div1=" << result.div1 << " div2=" << result.div2
  83.                             << " time=" << time_seconds << "s " << time_milliseconds << "ms "
  84.                             << time_microseconds << "us div1et2=" << result.div1et2 << std::endl;
  85.                     }
  86.                     results.clear();
  87.                 }
  88.             }
  89.  
  90.             threads.push_back(std::thread(calculate_collision_time, div1, div2, index, params));
  91.             index++;
  92.         }
  93.     }
  94.  
  95.     for (std::thread& thread : threads) {
  96.         thread.join();
  97.     }
  98.  
  99.     if (results.size() > 0) {
  100.         for (const Resultat& result : results) {
  101.             double time_microseconds = fmod(result.div_time, 1000000);
  102.             double time_milliseconds = (fmod(result.div_time, 1000)) / 1000;
  103.             double time_seconds = result.div_time / 1000000;
  104.             std::cout << result.index << ": div1=" << result.div1 << " div2=" << result.div2 << "\n time="
  105.                 << time_seconds << "s " << time_milliseconds << "ms " << time_microseconds << "us div1et2="
  106.                 << result.div1et2 << std::endl;
  107.         }
  108.  
  109.     }
  110.  
  111.     return 0;
  112. }
Tags: math
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement