Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ http://www.codesend.com/view/22a5c2ea4899bf0018a06bc36a4ed28b/ This is about

ID: 3591667 • Letter: C

Question

C++

http://www.codesend.com/view/22a5c2ea4899bf0018a06bc36a4ed28b/

This is about computing prime factorization of two numbers and then finding their GCD and LCD.
Please follow the skeleton given. Everything inside //TODO I have attempted. However I'm still getting errors

In getFactors function:
we want to compute the prime factors of a number in the function getFactors. The routine we will use is relatively simple. As a quick summary, we will start at the number 2 and see how many times 2 divides n. Every time 2 divides n evenly, we add it to our list of factors. When 2 does not divide n anymore, we then start the same process but for 3, and then for 4, then for 5 , etc. Here is an example run through of the algorithm: let n = 180; let factors = {} 180 % 2 = 0 factors = {2} and n = 90 90 % 2 = 0 factors = {2, 2} and n = 45

We start with an int c ounter, we can call it c and we initialize it to 2 i. we will use this as the temporary factor
set up a while loop while n is not equal to 1
inside of the while loop from step b, we want to see if our temporary factor divides into n evenly, that is, we want to see if there is any remainder after dividing n by c. we also want to do this until c no long divides n
inside of the while loop from step c, we want to divide n by c and save the quotient in n. we also want to push_back c into our factors vector
increment c

Now we need to implement the GCD and LCM routines.  C++ gives us a function called set_intersection which takes in two (sorted) sets of numbers and provides us with the intersection of those two sets. first, create a new vector of ints named intersection.  make the call to set_intersection, passing in the beginning and end of pFactors, then the beginning and end of qFactors, then the special construction .

now that we have the intersection of the prime factors, we need to just multiply them all together. the function accumulate much like set_intersection takes in ranges to vector-like objects, accept we have to tell it how to accumulate the values.

Thanks.


pa LCM(p, q) = GCD(p

Explanation / Answer

/*
The changes made in the code have been commented.
The getFactors function works fine now.
Hope this helps you. Kindly rate the answer.

Please note that the code is compatible only with c++11 and above.
*/

// main.cpp
//

#include <algorithm> // set_intersection
#include <iostream>
#include <numeric> // accumulate
#include <vector>

using namespace std;

vector<int> getFactors(int n)
{
vector<int> factors = {1};

// TODO:
// put the code that computes the factors here!
  
int c = 2;
  
while (n != 1)
{
while (n % c == 0)
{
n = n / c;
factors.push_back(c);
// CHANGE: the variable name was incorrect here
}

++c;
}

return factors;
}

int gcd(int p, int q)
{
vector<int> pFactors = getFactors(p);
vector<int> qFactors = getFactors(q);

int gcd = 0;
  
// TODO:
// put the code that computes the GCD here!
vector<int> intersection;
// CHANGE: Initializing with int n is incorrect
// vector<int> intersection(n) would have been correct if n was defined in this context
// In any case, it is not needed here
set_intersection(begin(pFactors), end(pFactors), begin(qFactors), end(qFactors), back_inserter(intersection));
gcd = accumulate(begin(intersection), end(intersection), 1, multiplies<int>());
return gcd;
}

int lcm(int p, int q)
{
int lcm = 0;

// TODO:
// put the code that computes the LCM here!
lcm = (p*q)/(gcd(p,q));
// CHANGE: A semicolon was missing here
return lcm;
}

void printFactors(const vector<int> &factors)
{
cout << "< ";
for (auto factor : factors)
{
cout << factor << " ";
}
cout << ">" << endl;
}

int main()
{
int p = 0;
cout << "Enter a positive integer for p:" << endl;
cin >> p;

int q = 0;
cout << "Enter a positive integer for q:" << endl;
cin >> q;

if (p < 1 || q < 1)
{
cerr << "ERROR: both p and q need to be positive integers! Received: " << p << ", " << q << endl;
return 1;
}

vector<int> pFactors = getFactors(p);
vector<int> qFactors = getFactors(q);

printFactors(pFactors);
printFactors(qFactors);

cout << "GCD = " << gcd(p, q) << endl;
cout << "LCM = " << lcm(p, q) << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote