0

Hi guys. I am trying to create a program where the user inputs an integer and the program will cout the nth prime number. Unfortunatly when I try to cout just the nth prime number it doesn't work. However theoretically if I have the program print out all of the prime numbers up to the nth prime number, it just prints out 3,4,5,6,7,8 etc. UI means userinput in my code.

#include<iostream>
using namespace std;

int main(){
    unsigned int UI; // userinput
    int counter1 = 1; // number of primenumbers
    bool isprime = true;
    int primenumber = 3; //number that it is checking to see if it is prime. 
    cout << "Enter in a positive integer" << endl;
    cin >> UI;
    while(UI == 0){
        cout << "Invalid input. Please try again" << endl;
        cin >> UI;
    }
    if(UI == 1){
        cout << endl << "2"<< endl;
    }
    else if(UI == 2){
        cout << endl << "3" << endl;
    }
    else if(UI > 2){
        while(counter1 != UI){
            isprime = true;
            cout << primenumber << ",";
            for(int checker = 2; checker < primenumber; checker++){
                if(primenumber % checker == 0){
                    isprime = false;
                    break;
                }
            }
            if(isprime == true){
                counter1++;
            }
            primenumber++;
        }
        if(counter1 == UI){
            cout << primenumber << endl;
        }
    }

}
4
Contributors
3
Replies
35
Views
1 Week
Discussion Span
Last Post by Adeniyi_3
1

Actually your algorithm is mostly correct. what you were forgetting is that 2 is the first prime number, but you weren't printing that.

Even though your algorithm is naive, there are some optimizations worth mentioning:

The greatest number that needs to be checked if it's a factor is the square root of the number. Everything that's a factor after that, its reciprocal has already been checked.

After 2 all the other primes are odd. Therefore, you only need to check the odd numbers.

Here's your code, that keeps all this in mind. Plus a few other tweaks:

#include <cmath>
#include<iostream>
using namespace std;

int main()
{
    unsigned int UI; // userinput
    cout << "Enter in a positive integer" << endl;
    cin >> UI;
    while (UI == 0)
    {
        cout << "Invalid input. Please try again" << endl;
        cin >> UI;
    }
    if (UI == 1)
    {
        cout << endl << "2" << endl;
    }
    else if (UI == 2)
    {
        cout << endl << "3" << endl;
    }
    else if (UI > 2)
    {
        int counter1 = 1; // number of primenumbers
        bool isprime = true;
        int primenumber = 3; //number that it is checking to see if it is prime.         
        cout << 2;
        while (counter1 != UI)
        {
                isprime = true;
                int limit = sqrt(primenumber);
                for (int checker = 3; checker < limit; checker += 2)
                {
                    if (primenumber % checker == 0)
                    {
                        isprime = false;
                        break;
                    }
                }

            if (isprime == true)
            {
                cout << "," << primenumber;
                counter1++;
            }
            primenumber += 2;
        }
        cout << '\n';
    }
}

On a side note. Try to always keep the declaration of your variables as close as possible to where you first need them. On a small project, this is kind of a moot point. But, on a large project it is invaluable.

Edited by tinstaafl

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.