Help Translate this Simple? Algorithm

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Help Translate this Simple? Algorithm

 
0
  #1
Nov 5th, 2009
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

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 = 25852

Please 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:
  1. void factor(int I) {
  2.  
  3. int P = 1;
  4. int mult;
  5.  
  6. while (I > 1) {
  7.  
  8. P = P+1;
  9. I = I/P;
  10.  
  11. while (I%P != 0) {
  12. P = P+1;
  13. I = I/P;
  14. }
  15.  
  16. cout << P << " with multiplicity ";
  17.  
  18. mult = 1;
  19.  
  20. while (I%P == 0) {
  21. I = I/P;
  22. mult++;
  23. }
  24.  
  25. cout << mult << endl;
  26. }
  27.  
  28. }

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
Last edited by gretty; Nov 5th, 2009 at 7:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster
 
0
  #2
Nov 5th, 2009
You need to give us more of a feeling that you have really tried. Did your code produce errors? Or incorrect results? Why don't you try some simple cases like the number '4'. You should expect to see factors of 2 and 2, right? Small examples like these will help you debug the code, then you can try large numbers.

Dave
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster
 
0
  #3
Nov 5th, 2009
Originally Posted by daviddoria View Post
You need to give us more of a feeling that you have really tried. Did your code produce errors? Or incorrect results? Why don't you try some simple cases like the number '4'. You should expect to see factors of 2 and 2, right? Small examples like these will help you debug the code, then you can try large numbers.

Dave
Hey listen I appreciate you looking at my post but dot say I need to prove to you I have tried, I explained that I have been racking my brain for days with numurous different functions & many many wasted hours.

Why don't you try some simple cases like the number '4'.
OFCOURSE I have tried my function, simple & hard input alike, I wouldn't have spent days rewritting testing, rewritting without testing!?

This is what my code produces for input:
- 25852
2 with multiplicity 2
3 with multiplicity 2
5 with multiplicity 2
7 with multiplicity .... crash
- 8
2 with multiplicity 3 = works
- 135
6 with multiplicity ... crash

You need to give us more of a feeling that you have really tried.
A little bit of evidence...

  1. void factorise(bigInt I) {
  2.  
  3. bigInt one(1); // used to increment big ints
  4. bigInt zero; // find if a bigInt equals zero
  5. bigInt result;
  6. bigInt temp(I);
  7. bigInt P(1);
  8. bigInt R(1);
  9. bigInt mult;
  10.  
  11. while (!I.lessOrEqual(one)) {
  12.  
  13. P = P.add(one);
  14. temp = I;
  15.  
  16. temp.divide(P,temp,R);
  17.  
  18. if (!R.isEqual(zero)) { // MAY NEED TO CHANGE TO IF STATEMENT
  19. temp.divide(P,temp,R);
  20. P = P.add(one);
  21. }
  22.  
  23. // Now P is the smallest prime factors of I
  24. P.writeBigInt(); // OR I.writeBigInt();
  25. cout << " with multiplicity ";
  26.  
  27. mult = one;
  28.  
  29. I.divide(P,temp,R);
  30.  
  31. //while (R.isEqual(zero)) {
  32. while (!I.lessOrEqual(P)) { // && !R.isEqual(zero)) {
  33. I.divide(P,I,R);
  34. mult = mult.add(one);
  35. }
  36.  
  37. mult.writeBigInt();
  38. cout << endl;
  39.  
  40. }
  41.  
  42. }
  43.  
  44. void fact(bigInt I) {
  45.  
  46. bigInt one(1); // used to increment big ints
  47. bigInt zero; // find if a bigInt equals zero
  48. bigInt result;
  49. bigInt temp(I);
  50. bigInt P(1);
  51. bigInt R(1);
  52. bigInt mult(1);
  53.  
  54. while (!I.lessOrEqual(one)) {
  55. temp = I;
  56. while (one.lessThan(temp)) {
  57.  
  58.  
  59. temp.divide(P,temp,R);
  60. P = P.add(one);
  61. if (R.isEqual(zero) && one.lessThan(temp)) {
  62. break;
  63. }
  64. }
  65.  
  66. P.writeBigInt(); // OR I.writeBigInt();
  67. cout << " with multiplicity ";
  68.  
  69. mult = one;
  70.  
  71. while (!I.lessOrEqual(P) && !R.isEqual(zero)) {
  72. I.divide(P,I,R);
  73. mult = mult.add(one);
  74. }
  75.  
  76. mult.writeBigInt();
  77. cout << endl;
  78. }
  79. }
  80.  
  81. void factor(int I) {
  82.  
  83. int P = 1;
  84. int mult;
  85.  
  86. while (I > 1) {
  87.  
  88. P = P+1;
  89. I = I/P;
  90.  
  91. while (I%P != 0) {
  92. P = P+1;
  93. I = I/P;
  94. }
  95.  
  96. cout << P << " with multiplicity ";
  97.  
  98. mult = 1;
  99.  
  100. while (I%P == 0) {
  101. I = I/P;
  102. mult++;
  103. }
  104.  
  105. cout << mult << endl;
  106. }
  107.  
  108. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster
 
0
  #4
Nov 5th, 2009
From that algorithm you gave I don't understand how you are supposed to get more than one factor?

Maybe this will be a good starting point for you - it just gets the smallest factor (besides 1) and multiplies it by successive integers until it gets to the "right" number (where P * mult = I).

  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. //an example input
  6. int I = 40;
  7.  
  8. //find the smallest P that divides I evenly
  9. int P = 2;
  10.  
  11. while(I % P != 0)
  12. {
  13. P++;
  14. }
  15.  
  16. //output the smallest P that was found
  17. std::cout << P << std::endl;
  18.  
  19. //show what P multiplied by everything up until the correct amount
  20. int mult = 1;
  21. while(mult * P <= I)
  22. {
  23. std::cout << mult << " * " << P << " = " << mult*P <<std::endl;
  24. mult++;
  25. }
  26.  
  27. return 0;
  28. }

Good luck.
Dave
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,408
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 181
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #5
Nov 6th, 2009
Let me try to explain further of the algorithm below :
  1. An outline of the above algorithm would look something like:
  2.  
  3. // positive integer I is given
  4. P = 1 //determines when a even factor of I is found
  5. while (I > 1) //I is you input number
  6. {
  7. P = P + 1// p is now 2
  8. if I mod P != 0 go back to previous statement //if I mod p == 1 then increment P until a factor is found
  9.  
  10. // now P is the smallest prime factor of I //now you found a factor for I
  11. display P //show the factor
  12. mult = 1
  13. exactly divide I by P as often as is possible, //keep diving I by the even factor until you get a decimal division
  14. assigning to I the result of each exact division, //now you need to find another factor of that number, so start repeating from the start
  15.  
  16. and incrementing mult with each exact division
  17. display mult, end line
  18. }
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC