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
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.
we now need to write the function to compute the LCM. once we have the GCD of two numbers, we can find the LCM as follows 
Thanks.
pa LCM(p, q) = GCD(p
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
