简体   繁体   中英

C++ Finding the nth prime number

i am trying to find the nth prime number. For example: input 1 - result 2, input 2 - result 3, input 3-result 5...

My isprime function currently works but i couldnt figure it out, there must be something wrong, please help, thanks:)

/*
      Finding the nth Prime Number
              Yasin OSMAN
*/

//Including Libraries
#include <iostream>
using namespace std;

//Defining a global counter
int counter = 0;
/*
         Defining Functions
*/

//isPrime Function (returns true if the given number is prime)
bool isPrime(int n) {
    bool answer = true;
    for (int i = 2; i < n; i++) {
        if (n % i == 0) {
            answer = false;
        }
    }
    return answer;
}

int main() {
    int userInput;
    cout<<"Please indicate which prime number do you want to see: ";
    cin>>userInput;

    for(int i=0;i<=userInput;i++){
        if(isPrime(counter)){
            if(counter==userInput){
                cout<<counter<<"th prime number is : "<<i<<endl;
            }
            counter++;
        }
        counter++;
    }

    return 0;
}

To improve the isPrime function:

  • numbers less than 2 won't be prime, so reject them first.
bool isPrime(int n) {
    if (n < 2) return false; // add this line
    bool answer = true;
    for (int i = 2; i < n; i++) {
        if (n % i == 0) {
            answer = false;
        }
    }
    return answer;
}

To improve the main function:

  • Increment the counter only if a prime number is found.
  • Count prime numbers found, then check the total number.
  • Check if the number, not the counter, is prime.
int main() {
    int userInput;
    cout<<"Please indicate which prime number do you want to see: ";
    cin>>userInput;

    for(int i=0, counter=0;counter<=userInput;i++){ // note: the global "counter" is shadowed here
        if(isPrime(i)){
            counter++;
            if(counter==userInput){
                cout<<counter<<"th prime number is : "<<i<<endl;
            }
        }
    }

    return 0;
}

Thanks to @MikeCAT

i changed my isPrime function for numbers that less than 2,

bool isPrime(int n) {
    bool answer = true;
    if(n<2){
        answer=false;
        return answer;
    }
    if(n>=2){
        for (int i = 2; i < n; i++) {
            if (n % i == 0) {
                answer = false;
                return answer;
        }
    }
    return answer;
    }
}

and i also made nthPrime a function,

int nthPrime(int n){
    double i;
    for(i=2;counter<n;i++){
        if(isPrime(i)){
            counter++;
        }
    }
    return i-1;
}

and to display the result i used the following code:

int userInput;
    cout<<"Please indicate which prime number do you want to see: ";
    cin>>userInput;

    cout<<counter<<"th prime number is : "<<nthPrime(userInput);
    return 0;

Output Example: Please indicate which prime number do you want to see: 5 5th prime number is: 11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM