重印2D阵列素数因子

时间:2014-10-03 02:06:53

标签: c++ multithreading concurrency prime-factoring

我正在用C ++编写并行素数分解程序。我设法得到了所有的线程,并找到了非常好的,但它是我似乎无法得到的最终结果。当用户输入多个数字以查找素数因子时,它会打印整个素数分解数组。我希望它只打印与唯一数字相关的素因子。

enter image description here

我想将其更改为“10的素数因子化”之后的行不会打印素数的整个向量。所有打印都发生在主功能的底部。非常具体,如果我输入两个10,输出应该是:

---期望的输出---

“10的主要因子分解是”

“2 5”

“10的主要因子分解是”

“2 5”

--- /期望的输出---

不要担心“有0个素数”部分。我知道如何解决这个问题

感谢任何和所有帮助!

#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();

}

2 个答案:

答案 0 :(得分:1)

这对评论来说太长了,所以我将此作为答案发布

你也可以试试这个

#include <iostream>

using namespace std;

long long Number;
int Prime[10000];

void Gen()
{
    Prime[0]=2;
    Prime[1]=3;
    bool IsPrime;
    long long Counter=2;
    for( int ii=4 ; Counter<10000 ; ii++ )
    {
        IsPrime=true;
        for( int jj=0 ; Prime[jj]<=sqrt(ii) ; jj++ )
        {
            if(ii%Prime[jj]==0)
            {
                IsPrime=false;
                break;
            }
        }
        if(IsPrime)
        {
            Prime[Counter]=ii;
            Counter++;
        }
    }
}
int main()
{
    int Factor[10000]={0};
    Gen();
    cout<<"Enter Number"<<endl;
    cin>>Number;
    Factorize :
    for( int ii=0 ; ii<10000 ; ii++ )
    {
        if(Number<Prime[ii])
        {
            break;
        }
        if(Number%Prime[ii]==0)
        {
            Number/=Prime[ii];
            Factor[ii]=1;
            if(Number==1)
            {
                break;
            }
            goto Factorize;         
        }
    }
    for( int ii=0 ; ii<10000 ; ii++ )
    {
        if(Factor[ii])
        {
            cout<<Prime[ii]<<" ";
        }
    }
}

嗯,我正在做的是我首先生成素数数组,然后我将给定的数字除以Prime数组的元素。如果Number可以被相应的素因子整除,那么我将因子数组中的索引标记为因子,然后我迭代因子数组,如果任何元素被标记为因子那么我&#39 ;打印它。

实际上,您可以根据需要调整数组中的元素数量。

答案 1 :(得分:0)

所以我明白了:

#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();

}