Homework Solution: CSE2383 Programming Assignment 1, version 1.2 Page 1…

    CSE2383 Programming Assignment 1, version 1.2 Page 1 Algorithm Analysis Objective The objective of this programming assignment is to present the student with a C++ introduction assignment with a more challenging problem to solve than the basic practice tasks given in regular homework assignments. This assignment also attempts to illustrate the differences in running times for algorithms that perform the same task: finding prime numbers smaller than a given integer. Instructions Implement a program that asks the user for an integer n, which method (trial division or sieve of Eratosthenes) to use, and whether the prime numbers should be printed to the screen. The program should then find all of the prime numbers smaller than n using the selected method and print the time taken by the algorithm. There is a timer class and example program attached to this assignment. During testing of your program use the option to print the prime numbers found by the algorithms to the screen for verification. Finding prime values by Trial Division To decide if a number is prime using trial division, try dividing the number by all integers between 2 and the square root of the number. If there is a remainder from all of the division operations, the number is prime. You will test each integer less than n using this process to create a list of all the prime numbers less than n. Finding prime values by Sieve of Eratosthenes The following excerpt for finding prime numbers using the Sieve of Eratosthenes algorithm is from http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes (it is important that you view this page to get a better idea on what the algorithm does): To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method: 1. Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n). 2. Initially, let p equal 2, the first prime number. 3. Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked. 4. Find the first number greater than p in the list that is not marked; let p now equal this number (which is the next prime). 5. If there were no more unmarked numbers in the list, stop. Otherwise, repeat from step 3. When the algorithm terminates, all the numbers in the list that are not marked are prime. Run your program for different values of n (at least 10) and record the times taken for finding the prime numbers smaller than n. Recorded times should be distinct and non-zero. For the analysis questions below, use the timings of your program execution that does not print the prime values to the screen. CSE2383 Programming Assignment 1, version 1.2 Page 2 Choose values of n such that valid timings (greater than 0 seconds) are found and choose an increment between the values of n such that your times for each n are not the same. You must report timings for 10 different values of n for each method of finding prime numbers. NOTE: for the sake of debugging your code, choose trivial values of n (e.g. 25 to 32 or smaller) so that your code can finish its search quickly. For actual timing runs, choose larger values of n such that a difference in execution between the two algorithms can be observed. Be aware that some values of n may result in very long execution times (i.e., several minutes to hours). Therefore, it is important for you to complete writing the program in enough time to allow for adequate timing analysis runs. Files and starter code Below is a list of files and/or starter code that is provided with this assignment. If these files are not provided, notify your instructor immediately. The list below may also include a small description on what each files does (or provides); the list may elect to refer you to the comments provided within the file itself. The following files are included in the compressed file, student_files.zip: timer.h and testTimer.cpp  A timer class that you will use to perform your timing analysis. The file testTimer.cpp provides an example of how to include this file into you program, create an object of the timer class, and then use its timing methods to perform a timing analysis prime.cpp  Starter code that includes the dynamic allocation of an array (using a pointer), and the signatures of the functions that will implement your two algorithms; the functions receive an array as an argument. You do not need to know the details on how to use dynamic memory, pointers, or how arrays are passed to functions to complete this assignment. See code comments for further details. prime.cpp #include <iostream> // the following directive allows you to use the user-defined class called Timer #include "timer.h" using namespace std; /*** Clarifying Comments ******************************************************* * The below two lines are the function signatures of the algorithms that you are * supposed to implement. The parameter array[] accepts an array as an argument * (see prime in the main function). Passing an array as an argument to a * function passes a reference (actually pointer) to the original array and * allows the function to modify the contents of the passed array; this is * similar to how lists are handled when passing them as arguments to a * function in Python. For example if you pass in prime as an argument to * sieveErat, and if you change the value array[4] inside of the sieveErat, * function the change will be made to prime[4] since array is constant pointer * to the first element in prime. * * parameter NUM is the number n you retrieved from the user * parameter SIZE is the size of the array. * * Note: both algorithms fill an array with all the found prime values. Since the * array[] parameter modifies the original passed argument you can use it to * store your found values into the prime array * * Note: the Trial Division algorithm will require you to find the square root of * a value. The C++ function that computes the square root of a number is sqrt() * look up the sqrt function and the library that should be included in the C++ * reference at http://en.cppreference.com/w/ * *******************************************************************************/ void trialDivision(int array[], const int NUM, const int SIZE); void sieveErat(int array[], const int NUM, const int SIZE); int main() { int *prime; // a pointer that will be used to create an array at run time int size = 0; // variable that would be used to set the size of the array Timer myTimer; // a Timer class object that you will use to time your algorithms /*** Your Code ********************************************* * place your code that asks for user input here. you are * going to need this value to set the size of prime array * as well as the number n and selected algorithm * * Note: when setting the variable size, it should be * at least twice the value of the number n you received * from the user ***********************************************************/ /*** Clarifying Comments ***************************************************** * the following statement creates an array using dynamic memory allocation. * IT IS NOT NECESSARY FOR YOU TO KNOW EITHER POINTERS OR HOW TO CREATE AN * ARRAY USING DYNAMIC MEMORY ALLOCATION TO COMPLETE THIS ASSIGNMENT. The * array created is named "prime" and its capcity is set to the value of the * size variable. Make sure the value, received by the user, that is assigned * to size is a non-zero value. * * Note: regardless of how it is created, prime is an array and can be used like * a regular array, that is, you can assign values to prime using an index, * for example: * * prime[3] = 7; * * and you can also read from the array using an index: * * cout << prime[3]; * prime[2] = prime[1] + prime[0] * cout << prime[2]; *****************************************************************************/ prime = new int[size]; /*** Your Code ********************************************* * place your code that times the execution of the algorithms * implemented by trialDivision() and sieveErat() ***********************************************************/ /*** Clarifying Comments ***************************************************** * for all things created using dynamic memory, you should also free up the * that memory when done using it (see the following statement). *****************************************************************************/ delete [] prime; return 0; } /*** Your Code ***************************************************************** * Here is where you can define the functions that implement the Trial Division * and Sieve of Eratosthenes algorithms. You may choose to give the signatures * above main() bodies, or leave the signatures in place and define the functions * here. *******************************************************************************/ timer.h // Timer.h documents a simple, platform-independent interval timer // that measures CPU usage in (fractional) seconds. // // Author: Joel Adams, for Hands On C++. // Date: November 1997 // // Revision History // // Seconds() added - kvlinden, 5-Feb-98 // // ANSI compliance, interface improvements // - Adams, March, 1998. // #ifndef TIMER #define TIMER #include <iostream> // ostream #include <ctime> // C time library: clock_t, clock(), CLOCKS_PER_SEC using namespace std; class Timer { public: Timer(); void Start(); void Stop(); void Reset(); double Clocks() const; double Seconds() const; void Print(ostream & out) const; private: clock_t myStartTime; clock_t myRunTime; bool running; }; //*************************************************************** // Timer constructor. * // Postcondition: myStartTime == 0 && myRunTime == 0 && * // running == false. * //*************************************************************** inline Timer::Timer() { myStartTime = myRunTime = 0; running = false; } //*************************************************************** // Start myself. * // Postcondition: myStartTime == the current clock value && * // running == true. * // Note: Start() while I'm running re-starts me. * //*************************************************************** inline void Timer::Start() { running = true; myStartTime = clock(); } //*************************************************************** // Stop myself. * // Precondition: running == true. * // Postcondition: myRunTime has been updated with time interval * // since the Timer was started && * // running == false. * // Note: Stop() only has an effect when I'm running. * //*************************************************************** inline void Timer::Stop() { if (running) { myRunTime += clock() - myStartTime; running = false; } } //*************************************************************** // Reset myself. * // Postcondition: myStartTime is the current clock value && * // myRunTime == 0. * // Note: Reset() while I'm running re-starts me. * //*************************************************************** inline void Timer::Reset() { myRunTime = 0; myStartTime = clock(); } //*************************************************************** // My current time value (in clocks) * // Return: the elapsed time since I was started. * // Note: If I'm not running: * // repeated calls to Clocks() return the same value. * // Otherwise: * // repeated calls to Clocks() return different values. * //*************************************************************** inline double Timer::Clocks() const { if (running) return double(clock() - myStartTime); else return double(myRunTime); } //*************************************************************** // My current time value (in seconds) * // Return: the elapsed time since I was started. * // Note: If I'm not running: * // repeated calls to Seconds() return the same value. * // Otherwise: * // repeated calls to Seconds() return different values. * //*************************************************************** inline double Timer::Seconds() const { if (running) return double(clock() - myStartTime) / double(CLOCKS_PER_SEC); else return double(myRunTime) / double(CLOCKS_PER_SEC); } //*************************************************************** // Output myself (function member). * // Receive: out, an ostream. * // Output: the time since I was started. * // Passback: out, containing the Timer output. * // Note: If I'm not running: * // repeated calls to Print() display the same value. * // Otherwise: * // repeated calls to Print() display different values. * //*************************************************************** inline void Timer::Print(ostream & out) const { out << Seconds(); } //*************************************************************** // Output a Timer (operator<<) * // Receive: out, an ostream, * // aTimer, a Timer. * // Output: the value of aTimer via out. * // Passback: out, containing the time since aTimer was started. * // Return: out, for output chaining. * // Note: If I'm not running: * // repeated calls to << display the same value. * // Otherwise: * // repeated calls to << display different values. * //*************************************************************** inline ostream & operator<< (ostream & out, const Timer & aTimer) { aTimer.Print(out); return out; } #endif timerTest.cpp #include "timer.h" #include <iostream> #include <iomanip> #include <cstdlib> using namespace std; int main() { Timer myTimer; cout << "How long does it take to print 10 thousand numbers?" << endl; myTimer.Start(); for (int i=0; i<10000; i++) cout << i << " "; myTimer.Stop(); cout << endl << endl; cout << "That took " << myTimer.Clocks() << " clock ticks. " << endl; cout << "That took " << setprecision(10) << myTimer.Seconds() << " seconds. " << endl; system("pause"); return EXIT_SUCCESS; }

    Expert Answer

     
    Here is the C++ program: #include <iostream>

    CSE2383 Programming Provision 1, accalculate 1.2 Page 1

    Algorithm Segregation

    Objective

    The extrinsic of this programming provision is to impart the tyro with a C++ presentation provision with a past challenging quantity to reresolve than the basic manner tasks absorbed in ordinary homework provisions. This provision to-boot attempts to embody the contrarietys in exoteric terms ce algorithms that execute the corresponding task: retort superexcellent aggregate minuteer than a absorbed integer.

    Instructions

    Tool a program that asks the reasonr ce an integer n, which manner (suffering disunion or sieve of Eratosthenes) to reason, and whether the superexcellent aggregate should be imprinted to the defend. The program should then invent entire of the superexcellent aggregate minuteer than n using the clarified manner and imimimprint the term fascinated by the algorithm. There is a termr tabulate and specimen program secure to this provision. During standarding of your program reason the liberty to imimimprint the superexcellent aggregate ground by the algorithms to the defend ce honesty.

    Retort superexcellent appreciates by Suffering Disunion

    To career if a enumerate is superexcellent using suffering disunion, standard dividing the enumerate by entire integers among 2 and the balance radix of the enumerate. If there is a surplus from entire of the disunion operations, the enumerate is superexcellent. You conquer standard each integer close than n using this rule to constitute a inventory of entire the superexcellent aggregate close than n.

    Retort superexcellent appreciates by Sieve of Eratosthenes

    The subjoined gather ce retort superexcellent aggregate using the Sieve of Eratosthenes algorithm is from http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes (it is material that you survey this page to earn a meliorate purpose on what the algorithm does):

    To invent entire the superexcellent aggregate close than or correspondent to a absorbed integer n by Eratosthenes’ manner:

    1. Constitute a inventory of continuous integers from 2 to n: (2, 3, 4, …, n).

    2. Initially, everyow p correspondent 2, the principal superexcellent enumerate.

    3. Starting from p, calculate up in increments of p and sign each of these aggregate leading than p itself in the inventory. These aggregate conquer be 2p, 3p, 4p, expectation.; voicelessness that some of them may entertain already been signed.

    4. Invent the principal enumerate leading than p in the inventory that is relateable signed; everyow p now correspondent this enumerate (which is the present superexcellent).

    5. If there were no past unnotable aggregate in the inventory, plug. Otherwise, recapitulate from stalk 3.

    When the algorithm terminates, entire the aggregate in the inventory that are relateable signed are superexcellent.

    Escape your program ce irrelative appreciates of n (at subsidedest 10) and annals the terms fascinated ce retort the superexcellent aggregate minuteer than n. Annalsed terms should be unlike and non-zero. Ce the segregation questions beneath, reason the timings of your program effort that does relateable imimimprint the superexcellent appreciates to the defend.

    CSE2383 Programming Provision 1, accalculate 1.2 Page 2

    Adopt appreciates of n such that available timings (leading than 0 seconds) are ground and adopt an increment among the appreciates of n such that your terms ce each n are relateable the corresponding. You must rumor timings ce 10 irrelative appreciates of n ce each manner of retort superexcellent aggregate.

    NOTE: ce the reason of debugging your principle, adopt trite appreciates of n (e.g. 25 to 32 or minuteer) so that your principle can end its pursuit straightway. Ce goalive timing escapes, adopt larger appreciates of n such that a contrariety in effort among the brace algorithms can be observed. Be sensible that some appreciates of n may end in very hanker effort terms (i.e., sundry minutes to hours). Therefore, it is material ce you to consummate communication the program in sufficient term to entireow ce large timing segregation escapes.

    Files and starter principle

    Beneath is a inventory of rasps and/or starter principle that is supposing with this provision. If these rasps are relateable supposing, relateableify your professor straightway. The inventory beneath may to-boot apprehend a smentire patronymic on what each rasps does (or provides); the inventory may chosen to relate you to the comments supposing among the rasp itself.

    The subjoined rasps are apprehendd in the close rasp, tyro_files.zip:

    timer.h and standardTimer.cpp

     A termr tabulate that you conquer reason to execute your timing segregation. The rasp standardTimer.cpp provides an specimen of how to apprehend this rasp into you program, constitute an goal of the termr tabulate, and then reason its timing manners to execute a timing segregation

    prime.cpp

     Starter principle that apprehends the dynamic entireocation of an attire (using a pointer), and the signatures of the employments that conquer tool your brace algorithms; the employments entertain an attire as an reasoning. You do relateable deficiency to distinguish the details on how to reason dynamic reminiscence, pointers, or how attires are ignoringed to employments to consummate this provision. Comprehend principle comments ce elevate details.

    prime.cpp

    #apprehend <iostream>

    // the subjoined moral entireows you to reason the reasonr-defined tabulate denominated Termr
    #apprehend “timer.h”

    using namespace std;

    /*** Clarifying Comments *******************************************************
    * The beneath brace lines are the employment signatures of the algorithms that you are
    * reported to tool. The parameter attire[] accepts an attire as an reasoning
    * (comprehend superexcellent in the deep employment). Latter an attire as an reasoning to a
    * employment ignoringes a relateence (in-movables pointer) to the primary attire and
    * entireows the employment to modify the variation of the ignoringed attire; this is
    * concordant to how inventorys are handled when latter them as reasonings to a
    * employment in Python. Ce specimen if you ignoring in superexcellent as an reasoning to
    * sieveErat, and if you substitute the appreciate attire[4] internally of the sieveErat,
    * employment the substitute conquer be made to superexcellent[4] gone attire is fixed pointer
    * to the principal component in superexcellent.
    *
    * parameter NUM is the enumerate n you retrieved from the reasonr
    * parameter SIZE is the dimension of the attire.
    *
    * Voicelessness: twain algorithms glut an attire with entire the ground superexcellent appreciates. Gone the
    * attire[] parameter modifies the primary ignoringed reasoning you can reason it to
    * treastable your ground appreciates into the superexcellent attire
    *
    * Voicelessness: the Suffering Disunion algorithm conquer demand you to invent the balance radix of
    * a appreciate. The C++ employment that computes the balance radix of a enumerate is sqrt()
    * contemplate up the sqrt employment and the library that should be apprehendd in the C++
    * relateence at http://en.cppreference.com/w/
    *
    *******************************************************************************/
    unfilled sufferingDivision(int attire[], const int NUM, const int SIZE);
    unfilled sieveErat(int attire[], const int NUM, const int SIZE);

    int deep() {

    int *prime; // a pointer that conquer be reasond to constitute an attire at escape term
    int dimension = 0; // capricious that would be reasond to fixed the dimension of the attire
    Timer myTimer; // a Termr tabulate goal that you conquer reason to term your algorithms
    /*** Your Principle *********************************************
    * fix your principle that asks ce reasonr input here. you are
    * going to deficiency this appreciate to fixed the dimension of superexcellent attire
    * as well-mannered-mannered as the enumerate n and clarified algorithm
    *
    * Voicelessness: when fixedting the capricious dimension, it should be
    * at subsidedest twice the appreciate of the enumerate n you entertaind
    * from the reasonr
    ***********************************************************/

    /*** Clarifying Comments *****************************************************
    * the subjoined announcement constitutes an attire using dynamic reminiscence entireocation.
    * IT IS NOT NECESSARY FOR YOU TO KNOW EITHER POINTERS OR HOW TO CREATE AN
    * ARRAY USING DYNAMIC MEMORY ALLOCATION TO COMPLETE THIS ASSIGNMENT. The
    * attire constituted is denominated “prime” and its capcity is fixed to the appreciate of the
    * dimension capricious. Make stable the appreciate, entertaind by the reasonr, that is refered
    * to dimension is a non-zero appreciate.
    *
    * Voicelessness: regardclose of how it is constituted, superexcellent is an attire and can be reasond like
    * a ordinary attire, that is, you can refer appreciates to superexcellent using an refutation,
    * ce specimen:
    *
    * superexcellent[3] = 7;
    *
    * and you can to-boot peruse from the attire using an refutation:
    *
    * cextinguished << superexcellent[3];
    * superexcellent[2] = superexcellent[1] + superexcellent[0]
    * cextinguished << superexcellent[2];
    *****************************************************************************/
    superexcellent = novel int[size];

    /*** Your Principle *********************************************
    * fix your principle that terms the effort of the algorithms
    * tooled by sufferingDivision() and sieveErat()
    ***********************************************************/

    /*** Clarifying Comments *****************************************************
    * ce entire things constituted using dynamic reminiscence, you should to-boot liberal up the
    * that reminiscence when produced using it (comprehend the subjoined announcement).
    *****************************************************************************/
    delete [] superexcellent;

    retaliate 0;
    }

    /*** Your Principle *****************************************************************
    * Here is where you can settle the employments that tool the Suffering Disunion
    * and Sieve of Eratosthenes algorithms. You may adopt to impart the signatures
    * over deep() bodies, or concession the signatures in fix and settle the employments
    * here.
    *******************************************************************************/

    timer.h

    // Termr.h documents a artless, platform-independent gap termr
    // that measures CPU performance in (fractional) seconds.
    //
    // Author: Joel Adams, ce Hands On C++.
    // Date: November 1997
    //
    // Revision History
    //
    // Seconds() added – kvlinden, 5-Feb-98
    //
    // ANSI yielding, interface improvements
    // – Adams, March, 1998.
    //

    #ifndef TIMER
    #settle TIMER

    #apprehend <iostream> // ostream
    #apprehend <ctime> // C term library: clock_t, clock(), CLOCKS_PER_SEC
    using namespace std;

    tabulate Termr
    {
    public:
    Timer();
    unfilled Start();
    unfilled Plug();
    unfilled Reset();
    inclose Clocks() const;
    inclose Seconds() const;
    unfilled Imprint(ostream & extinguished) const;

    private:
    clock_t myStartTime;
    clock_t myRunTime;
    bool exoteric;
    };

    //***************************************************************
    // Termr composer. *
    // Postcondition: myStartTerm == 0 && myRunTerm == 0 && *
    // exoteric == untrue. *
    //***************************************************************
    inline Termr::Timer()
    {
    myStartTerm = myRunTerm = 0;
    exoteric = untrue;
    }

    //***************************************************************
    // Start myself. *
    // Postcondition: myStartTerm == the present clock appreciate && *
    // exoteric == gentleman. *
    // Voicelessness: Start() occasion I’m exoteric re-starts me. *
    //***************************************************************
    inline unfilled Termr::Start()
    {
    exoteric = gentleman;
    myStartTerm = clock();
    }

    //***************************************************************
    // Plug myself. *
    // Precondition: exoteric == gentleman. *
    // Postcondition: myRunTerm has been updated with term gap *
    // gone the Termr was agoing && *
    // exoteric == untrue. *
    // Voicelessness: Plug() merely has an movables when I’m exoteric. *
    //***************************************************************
    inline unfilled Termr::Stop()
    {
    if (running)
    {
    myRunTerm += clock() – myStartTime;
    exoteric = untrue;
    }
    }

    //***************************************************************
    // Refixed myself. *
    // Postcondition: myStartTerm is the present clock appreciate && *
    // myRunTerm == 0. *
    // Voicelessness: Reset() occasion I’m exoteric re-starts me. *
    //***************************************************************
    inline unfilled Termr::Reset()
    {
    myRunTerm = 0;
    myStartTerm = clock();
    }

    //***************************************************************
    // My present term appreciate (in clocks) *
    // Retaliate: the late term gone I was agoing. *
    // Voicelessness: If I’m relateable exoteric: *
    // recapitulateed calls to Clocks() retaliate the corresponding appreciate. *
    // Otherwise: *
    // recapitulateed calls to Clocks() retaliate irrelative appreciates. *
    //***************************************************************
    inline inclose Termr::Clocks() const
    {
    if (running)
    retaliate inclose(clock() – myStartTime);
    else
    retaliate inclose(myRunTime);
    }

    //***************************************************************
    // My present term appreciate (in seconds) *
    // Retaliate: the late term gone I was agoing. *
    // Voicelessness: If I’m relateable exoteric: *
    // recapitulateed calls to Seconds() retaliate the corresponding appreciate. *
    // Otherwise: *
    // recapitulateed calls to Seconds() retaliate irrelative appreciates. *
    //***************************************************************
    inline inclose Termr::Seconds() const
    {
    if (running)
    retaliate inclose(clock() – myStartTime) / inclose(CLOCKS_PER_SEC);
    else
    retaliate inclose(myRunTime) / inclose(CLOCKS_PER_SEC);
    }

    //***************************************************************
    // Extinguishedput myself (employment component). *
    // Entertain: extinguished, an ostream. *
    // Extinguishedput: the term gone I was agoing. *
    // Ignoringback: extinguished, containing the Termr extinguishedput. *
    // Voicelessness: If I’m relateable exoteric: *
    // recapitulateed calls to Imprint() flaunt the corresponding appreciate. *
    // Otherwise: *
    // recapitulateed calls to Imprint() flaunt irrelative appreciates. *
    //***************************************************************
    inline unfilled Termr::Print(ostream & extinguished) const
    {
    extinguished << Seconds();
    }

    //***************************************************************
    // Extinguishedput a Termr (operator<<) *
    // Entertain: extinguished, an ostream, *
    // aTimer, a Termr. *
    // Extinguishedput: the appreciate of aTimer via extinguished. *
    // Ignoringback: extinguished, containing the term gone aTimer was agoing. *
    // Retaliate: extinguished, ce extinguishedput chaining. *
    // Voicelessness: If I’m relateable exoteric: *
    // recapitulateed calls to << flaunt the corresponding appreciate. *
    // Otherwise: *
    // recapitulateed calls to << flaunt irrelative appreciates. *
    //***************************************************************
    inline ostream & operator<< (ostream & extinguished, const Termr & aTimer)
    {
    aTimer.Print(out);
    retaliate extinguished;
    }

    #endif

    timerTest.cpp

    #apprehend “timer.h”
    #apprehend <iostream>
    #apprehend <iomanip>
    #apprehend <cstdlib>

    using namespace std;

    int deep()
    {
    Timer myTimer;

    cextinguished << “How hanker does it obtain?} to imimimprint 10 thousand aggregate?” << endl;
    myTimer.Start();

    ce (int i=0; i<10000; i++)
    cextinguished << i << ” “;
    myTimer.Stop();

    cextinguished << endl << endl;
    cextinguished << “That took ” << myTimer.Clocks() << ” clock ticks. ” << endl;
    cextinguished << “That took ” << fixedprecision(10) << myTimer.Seconds() << ” seconds. ” << endl;

    system(“pause”);
    retaliate EXIT_SUCCESS;
    }

    Expert Retort

     

    Here is the C++ program:

    #apprehend <iostream>

    using namespace std;

    unfilled artlessSieve(int boundary, vector<int> &prime)

    {

    bool sign[limit+1];

    memset(mark, gentleman, dimensionof(mark));

    ce (int p=2; p*p<limit; p++)

    {

    if (mark[p] == gentleman)

    {

    ce (int i=p*2; i<limit; i+=p)

    mark[i] = untrue;

    }

    }

    ce (int p=2; p<limit; p++)

    {

    if (mark[p] == gentleman)

    {

    prime.push_back(p);

    cextinguished << p << ” “;

    }

    }

    }

    unfilled segmentedSieve(int n)

    {

    int boundary = pavement(sqrt(n))+1;

    vector<int> superexcellent;

    simpleSieve(limit, superexcellent);

    int subsided = boundary;

    int eminent = 2*limit;

    occasion (subsided < n)

    {

    bool sign[limit+1];

    memset(mark, gentleman, dimensionof(mark));

    ce (int i = 0; i < superexcellent.size(); i++)

    {

    int loLim = pavement(low/prime[i]) * superexcellent[i];

    if (loLim < subsided)

    loLim += superexcellent[i];

    ce (int j=loLim; j<high; j+=prime[i])

    mark[j-low] = untrue;

    }

    ce (int i = subsided; i<high; i++)

    if (mark[i – subsided] == gentleman)

    cextinguished << i << ” “;

    subsided = subsided + boundary;

    eminent = eminent + boundary;

    if (eminent >= n) eminent = n;

    }

    }

    int deep()

    {

    int n = 100;

    cextinguished << “Primes minuteer than ” << n << “:n”;

    segmentedSieve(n);

    retaliate 0;

    }