944,126 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 11230
  • C++ RSS
0

Prime Factorization

by on Aug 21st, 2005
The program uses an implementation of the Sieve of Eratosthenes to generate a small list of prime numbers which is subsequently used for direct comparison to find the prime factors for any whole number up to and including 478939.
This limitation is due to the primitive method used to generate prime numbers, but it is adequate for doing homework problems for adding fractions etc..
C++ Code Snippet (Toggle Plain Text)
  1. // Verified with Dev C++ 4.9.9.2 wx beta 6.7
  2. // Uses an implementation of the Sieve of Eratosthenes
  3. // to generate a small list of prime numbers
  4. // which is subsequently used for direct comparison
  5. // to find the prime factors for any number up to 478939
  6. // This limitation is due to the primitive method
  7. // used to generate prime numbers, but it is adequate
  8. // for doing homework problems for adding fractions etc..
  9.  
  10. #include <cstdio>
  11. #include <cstdlib>
  12. #include <iostream>
  13. using std::cout;
  14. using std::cin;
  15. using std::endl;
  16.  
  17. // prototype declarations
  18. void ClearMultiple(unsigned long nElimina, unsigned long integerArray[], unsigned long sizeOfloatArray);
  19.  
  20. int main()
  21. {
  22. // Enter the number to find prime components
  23. unsigned long nLimit=0;
  24. bool bFlag=false;
  25.  
  26. while (bFlag == false)
  27. {
  28. cout << "Calculate Prime Factors for what number ? ";
  29. cin >> nLimit;
  30. if ((nLimit > 1)&&(nLimit<=478939)) {bFlag = true;}
  31. }
  32. cout << "\n\n";
  33.  
  34. // Creates an array of all numbers less than nLimit
  35. // then uses an implementation of the Sieve of Eratosthenes
  36. // to eliminate all repeating multiples
  37. unsigned long inputValues[nLimit];
  38. unsigned long nLoop=0;
  39.  
  40. for(nLoop = 1; nLoop < nLimit; nLoop++)
  41. {
  42. inputValues[nLoop] = nLoop;
  43. }
  44.  
  45. for(nLoop = 2; nLoop < nLimit; nLoop++)
  46. {
  47. // Find the next number to eliminate
  48. if (inputValues[nLoop] == nLoop) // If Loop = Loop then ClearMultiple
  49. {
  50. ClearMultiple(nLoop, inputValues, nLimit);
  51. } // ... if not continue to the next
  52. }
  53.  
  54. // After the the Sieve of Eratosthenes is finished
  55. // keep only the numbers remaining i.e. Prime numbers
  56.  
  57. //This part counts how many numbers were eliminated
  58. unsigned long nAccumalator=0;
  59. for(nLoop = 2; nLoop < nLimit; nLoop++)
  60. {
  61. //if inputValues[Loop] = 0 then Accumalator=Accumalator+1
  62. if (inputValues[nLoop] == 0)
  63. {
  64. nAccumalator++;
  65. }
  66. }
  67.  
  68.  
  69. // Qty of prime numbers = nLimit - Accumalator
  70. unsigned long nQta;
  71. nQta = nLimit - nAccumalator;
  72.  
  73. //Define a new array to keep only the remaining prime numbers
  74. unsigned long nCounter=0;
  75. unsigned long Arrayprimi[nQta];
  76.  
  77. for(nLoop = 2; nLoop < nLimit; nLoop++)
  78. {
  79. //if InputValue[Loop]= Loop then Accumalator=Accumalator+1
  80. // ArrayPrimi[Accumalator]=Loop
  81. if (inputValues[nLoop] == nLoop)
  82. {
  83. nCounter++;
  84. Arrayprimi[nCounter] = nLoop;
  85. }
  86. }
  87.  
  88.  
  89. // Test the Number nLimit against the primes we generated
  90. int QtyPrimes=0;
  91. double dDivideTest=1;
  92.  
  93. // first count how many primes are divisable
  94. // to determine if the number is prime or composite
  95. for(nLoop = 1; nLoop < nQta-1; nLoop++)
  96. {
  97. dDivideTest=(int(nLimit/Arrayprimi[nLoop])* Arrayprimi[nLoop])-nLimit;
  98. if(dDivideTest==0)
  99. {
  100. QtyPrimes++;
  101. }
  102. }
  103.  
  104. // if it is a prime number then print and quit
  105. if(QtyPrimes==0)
  106. {
  107. cout << nLimit << " is a prime number \n" << endl;
  108.  
  109. // wait until user is ready before terminating program
  110. // to allow the user to see the program results
  111. system("PAUSE");
  112. return 0;
  113. }
  114.  
  115. // Now print the divisable primes
  116.  
  117. //if it a composite number print the factors and then print their usage
  118. cout << nLimit << " is divisable by the following prime numbers \n" << endl;
  119.  
  120. //this part determines the factors by testing for even division
  121. int nPrimes[QtyPrimes];
  122. QtyPrimes=0;
  123. for(nLoop = 1; nLoop < nQta-1; nLoop++)
  124. {
  125. dDivideTest=(int(nLimit/Arrayprimi[nLoop])* Arrayprimi[nLoop])-nLimit;
  126. if(dDivideTest==0)
  127. {
  128. QtyPrimes++;
  129. nPrimes[QtyPrimes]=Arrayprimi[nLoop];
  130. cout << " " << Arrayprimi[nLoop] << endl;
  131. }
  132. }
  133.  
  134. cout << "\n" << endl;
  135.  
  136. //this part counts and prints how many times each prime factor is used.
  137. int Qty[QtyPrimes];
  138. int nTemp=nLimit;
  139. int nCount=0;
  140.  
  141. cout << nLimit << " = ";
  142. for (nLoop=1; nLoop <= QtyPrimes; nLoop++)
  143. {
  144. Qty[nLoop]=0;
  145. dDivideTest=0;
  146. while(dDivideTest==0)
  147. {
  148. dDivideTest=(int(nTemp/nPrimes[nLoop])* nPrimes[nLoop])-nTemp;
  149. if (dDivideTest==0)
  150. {
  151. nTemp=nTemp/nPrimes[nLoop];
  152. Qty[nLoop]++;
  153. }
  154. }
  155. //print the result for each prime
  156. for (nCount=1;nCount <Qty[nLoop];nCount++)
  157. {
  158. cout << nPrimes[nLoop] << "*";
  159. }
  160. //print the prime one more time
  161. // and if there are more primes print another *
  162. cout << nPrimes[nLoop];
  163. if (QtyPrimes>nLoop)
  164. {
  165. cout << "*";
  166. }
  167. }
  168.  
  169. cout << "\n\n" << endl;
  170.  
  171. // wait until user is ready before terminating program
  172. // to allow the user to see the program results
  173. system("PAUSE");
  174. return 0;
  175. }
  176.  
  177.  
  178. // Function ClearMultiple
  179. // eliminates all multiples from the Array
  180. void ClearMultiple(unsigned long nElimina, unsigned long integerArray[], unsigned long nLimit)
  181. {
  182. unsigned long nA;
  183. for (nA= 2 * nElimina ; nA < nLimit; nA = nA + nElimina)
  184. {
  185. integerArray[nA]=0;
  186. }
  187. return;
  188. }
Comments on this Code Snippet
Jan 17th, 2010
0

Re: Prime Factorization

It might be easier to use the following code:

C++ Syntax (Toggle Plain Text)
  1. int q(int n,int*a,int i=2){*a=i;return n>1?n%i?q(n,a,i+1):1+q(n/i,a+1,i):0;}

it takes 2 arguments on invocation, the first being the number to factorize and the second being en array where all prime factors can be stored. This array should be big enough to hold all possible prime factors. It returns the number q of prime factors put into the given array.

Given the sheer complexity of the above code, my line of code will probably run faster. It is probably limited to the maximal stack size and it will recurse O(n+q) levels deep.
Newbie Poster
GeniusDex is offline Offline
1 posts
since Jan 2010
Message:
Previous Thread in C++ Forum Timeline: Member function pointers (AGAIN!)
Next Thread in C++ Forum Timeline: Help with plplot





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


Follow us on Twitter


© 2011 DaniWeb® LLC