2014-10-03 26 views
0

我正在用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(); 

} 

回答

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

} 
+0

你發現了什麼?你能解釋一下嗎? – 0x499602D2 2014-10-03 02:38:27

+0

對不起。我改變的是for循環,其中包含cout「來自原始代碼的打印輸出語句的主要因式分解」。我改變它不做雙打印。所以邏輯是因爲矢量結束只是一個2D矢量,我會傳遞一個名爲'temp'的臨時矢量,幷包含從GetPrimeFactors()收到的所有素數。這總是按順序打印。 – booky99 2014-10-03 19:15:32

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可以被各個素數因子整除,那麼我將它作爲因子數組中的索引作爲因子,然後我遍歷因子數組,如果有任何元素被標記爲因子,那麼我將它打印出來。

實際上,您可以根據需要調整陣列中元素的數量。

+0

我還沒有機會實現這個想法,但我很喜歡它的外觀。生成陣列時遇到了很多問題,因爲會發生什麼情況是控制檯中的「222222222」的無限打印。這種風格看起來很乾淨。 – booky99 2014-10-03 19:16:39