Hollowfires

Untitled

Apr 29th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include "queue.cpp"
  4. #include <cmath>
  5. using namespace std;
  6. void SievesOfEratosthenes(int); //function prototype.
  7.  
  8. //computer all the primes up to some integer n, have this n be an integer you prompt to enter the user to enter.
  9.  
  10.  
  11. int main()
  12. {
  13.  
  14. int number = 0;
  15. do
  16. {
  17. cout << "Please enter an integer greater than 1: ";
  18. cin >> number;
  19. }
  20. //don't accept a number less than or equal to 1.
  21. while(number <=1);
  22.  
  23. SievesOfEratosthenes(number);
  24. //now do the cout. send it through a function.
  25.  
  26. return 0;
  27. }
  28.  
  29. void SievesOfEratosthenes(int n)
  30. {
  31. //create a queue for the integers. Enqueue this 2->n
  32. Queue<int> queueOfIntegers;
  33. Queue<int> queueOfPrimes;
  34.  
  35. //enqueue queueOfIntegers 2->n
  36. for (int i=2; i<=n;i++)
  37. {
  38. queueOfIntegers.enqueue(i);
  39. //cout << i << " ";
  40. }
  41. //queue of integers is made now. it will have 2-100 if you input 100 originally.
  42.  
  43.  
  44. do
  45. {
  46. //get the next prime number p by removing the first value of queueOfIntegers
  47.  
  48. int p = queueOfIntegers.dequeue();
  49. //add p to the Primes queue
  50. queueOfPrimes.enqueue(p);
  51.  
  52. cout << "queueOfPrimes has: ";
  53. queueOfPrimes.display();
  54.  
  55. Queue<int> newQueue;
  56. newQueue.enqueue(queueOfPrimes.front());
  57.  
  58. while(!queueOfIntegers.isEmpty())
  59. {
  60.  
  61. if((queueOfIntegers.front()%p) !=0)
  62. {
  63. cout<<"welcome to the queue " << queueOfIntegers.front() << " ";
  64. newQueue.enqueue(queueOfIntegers.dequeue());
  65. }
  66. else
  67. {
  68. cout<< "bye " <<queueOfIntegers.front() << " ";
  69. //if the integer is divisible, then it'll be dequeued.
  70. queueOfIntegers.dequeue();
  71. }
  72. //need to update the prime list and queueOfIntegers somehow and p....?
  73.  
  74.  
  75. p = queueOfPrimes.back();
  76. cout<< "value of p " << p << endl;
  77.  
  78. }
  79.  
  80. //queueOfIntegers=newQueue; //this alone makes it derp the fuck out.
  81. //need to add newQueue somehow.
  82. //p = queueOfPrimes.back();
  83. cout<< "value of p " << p << endl;
  84.  
  85. queueOfPrimes=newQueue;
  86.  
  87.  
  88. }while(queueOfPrimes.back()<sqrt(n));
  89. //do-while the last # of the primes list is less than number
  90.  
  91. //display the queueOfPrimes and go back to main now.
  92. queueOfPrimes.display();
  93.  
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment