944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 489
  • C++ RSS
Nov 5th, 2009
0

Help Translate this Simple? Algorithm

Expand Post »
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:
Quote ...
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:
Quote ...
- 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:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Nov 5th, 2009
0
Re: Help Translate this Simple? Algorithm
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
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Nov 5th, 2009
0
Re: Help Translate this Simple? Algorithm
Click to Expand / Collapse  Quote originally posted by daviddoria ...
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.

Quote ...
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
Quote ...
2 with multiplicity 2
3 with multiplicity 2
5 with multiplicity 2
7 with multiplicity .... crash
- 8
Quote ...
2 with multiplicity 3 = works
- 135
Quote ...
6 with multiplicity ... crash

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

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Nov 5th, 2009
0
Re: Help Translate this Simple? Algorithm
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).

C++ Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Nov 6th, 2009
0
Re: Help Translate this Simple? Algorithm
Let me try to explain further of the algorithm below :
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Viewing a buffer when using std::string advice.
Next Thread in C++ Forum Timeline: Class Fails at Runtime





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC