This code is supposed to find the nth prime number (n being input by the user). I have the code to calculate whether a number is prime or not, but I don't know how to get the code to display the nth prime number.

#include <iostream>
using namespace std;

void isprime(int);

int main () 
{
    int x;
    cout << "Which prime number would you like to display? ";
    cin >> x;

    if (x==0)
    {
        cout << "That is not a valid choice. Please enter another number." << endl;
        cin >> x;
    }

    if (x<0)
    {
        cout << "That is not a valid choice. There are no negative prime numbers. Please enter another number." << endl;
        cin >> x;
    }


    return 0;
}

void isprime(int x)
{
    cout << "Enter a number: ";
    int n;
    cin >> n;

    int isprime;

    if(n<2 || n>2 && n%2==0)
    {
        isprime=0;

        for(int i=2; i<n; i++)
        {
            if((n%i)==0)
            {
                isprime=0;
            }
        }
    }

    else isprime=1;
}   

Dani AI

Generated

A clear, simple plan fixes the issues in the thread: make is_prime a pure test that returns true/false, run a loop in main that tests successive integers and increments a counter when a prime is found, and stop when the counter reaches the requested index. This follows and : generate primes until you have x. was right to suggest a counter — the mistake in the posted code is mixing input, uninitialized variables, and == used where assignment was intended; keep responsibilities separate (input/counting in main, primality in one small function).

A minimal, robust approach (trial division up to sqrt(n), skip evens) is easy to implement and fine for small-to-moderate x:

#include <iostream>
#include <cmath>

bool is_prime(int n) {
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0) return n == 2;
    int r = static_cast<int>(std::sqrt(n));
    for (int d = 3; d <= r; d += 2)
        if (n % d == 0) return false;
    return true;
}

int nth_prime(int k) {
    int count = 0, num = 1;
    while (count < k) {
        ++num;
        if (is_prime(num)) ++count;
    }
    return num;
}

For large x the sieve of Eratosthenes (generate all primes up to a bound) is much faster; to size the sieve use a safe estimate for the k-th prime (for k >= 6 one can use bounds based on n (ln n + ln ln n)). Also watch memory (sieving large ranges) and integer overflow when k is huge — for very large inputs look at segmented sieves or more advanced bounds/tests. (en.wikipedia.org)

Recommended Answers

All 7 Replies

Start calcumating prime numbers. When you've calculated x of them, there you are...

And your tests for bad values of x would be better if you used a do-while loop.

That's the part I am stuck with. Should I be calculating the prime numbers in my isprime function or main? Also I'm not sure how to write that in the code. Is a do-while loop similar to a while loop? I never heard of do-while loop before. How would that differ from what I have now, with regards to the code?

You should calculate the prime numbers inside of main.
Do-While loops are the same as While loops but Do-While loops go through the loop once no matter what, then checks the condition, ie:

do
{
    // This will run once, even though the condition is false.
}while(1 == 2);

Hope this Helps!

The trouble I am having with the coding is this: My isprime function computes composite numbers as 0 and prime numbers as 1. I want the code to say that when isprime=1, it should count all those until it gets to the x-th value and output that.

Use a counter that increments whenever isprime is 1. Once the counter is equal to the input x, display the value you sent to isprime.

This is my code now, but it always calculates the answer as 0. Where am I going wrong?

#include <iostream>
using namespace std;

int isprime(int&);

int main () 
{
    int i;
    int x;
    cout << "Which prime number would you like to display? ";
    cin >> x;

    if (x==0)
    {
        cout << "That is not a valid choice. Please enter another number." << endl;
        cin >> x;
    }

    if (x<0)
    {
        cout << "That is not a valid choice. There are no negative prime numbers. Please enter another number." << endl;
        cin >> x;
    }

    while (isprime(x)==1)
    {
        for (i=2; i<=x; i++)
        {
            i;
        }
    }

    cout << "The " << x << "th prime number is " << i;

    return 0;
}

int isprime(int&x)
{
    int n;
    int isprime;

    if(n<2 || n>2 && n%2==0)
    {
        isprime==0;

        for(int i=2; i<n; i++)
        {
            if((n%i)==0)
            {
                isprime==0;
            }
        }
    }

    else isprime==1;

    return isprime;
}

You need to think through your problem more carefully, Given any input:
1) How many numbers do you have to test to find x primes? What kind of loop is best for this?
2) How can you count the primes you find so you know when x have been calculated?

And for your input, use while since you know how it works. Right now the user can break your input easily.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.