abc123mewot

Untitled

May 31st, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. // CollatzConjecture.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <thread>
  7. #include <chrono>
  8.  
  9. #include "Progress.cpp"
  10.  
  11. volatile bool modifying = false;
  12. bool finished = false;
  13. uint64_t maxLen = 1;
  14. uint64_t maxStep = 0;
  15. uint64_t startTime = 0;
  16. Progress *progressArray;
  17. std::thread *threads;
  18.  
  19. void userIoThread(uint8_t cores) {
  20.     using namespace std;
  21.     using namespace std::chrono;
  22.     this_thread::sleep_for(milliseconds(1000));
  23.     while (!finished) {
  24.         double avgPercent = 0;
  25.         for (int i = 0; i < cores; i++) {
  26.             while (modifying) this_thread::sleep_for(std::chrono::milliseconds(10));
  27.             modifying = true;
  28.             Progress p = progressArray[i];
  29.             avgPercent += p.percent();
  30.             modifying = false;
  31.         }
  32.         avgPercent /= cores;
  33.         uint64_t curTime = clock();
  34.         uint64_t seconds = (curTime - startTime) / 1000.0;
  35.         uint64_t minutes = seconds / 60;
  36.         seconds -= minutes * 60;
  37.         uint64_t hours = (minutes - (minutes % 60)) / 60;
  38.         minutes -= hours * 60;
  39.         uint64_t days = (hours - (hours % 24)) / 24;
  40.         hours -= days * 24;
  41.         cout << 100.0 * avgPercent << "% ";
  42.         cout << days << "d ";
  43.         cout << hours << "h ";
  44.         cout << minutes << "m ";
  45.         cout << seconds << "s ";
  46.         cout << endl;
  47.         this_thread::sleep_for(milliseconds(1000));
  48.     }
  49. }
  50. void calcRange(uint64_t start, uint64_t stop, uint8_t threadId, bool update) {
  51.     for (uint64_t current = start; current < stop; current++) {
  52.         uint64_t num = current;
  53.         uint64_t steps = 0;
  54.         using namespace std;
  55.         using namespace std::chrono;
  56.         while (true) {
  57.             steps++;
  58.             if (num % 2 == 0)
  59.                 num /= 2;
  60.             else
  61.                 num = 3 * num + 1;
  62.             if (num <= 1)
  63.                 break;
  64.         }
  65.         while (modifying)
  66.             this_thread::sleep_for(nanoseconds(10));
  67.         modifying = true;
  68.         if (update)
  69.             progressArray[threadId].current = current;
  70.         if (steps > maxStep) {
  71.             maxStep = steps;
  72.             maxLen = current;
  73.         }
  74.         modifying = false;
  75.     }
  76. }
  77.  
  78. int main() {
  79.     using namespace std;
  80.     using namespace std::chrono;
  81.     //Get starting value
  82.     cout << "Starting value: ";
  83.     uint64_t amount;
  84.     cin >> amount;
  85.     if (amount <= 0)
  86.         amount = 1;
  87.     //Get amount of threads to use
  88.     cout << "Threads: ";
  89.     int cores;
  90.     cin >> cores;
  91.     if (cores <= 0)
  92.         cores = 1;
  93.     //Initialize variables
  94.     startTime = clock();
  95.     threads = new thread[cores];
  96.     progressArray = new Progress[cores];
  97.     thread userInput = thread(userIoThread, cores);
  98.     userInput.detach();
  99.     //Initiate threads and other info
  100.     uint64_t rem = amount % cores;
  101.     uint64_t chg = (amount - rem) / cores;
  102.     for (uint8_t i = 0; i < cores; i++) {
  103.         uint64_t start = i * chg;
  104.         uint64_t stop = (i + 1) * chg;
  105.         progressArray[i] = Progress(start, stop);
  106.         if (i >= cores - 1)
  107.             stop += rem;
  108.         threads[i] = thread(calcRange, start, stop, i, true);
  109.     }
  110.     for (uint8_t i = 0; i < cores; i++)
  111.         threads[i].join();
  112.     finished = true;
  113.     //Delete allocated memory
  114.     cout << "Deleting allocated memory!" << endl;
  115.     delete[] threads;
  116.     delete[] progressArray;
  117.     //Print information
  118.     cout << "The value that takes the most steps is: " << maxLen << endl;
  119.     cout << "The number of steps it took was: " << maxStep << endl;
  120.     uint64_t curTime = clock();
  121.     float minutes = ((curTime - startTime) / 1000.0) / 60.0;
  122.     cout << "And took " << minutes << "m" << endl;
  123.     //Wait for user input
  124.     cout << "Press Ctrl+C to continue" << endl;
  125.     string dummy;
  126.     cin >> dummy;
  127.     //Exit
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment