Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CollatzConjecture.cpp : Defines the entry point for the console application.
- //
- #include <iostream>
- #include <string>
- #include <thread>
- #include <chrono>
- #include "Progress.cpp"
- volatile bool modifying = false;
- bool finished = false;
- uint64_t maxLen = 1;
- uint64_t maxStep = 0;
- uint64_t startTime = 0;
- Progress *progressArray;
- std::thread *threads;
- void userIoThread(uint8_t cores) {
- using namespace std;
- using namespace std::chrono;
- this_thread::sleep_for(milliseconds(1000));
- while (!finished) {
- double avgPercent = 0;
- for (int i = 0; i < cores; i++) {
- while (modifying) this_thread::sleep_for(std::chrono::milliseconds(10));
- modifying = true;
- Progress p = progressArray[i];
- avgPercent += p.percent();
- modifying = false;
- }
- avgPercent /= cores;
- uint64_t curTime = clock();
- uint64_t seconds = (curTime - startTime) / 1000.0;
- uint64_t minutes = seconds / 60;
- seconds -= minutes * 60;
- uint64_t hours = (minutes - (minutes % 60)) / 60;
- minutes -= hours * 60;
- uint64_t days = (hours - (hours % 24)) / 24;
- hours -= days * 24;
- cout << 100.0 * avgPercent << "% ";
- cout << days << "d ";
- cout << hours << "h ";
- cout << minutes << "m ";
- cout << seconds << "s ";
- cout << endl;
- this_thread::sleep_for(milliseconds(1000));
- }
- }
- void calcRange(uint64_t start, uint64_t stop, uint8_t threadId, bool update) {
- for (uint64_t current = start; current < stop; current++) {
- uint64_t num = current;
- uint64_t steps = 0;
- using namespace std;
- using namespace std::chrono;
- while (true) {
- steps++;
- if (num % 2 == 0)
- num /= 2;
- else
- num = 3 * num + 1;
- if (num <= 1)
- break;
- }
- while (modifying)
- this_thread::sleep_for(nanoseconds(10));
- modifying = true;
- if (update)
- progressArray[threadId].current = current;
- if (steps > maxStep) {
- maxStep = steps;
- maxLen = current;
- }
- modifying = false;
- }
- }
- int main() {
- using namespace std;
- using namespace std::chrono;
- //Get starting value
- cout << "Starting value: ";
- uint64_t amount;
- cin >> amount;
- if (amount <= 0)
- amount = 1;
- //Get amount of threads to use
- cout << "Threads: ";
- int cores;
- cin >> cores;
- if (cores <= 0)
- cores = 1;
- //Initialize variables
- startTime = clock();
- threads = new thread[cores];
- progressArray = new Progress[cores];
- thread userInput = thread(userIoThread, cores);
- userInput.detach();
- //Initiate threads and other info
- uint64_t rem = amount % cores;
- uint64_t chg = (amount - rem) / cores;
- for (uint8_t i = 0; i < cores; i++) {
- uint64_t start = i * chg;
- uint64_t stop = (i + 1) * chg;
- progressArray[i] = Progress(start, stop);
- if (i >= cores - 1)
- stop += rem;
- threads[i] = thread(calcRange, start, stop, i, true);
- }
- for (uint8_t i = 0; i < cores; i++)
- threads[i].join();
- finished = true;
- //Delete allocated memory
- cout << "Deleting allocated memory!" << endl;
- delete[] threads;
- delete[] progressArray;
- //Print information
- cout << "The value that takes the most steps is: " << maxLen << endl;
- cout << "The number of steps it took was: " << maxStep << endl;
- uint64_t curTime = clock();
- float minutes = ((curTime - startTime) / 1000.0) / 60.0;
- cout << "And took " << minutes << "m" << endl;
- //Wait for user input
- cout << "Press Ctrl+C to continue" << endl;
- string dummy;
- cin >> dummy;
- //Exit
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment