Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include "queue.cpp"
- #include <cmath>
- using namespace std;
- void SievesOfEratosthenes(int); //function prototype.
- //computer all the primes up to some integer n, have this n be an integer you prompt to enter the user to enter.
- int main()
- {
- int number = 0;
- do
- {
- cout << "Please enter an integer greater than 1: ";
- cin >> number;
- }
- //don't accept a number less than or equal to 1.
- while(number <=1);
- SievesOfEratosthenes(number);
- //now do the cout. send it through a function.
- return 0;
- }
- void SievesOfEratosthenes(int n)
- {
- //create a queue for the integers. Enqueue this 2->n
- Queue<int> queueOfIntegers;
- Queue<int> queueOfPrimes;
- //enqueue queueOfIntegers 2->n
- for (int i=2; i<=n;i++)
- {
- queueOfIntegers.enqueue(i);
- //cout << i << " ";
- }
- //queue of integers is made now. it will have 2-100 if you input 100 originally.
- do
- {
- //get the next prime number p by removing the first value of queueOfIntegers
- int p = queueOfIntegers.dequeue();
- //add p to the Primes queue
- queueOfPrimes.enqueue(p);
- cout << "queueOfPrimes has: ";
- queueOfPrimes.display();
- Queue<int> newQueue;
- newQueue.enqueue(queueOfPrimes.front());
- while(!queueOfIntegers.isEmpty())
- {
- if((queueOfIntegers.front()%p) !=0)
- {
- cout<<"welcome to the queue " << queueOfIntegers.front() << " ";
- newQueue.enqueue(queueOfIntegers.dequeue());
- }
- else
- {
- cout<< "bye " <<queueOfIntegers.front() << " ";
- //if the integer is divisible, then it'll be dequeued.
- queueOfIntegers.dequeue();
- }
- //need to update the prime list and queueOfIntegers somehow and p....?
- p = queueOfPrimes.back();
- cout<< "value of p " << p << endl;
- }
- //queueOfIntegers=newQueue; //this alone makes it derp the fuck out.
- //need to add newQueue somehow.
- //p = queueOfPrimes.back();
- cout<< "value of p " << p << endl;
- queueOfPrimes=newQueue;
- }while(queueOfPrimes.back()<sqrt(n));
- //do-while the last # of the primes list is less than number
- //display the queueOfPrimes and go back to main now.
- queueOfPrimes.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment