Search code examples
c++multithreadingconcurrencyprime-factoring

Reprinting 2D array prime factors


I am writing a parallel prime factorization program in C++. I managed to get all of the threading and finding out the prime pretty well but its the very end that I can't seem to get. When the user enters more than one number to find the prime factor of, it prints the entire array of prime factorization. I want it to only print the prime factors related to a unique number.

enter image description here

I would like to change it to where the line after "The prime factorization of 10 is" doesn't print the entire vector of prime numbers. All of the printing occurs towards the bottom of the main function. To be very specific, if I were to type in two 10's, the output should be:

---desired output---

"The prime factorization of 10 is"

"2 5"

"The prime factorization of 10 is"

"2 5"

---/desired output---

do not worry about the "there are: 0 prime numbers" part. I know how to fix that already

Any and all help is appreciated!

#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <mutex>
#include <list>
#include <algorithm>

using namespace std;
using namespace std::chrono;

int userInput;                    // This number is used to help store the user input
vector<long long> vec(0);         // A vector storing all of the information
int numPrimes;                    // Used to count how many prime numbers there are
bool PRINT = false;               // lets me decide if I want to print everything for debugging purposes
int arraySize;
vector<thread> threads;
vector<vector<long long> > ending;

void getUserInput()
{
    //while the user has not entered 0, collect the numbers.
    cout << "Please enter a number for prime factorization. Enter 0 to quit" << endl;

    do
    {
        cin >> userInput;

        if (userInput != 0)
        {
            vec.push_back(userInput);
            arraySize++;
        }

    } while (userInput != 0);
}

vector<long long> primeFactors(long long n)
{
    vector<long long> temp;
    while (n % 2 == 0)
    {
        temp.push_back(n);
        numPrimes++;
        n = n / 2;
    }

    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n%i == 0)
        {
            temp.push_back(n);
            numPrimes++;
            n = n / i;
        }
    }

    if (n > 2)
    {
        temp.push_back(n);
        numPrimes++;
    }

    return temp;
}

void format()
{
    cout << endl;
}

bool isPrime(long long number){

    if (number < 2) return false;
    if (number == 2) return true;
    if (number % 2 == 0) return false;
    for (int i = 3; (i*i) <= number; i += 2){
        if (number % i == 0) return false;
    }
    return true;

}

vector<long long> GetPrimeFactors(long long num)
{
    vector<long long> v;
    for (int i = 2; i <= num; i++)
    {
        while (num % i == 0)
        {
            num /= i;
            v.push_back(i);
        }
    }
    return v;
}

int main()
{
    // how to find out how many cores are available.
    getUserInput();

    high_resolution_clock::time_point t1 = high_resolution_clock::now();

    // vector container stores threads

    format();

    for (int i = 0; i < arraySize; ++i)
    {
        vector<long long> temp;

        threads.push_back(thread([&] 
        { 
            ending.push_back(GetPrimeFactors(vec.at(i)));
        }));
    }

    // allow all of the threads to join
    for (auto& th : threads)
    {
        th.join();
    }

    for (int i = 0; i < arraySize; ++i)
    {
        cout << "The prime factorization of " << vec.at(i) << " is \n" << endl;
        for (int m = 0; m < ending.size(); m++)
        {
            vector<long long> v = ending[m];
            for (int k = 0; k < v.size(); k++)
            {
                cout << v.at(k) << " ";

            }

        }
        cout << endl;
    }



    format();

    cout << "There are: " << numPrimes << " prime numbers" << endl;

    //time
    high_resolution_clock::time_point t2 = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(t2 - t1).count();

    format();

    cout << "Time in seconds: " << (duration / 1000000.0) << endl;

    format();

}

Solution

  • So I figured it out:

    #include <iostream>
    #include <vector>
    #include <chrono>
    #include <thread>
    
    using namespace std;
    using namespace std::chrono;
    
    int userInput;                    // This number is used to help store the user input
    vector<long long> vec(0);         // A vector storing all of the information
    int numPrimes;                    // Used to count how many prime numbers there are
    int arraySize;
    vector<thread> threads;
    vector<vector<long long> > ending;
    
    void getUserInput()
    {
        //while the user has not entered 0, collect the numbers.
        cout << "Please enter a number for prime factorization. Enter 0 to quit" << endl;
    
        do
        {
            cin >> userInput;
    
            if (userInput != 0)
            {
                vec.push_back(userInput);
                arraySize++;
            }
    
        } while (userInput != 0);
    }
    
    void format()
    {
        cout << endl;
    }
    
    bool isPrime(long long number){
    
        if (number < 2) return false;
        if (number == 2) return true;
        if (number % 2 == 0) return false;
        for (int i = 3; (i*i) <= number; i += 2){
            if (number % i == 0) return false;
        }
        return true;
    
    }
    
    vector<long long> GetPrimeFactors(long long num)
    {
        vector<long long> v;
        for (int i = 2; i <= num; i++)
        {
            while (num % i == 0)
            {
                num /= i;
                v.push_back(i);
                numPrimes++;
            }
        }
        return v;
    }
    
    int main()
    {
        // how to find out how many cores are available.
        getUserInput();
    
        high_resolution_clock::time_point t1 = high_resolution_clock::now();
    
        // vector container stores threads
    
        format();
    
        for (int i = 0; i < arraySize; ++i)
        {
            vector<long long> temp;
    
            threads.push_back(thread([&] 
            { 
                ending.push_back(GetPrimeFactors(vec.at(i)));
            }));
        }
    
        // allow all of the threads to join
        for (auto& th : threads)
        {
            th.join();
        }
    
        for (int i = 0; i < arraySize; ++i)
        {
            cout << "The prime factorization of " << vec.at(i) << " is \n" << endl; 
            vector<long long> temp = ending[i];
    
            for (int m = 0; m < temp.size(); m++)
            {
                cout << temp.at(m) << " ";  
            }
            cout << endl;
        }
    
    
    
        format();
    
        cout << "There are: " << numPrimes << " prime numbers" << endl;
    
        //time
        high_resolution_clock::time_point t2 = high_resolution_clock::now();
        auto duration = duration_cast<microseconds>(t2 - t1).count();
    
        format();
    
        cout << "Time in seconds: " << (duration / 1000000.0) << endl;
    
        format();
    
    }