running fast dynamic programming algorithm to get all the answers - c++

how i could run fast dynamic programming algorithm to get all the possible answers .
imagine we have 20 entries and it only shows 1 line of best answers , i want it to run all the way and show the others too, till all the entries are shows as a result, and no repetitions is allowed .
thank you so much. really appreciate it.
here is the code :
#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <utility>
using namespace std;
float W ,N; //N = olcu sayisi, W = profil boyu
vector<float> numbers; //stores the set of numbers
pair<float, multiset<float>> calc(float i, float j) //returns closest sum and best subset of the first i numbers for the target value j
{
static map<pair<float, float>, pair<float, multiset<float>>> dp; //stores results to avoid repeated calculations
pair<float, float> p(i, j); //pair for convenience
if(i == 0) //base case
{
return make_pair(0, multiset<float>(
{}));
}
auto findResult = dp.find(p);
if(findResult != dp.end()) //check if already calculated
{
return findResult->second;
}
auto temp1 = calc(i - 1, j); //compute result if not using number
if(numbers[i - 1] > j) //if current number is too big
{
return temp1;
}
auto temp2 = calc(i - 1, j - numbers[i - 1]); //compute result if using number
temp2.first += numbers[i - 1];
temp2.second.insert(numbers[i - 1]);
pair<float, multiset<float>> result;
if(temp1.first != temp2.first) //compare results and choose best
{
result = temp1.first > temp2.first ? temp1 : temp2;
}
else
{
result = temp1.second.size() < temp2.second.size() ? temp1 : temp2;
}
dp[p] = result;
return result;
}
int main()
{
cout << "sineklik sayisi: ";
cin >> N;
N = 2 * N;
cout << "Profil olcusu: ";
cin >> W;
numbers.reserve(N); //avoid extra reallocations
cout << "Olculeri giriniz: ";
for(int i = 0; i < N; i++) //input loop
{
float temp;
cin >> temp;
numbers.push_back(temp);
}
pair<float, multiset<float>> result = calc(N, W); //calculate
//output below
cout << "The best possible sum is " << result.first << " Left behind is " << W - result.first << ", obtained using the set of numbers {";
if(result.second.size() > 0)
{
cout << *result.second.begin();
for(auto i = ++result.second.begin(); i != result.second.end(); i++)
{
cout << ", " << *i;
}
}
cout << "}.\n";
}

Edit: This is one of my older answers. I wasn't as good back then, and now I know there is a much simpler, faster, and less memory-consuming solution to this problem. If we compute the DP bottom-up in a table that only stores the closest possible sum, we can reconstruct the subsets recursively later using the table values we computed.
A solution that outputs all sets of numbers with sum equal to the greatest possible sum not greater than the target value and containing the least possible number of numbers:
#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <utility>
using namespace std;
int N, W; //N = number of numbers, W = target sum
vector<int> numbers; //stores the set of numbers
pair<int, set<multiset<int>>> calc(int i, int j) //returns closest sum and best subset of the first i numbers for the target value j
{
static map<pair<int, int>, pair<int, set<multiset<int>>>> dp; //stores results to avoid repeated calculations
pair<int, int> p(i, j); //pair for convenience
if(i == 0) //base case
{
set<multiset<int>> temp;
temp.emplace();
return make_pair(0, temp);
}
auto findResult = dp.find(p);
if(findResult != dp.end()) //check if already calculated
{
return findResult->second;
}
auto temp1 = calc(i - 1, j); //compute result if not using number
if(numbers[i - 1] > j) //if current number is too big
{
return temp1;
}
pair<int, set<multiset<int>>> temp2 = calc(i - 1, j - numbers[i - 1]), newtemp2; //compute result if using number
newtemp2.first = temp2.first + numbers[i - 1];
for(const auto k : temp2.second)
{
multiset<int> temp = k;
temp.insert(numbers[i - 1]);
newtemp2.second.insert(temp);
}
pair<int, set<multiset<int>>> *result;
if(temp1.first != newtemp2.first) //compare results and choose best
{
result = temp1.first > newtemp2.first ? &temp1 : &newtemp2;
}
else if(temp1.second.begin()->size() != newtemp2.second.begin()->size())
{
result =
temp1.second.begin()->size() < newtemp2.second.begin()->size() ? &temp1 : &newtemp2;
}
else
{
temp1.second.insert(newtemp2.second.begin(), newtemp2.second.end());
result = &temp1;
}
dp.insert(make_pair(p, *result));
return *result;
}
int main()
{
cout << "Enter the number of numbers: ";
cin >> N;
cout << "Enter target sum: ";
cin >> W;
numbers.reserve(N); //avoid extra reallocations
cout << "Enter the numbers: ";
for(int i = 0; i < N; i++) //input loop
{
int temp;
cin >> temp;
numbers.push_back(temp);
}
pair<int, set<multiset<int>>> result = calc(N, W); //calculate
//output below
cout << "The best possible sum is " << result.first << ", which can be obtained using a set of "
<< result.second.begin()->size() << " numbers " << result.second.size()
<< " different ways:\n";
for(const auto &i : result.second)
{
cout << '{';
if(i.size() > 0)
{
cout << *i.begin();
for(auto j = ++i.begin(); j != i.end(); ++j)
{
cout << ", " << *j;
}
}
cout << "}\n";
}
}
This solution will not allow a number to appear in the output more than the number of times it appeared in the input. If you want to only allow a number to appear once in the output even if it appeared multiple times in the input, change the multiset numbersleft to a set.
#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <utility>
using namespace std;
int N, W; //N = number of numbers, W = target sum
vector<int> numbers; //stores the set of numbers
pair<int, set<multiset<int>>> calc(int i, int j) //returns closest sum and best subset of the first i numbers for the target value j
{
static map<pair<int, int>, pair<int, set<multiset<int>>>> dp; //stores results to avoid repeated calculations
pair<int, int> p(i, j); //pair for convenience
if(i == 0) //base case
{
set<multiset<int>> temp;
temp.emplace();
return make_pair(0, temp);
}
auto findResult = dp.find(p);
if(findResult != dp.end()) //check if already calculated
{
return findResult->second;
}
auto temp1 = calc(i - 1, j); //compute result if not using number
if(numbers[i - 1] > j) //if current number is too big
{
return temp1;
}
pair<int, set<multiset<int>>> temp2 = calc(i - 1, j - numbers[i - 1]), newtemp2; //compute result if using number
newtemp2.first = temp2.first + numbers[i - 1];
for(const auto k : temp2.second)
{
multiset<int> temp = k;
temp.insert(numbers[i - 1]);
newtemp2.second.insert(temp);
}
pair<int, set<multiset<int>>> *result;
if(temp1.first != newtemp2.first) //compare results and choose best
{
result = temp1.first > newtemp2.first ? &temp1 : &newtemp2;
}
else if(temp1.second.begin()->size() != newtemp2.second.begin()->size())
{
result =
temp1.second.begin()->size() < newtemp2.second.begin()->size() ? &temp1 : &newtemp2;
}
else
{
temp1.second.insert(newtemp2.second.begin(), newtemp2.second.end());
result = &temp1;
}
dp.insert(make_pair(p, *result));
return *result;
}
int main()
{
cout << "Enter the number of numbers: ";
cin >> N;
cout << "Enter target sum: ";
cin >> W;
numbers.reserve(N); //avoid extra reallocations
cout << "Enter the numbers: ";
for(int i = 0; i < N; i++) //input loop
{
int temp;
cin >> temp;
numbers.push_back(temp);
}
pair<int, set<multiset<int>>> result = calc(N, W); //calculate
//output below
cout << "The best possible sum is " << result.first << ", which can be obtained using sets of "
<< result.second.begin()->size() << " numbers:\n";
multiset<int> numbersleft;
numbersleft.insert(numbers.begin(), numbers.end());
for(const auto &i : result.second)
{
bool good = true;
for(const int &j : i)
{
if(numbersleft.find(j) == numbersleft.end())
{
good = false;
break;
}
}
if(good)
{
for(const int &j : i)
{
numbersleft.erase(j);
}
cout << '{';
if(i.size() > 0)
{
cout << *i.begin();
for(auto j = ++i.begin(); j != i.end(); ++j)
{
cout << ", " << *j;
}
}
cout << "}\n";
}
}
}

Related

Print prime factorization in exponential form in C++

So far I have this code. I'm trying to print prime factorization with exponents. For example, if my input is 20, the output should be 2^2, 5
#include <iostream>
#include <cmath>
using namespace std;
void get_divisors (int n);
bool prime( int n);
int main(int argc, char** argv) {
int n = 0 ;
cout << "Enter a number and press Enter: ";
cin >>n;
cout << " Number n is " << n << endl;
get_divisors(n);
cout << endl;
return 0;
}
void get_divisors(int n){
double sqrt_of_n = sqrt(n);
for (int i =2; i <= sqrt_of_n; ++i){
if (prime (i)){
if (n % i == 0){
cout << i << ", ";
get_divisors(n / i);
return;
}
}
}
cout << n;
}
bool prime (int n){
double sqrt_of_n = sqrt (n);
for (int i = 2; i <= sqrt_of_n; ++i){
if ( n % i == 0) return 0;
}
return 1;
}
I hope someone can help me with this.
You can use an std::unordered_map<int, int> to store two numbers (x and n for x^n). Basically, factorize the number normally by looping through prime numbers smaller than the number itself, dividing the number by the each prime as many times as possible, and recording each prime you divide by. Each time you divide by a prime number p, increment the counter at map[p].
I've put together a sample implementation, from some old code I had. It asks for a number and factorizes it, displaying everything in x^n.
#include <iostream>
#include <unordered_map>
#include <cmath>
bool isPrime(const int& x) {
if (x < 3 || x % 2 == 0) {
return x == 2;
} else {
for (int i = 3; i < (int) (std::pow(x, 0.5) + 2); i += 2) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
std::unordered_map<int, int> prime_factorize(const int &x) {
int currentX = abs(x);
if (isPrime(currentX) || currentX < 4) {
return {{currentX, 1}};
}
std::unordered_map<int, int> primeFactors = {};
while (currentX % 2 == 0) {
if (primeFactors.find(2) != primeFactors.end()) {
primeFactors[2]++;
} else {
primeFactors[2] = 1;
}
currentX /= 2;
}
for (int i = 3; i <= currentX; i += 2) {
if (isPrime(i)) {
while (currentX % i == 0) {
if (primeFactors.find(i) != primeFactors.end()) {
primeFactors[i]++;
} else {
primeFactors[i] = 1;
}
currentX /= i;
}
}
}
return primeFactors;
}
int main() {
int x;
std::cout << "Enter a number: ";
std::cin >> x;
auto factors = prime_factorize(x);
std::cout << x << " = ";
for (auto p : factors) {
std::cout << "(" << p.first << " ^ " << p.second << ")";
}
}
Sample output:
Enter a number: 1238
1238 = (619 ^ 1)(2 ^ 1)
To begin with, avoid using namespace std at the top of your program. Second, don't use function declarations when you can put your definitions before the use of those functions (but this may be a matter of preference).
When finding primes, I'd divide the number by 2, then by 3, and so on. I can also try with 4, but I'll never be able to divide by 4 if 2 was a divisor, so non primes are automatically skipped.
This is a possible solution:
#include <iostream>
int main(void)
{
int n = 3 * 5 * 5 * 262417;
bool first = true;
int i = 2;
int count = 0;
while (i > 1) {
if (n % i == 0) {
n /= i;
++count;
}
else {
if (count > 0) {
if (!first)
std::cout << ", ";
std::cout << i;
if (count > 1)
std::cout << "^" << count;
first = false;
count = 0;
}
i++;
if (i * i > n)
i = n;
}
}
std::cout << "\n";
return 0;
}
Note the i * i > n which is an alternative to the sqrt() you are using.

Square Root Code C++ without sqrt()

I have to create a code where the user inputs a number which is a perfect square, and I have to show its root. I've made this code, but I'm getting Segmentation Fault 11 , in this piece: int j = squareRootVector[i];
squareRoot.push_back(j);.
I can't change the code too much, so is there a way that I can do that?
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout <<
"Enter the number:\n";
int input;
int number = input;
int divider = 2;
vector<int> squareRootVector;
vector<int> squareRoot;
cin >> number;
for(int divider = 2; number > 1; divider++) {
while((number % divider) == 0) {
number /= divider;
cout << number << endl;
squareRootVector.push_back(divider);
}
}
for(int i = 0; i < squareRootVector.size(); i++) {
cout << squareRootVector[i] << " ";
/*******PROBLEM*******/
if(squareRootVector[i] == squareRootVector[i+1]) {
int j = squareRootVector[i];
squareRoot.push_back(j);
}
/*********************/
}
int root;
for (int i = 0; squareRoot.size(); i++) {
root = root * squareRoot[i];
}
cout << "Square Root of " << input << " is: " << root << endl;
return 0;
}
The behaviour on accessing squareRootVector[i+1] with i just one below size (which your loop constaint allows) is undefined.
Consider writing
for (std::size_t i = 1; i < squareRootVector.size(); i++) {
instead, and rebasing the for loop body accordingly. I've also slipped in a change of type for i.
Shortly, the problem is that the last cycle in the last "for":
for(int i = 0; i < squareRootVector.size(); i++)
has the following line in it:
squareRootVector[i] == squareRootVector[i+1];
This is an "out of limits" error: squareRootVector only has squareRootVector.size() elements (let's say n), and the elements are indexed from 0 to n-1.
squareRootVector[i+1] in the last cycle points one element after the last one of squareRootVector, which is undefined behavior.
Using vector::iterator is proper way.
for(vector<int>::iterator it = squareRootVector.begin(); it != squareRootVector.end(); ++it)
{
if( (it+1) == squareRootVector.end() )
{
//what to do if there's no next member???
break;
}
if( *it == *(it+1) )
{
squareRoot.push_back(*it);
}
}
Thanks for the answers, guys. I've ended up with this code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "Enter the number:\n";
int input = 0;
int number = 0;
cin >> input;
number = input;
int divider = 2;
vector<int> squareRootVector;
vector<int> squareRoot;
for(int divider = 2; number > 1; divider++) {
while((number % divider) == 0) {
number /= divider;
squareRootVector.push_back(divider);
}
}
int vectorSize = squareRootVector.size() - 1;
for(int i = 0; i < vectorSize; i++) {
if(squareRootVector[i] == squareRootVector[i+1]) {
int j = squareRootVector[i];
squareRoot.push_back(j);
}
}
int root = 1;
for (int i = 0; i < squareRoot.size(); i++) {
root = root * squareRoot[i];
}
cout << "Square Root of " << input << " is " << root << endl;
return 0;
}

Time limit exceed

The system said: my code was time limit exceed. Is there away to shortcut my code?
I'm using vector to save nails to graph.
Input: e,n
Output: checkcase=1 => Check u adjacent with i
checkcase=2 => find nails that around u
#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace std;
int main()
{
int e, n;
string u, i;
//using vectors
vector<string> graph;
//use list
list<string>listElementsInCase2;
cin >> e;
cin >> n;
//loop for e
for (long index = 0; index < e; index++)
{
cin>>u>> i;
//add u to graph
graph.push_back(u);
//add i to graph
graph.push_back(i);
}
//Option
int checkCase;
long index;
//loop for n
while(sizeof(n))
{
cin >> checkCase;
if (checkCase == 1)
{
cin >> u >> i;
for (index = 0; index < 2 * e; index += 2)
{ //Check u adjacent ? i
if ((graph.at(index) == u) && (graph.at(index + 1) == i))
{
cout << "TRUE" << endl;
break;
}
}
if (index == 2 * e)
cout << "FALSE" << endl;
}
//checkCase=2
if (checkCase == 2)
{
cin >> u;
for (long index = 0; index < 2 * e; index += 2)
{
if (graph.at(index) == u)
listElementsInCase2.push_back(graph.at(index + 1));
}
// return 0
if (listElementsInCase2.empty() == true)
cout << "0";
else
{
while (0 < listElementsInCase2.size())
{ //find nails that around u
cout << listElementsInCase2.front();
listElementsInCase2.pop_front();
cout << " ";
}
}
cout << endl;
}
n--;
}
}
//end
You seem to have an infinite while loop in your code.
Your statement while(sizeof(n)) will never stop repeating the loop since sizeof(n) always returns the byte size of the type integer, which is always a positive number and thus always evaluates as true.
A shortcut for your code would be to replace the while loop with a for or while loop that actually ends at some point. Also sizeof is probably not the function you want.
for(int i=0; i<n; i++){ might be what you are looking for.

C++ Vector Subscript Issues

I was making an app that calculates the mean, median, and range of any integers, but I ran into the issue: Vector subscript out of range. I've looked at some other posts about this, and still haven't been able to fix it.
Here's my code:
#include <iostream>
#include <Algorithm>
#include <Windows.h>
#include <vector>
using namespace std;
int main() {
//Variables
int sze;
int mraw = 0;
double mean;
double median;
double range;
int fullnum = 0;
int lastnum = 1;
vector<int> med;
cout << "How many numbers do you have? ";
cin >> sze;
int *arr = new int[sze];
for (int i = 0; i < sze; i++) {
med.push_back(arr[i]);
}
//Getting numbers
for (int i = 0; i < sze, i++;) {
system("cls");
cout << "Enter number #" << i + 1 << ": ";
cin >> arr[i];
}
//Mean
for (int i = 0; i < sze; i++){
fullnum += arr[i];
}
mean = fullnum / sze;
//Median
sort(med.begin(), med.end());
int mvs = sze;
while (med.size() >= 2) {
med.erase(med.begin());
med.erase(med.begin() + med.size() - 1);
mvs--;
}
if (mvs == 2) {
mraw = med[1] + med[2];
median = mraw / 2;
}
else {
median = mvs;
}
//Range
vector<int> rnge;
for (int i = 0; i < sze; i++) {
rnge.push_back(arr[i]);
lastnum++;
}
sort(rnge.begin(), rnge.end());
int bigsmall[2];
bigsmall[1] = rnge[1];
bigsmall[2] = rnge[lastnum];
range = bigsmall[2] - bigsmall[1];
//Outputs
cout << "Mean: " << mean << "\nMedian: " << median << "\nRange: " << range;
system("cls");
return 0;
}
You have what would be an off-by-one error if lastnum was initialized to 0.
When rnge is empty, presumably lastnum is 0. This means access rnge[lastnum] is in error, as rnge is empty.
Applying an inductive argument shows that lastnum is the count of number of elements, and not the index of the last element. Thus, rnge[lastnum] is always out of range.
In actuality, you have initialized lastnum to 1, so your bug is actually off-by-two.

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.
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();
}
This was too long for a comment so, I'm posting this as an answer
You could also try this
#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]<<" ";
}
}
}
Well, what I'm doing is I'm first generating array of primes, then I'm dividing given Number from elements of Prime array. If Number is divisible by respective prime factor then I'm marking it's index in factor array as a factor, Then I'm iterating over factor array, if any element is marked as factor then I'm printing it.
Actually, You can adjust number of elements in array as per your requirements.
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();
}