Hello
HELP :( Ok I have an assignment due in 5 days. I have done everything except for one function. This function finds the smallests factors of a positive int & it multiplicities, Have I lost you yet, dont worry because I've been lost for a week now with this function :P
Heres the funny part... we are given the algorithm!! But I cannot for the life of me understand it. I have rewritten, written, pulled my hair out for days trying to get this function to work.
I cannot seem to get this function to work. Can you take a look at my short function below & see where my algorithm is wrong?
Algorithm we have been supplied with:
An outline of the above algorithm would look something like:
// positive integer I is given
P = 1
while (I > 1)
{
P = P + 1
if I mod P != 0 go back to previous statement
// now P is the smallest prime factor of I
display P
mult = 1
exactly divide I by P as often as is possible,
assigning to I the result of each exact division,
and incrementing mult with each exact division
display mult, end line
}
An example of what the function is meant to do, if you are confused like me:
- We input a large integer: 25852
- The function void factor will find the factors and multiplicities
- So the answer is... 2*2*23*281 = 25852Please enter a big integer I: 25852
The number entered was 25852.
The prime factors of I are:
2 with multiplicity 2
23 with multiplicity 1
281 with multiplicity 1
Press any key to continue . . .- As you can see (2*2)+(23*1)+(281*1) = 25852
My code which doesn't work:
void factor(int I) {
int P = 1;
int mult;
while (I > 1) {
P = P+1;
I = I/P;
while (I%P != 0) {
P = P+1;
I = I/P;
}
cout << P << " with multiplicity ";
mult = 1;
while (I%P == 0) {
I = I/P;
mult++;
}
cout << mult << endl;
}
}
This is one of the functions I have written, if you want to see others that I have written to try to get this algorithm to work then I post them but I dont want this post any longer right now