Advertisement
rinab333

CSCI 340 assign 3

Jul 16th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. //terina burr
  2. //section one
  3. //assignment three
  4. //due 9/21/15
  5.  
  6. #include <iostream>
  7. #include <set>
  8. #include <cstdlib>
  9. #include <iomanip>
  10.  
  11. using namespace std;
  12.  
  13. int ITEM_W = 5;
  14. int NO_ITEMS =10;
  15.  
  16. void sieve( set<int>& s, int n) ;
  17.  
  18. void print_primes( const set<int>& s) ;
  19.  
  20. int main() {
  21.     set<int> s;
  22.     sieve(s, 1000);
  23.     print_primes(s);
  24.     return 0;
  25. }
  26.  
  27. /***************************************************************
  28. Function: sieve
  29.  
  30. Use:      it applies the sieve of eratosthesis algorithm to take out all the numbers that are not prime
  31.  
  32. Arguments:  1. s: the set that you are going to go through          
  33.         2. n:  the last number in the set
  34.  
  35. Returns:   nothing
  36. ***************************************************************/
  37.  
  38.  
  39. void sieve(set <int> &s, int n)
  40. {
  41.     int m, i;
  42.     s.erase(s.begin(), s.end());
  43. //erases everything that is in the set
  44.     for(m =2; m<n; m++)
  45.         s.insert(m);
  46. //inserts m if it is less than  the limit
  47.     for(m=2; m*m<=n; m++)
  48. //finds all number using the sieve of erat
  49. //it looks at the numbers from 2 to m*m<=2
  50.         if(s.find(m) !=s.end())
  51.         {  
  52. //checks to see if m is in the set
  53.             i=2*m;
  54. //if not then it removes multiples of m starting with 2*m
  55.             while (i<=n)
  56.             {
  57.                 s.erase(i);
  58.                 i+=m;
  59. //i is multiples of m
  60.             }
  61.         }
  62. }
  63.  
  64. /***************************************************************
  65. Function: print_primes
  66.  
  67. Use:      prints all items in the set with NO_ITEMS per line and ITEM_W on how far apart they are
  68.  
  69. Arguments:  1. s: the set that you are going to go through          
  70. Returns:   nothing
  71. ***************************************************************/
  72.  
  73.  
  74. void print_primes(const set<int> &s)
  75. {
  76.     set<int>::iterator iter;
  77.     int count =0;
  78.     iter = s.begin();
  79.     cout << "There are " << s.size() << " prime numbers: " <<endl;
  80.     while(iter !=s.end())
  81.     {
  82.         count++;
  83.         cout << setw(ITEM_W) << *iter;
  84. //prints the numbers at a width that is four spaces apart
  85.             if(count % NO_ITEMS == 0)
  86.             {
  87.                 cout <<endl;
  88.             }
  89. //if there is more than 16 numbers on a line then they start a new line
  90.         iter++;
  91.     }
  92. cout<<endl;
  93.  
  94.  
  95.  
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement