Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Below is the code I have made to find the nth prime no by taking "n" as the argument in the function (" void prime"). However I am not getting the answer as expected.

What I have tried:

C++
#include<iostream>
#include<cmath>

using namespace std;

void prime(int n)
{
    int ans=0;
    //int c=0;
    int MAX=1000000;
    int temp=0;
    if(n==1)
    {

        cout<<2;
    }
    else if(n==2)
    {

        cout<<2;

    }
    else
    {

    for (int i=5;i<MAX;i++)
    {
        for (int j=2;j<sqrt;j++)
        {

            if(i%j==0)
                temp=1;
            break;

        }
        if(temp==1)
        ans++;
            if(ans==n)
            {
                cout<<"\n "<<n<<"th prime no. is "<<i;
                    break;
            }
    }
    }

}
int main()
{
    cout<<"\n Enter n";
    int n;
    cin>>n;
    prime(n);
    return 0;
}
Posted
Updated 4-Jan-17 14:55pm
Comments
Richard MacCutchan 4-Jan-17 12:26pm    
Does that code compile?

So use the debugger and follow through what your code is doing.
This is part of your homework, and getting the code to work correctly is part of the task: indeed, it's often the bit that is most interesting and time consuming!

So put a breakpoint on the first line of the function, and step through the code working out in advance what should be happening. Compare that with what did happen and when they don't match you will start to get an idea as to why. You can use the debugger to look at variable contents, as well as stepping through your code line-by-line, and for some systems you can even edit the code while it is running!

But we don't know what system and IDE you are using, so we can't give explicit instructions. Google for "debugger" and the name of your compilation system and you should find at least basic instructions on how to use it.
 
Share this answer
 
First of, please follow the good advice of OriginalGriff: Get familiar with using a debugger. That's probably going to be the biggest step you will make in your programming skills so far!

Now to your question: You did not show in your code how the variable sqrt is being computed. The code as it stands would not even compile.

And second: You have used the variable temp as indicator that i is not prime. You start off with the assumption that it is prime and then test all integers j whether they are a divider of i. As soon as you have found a j that is a divider, then i cannot be prime and you set temp to 1. So far so good -- but then you are drawing the wrong conclusion:
if (temp==1)
    ans++;
if (ans==n)
{
    cout<<"\n "<<n<<"th prime no. is "<<i;
    break;
}

In variable ans you count how many prime numbers you have found so far. So you should increase it, if temp is 0, i.e. no divider of i had been found!

And there is still one more bug lurking: You initialize temp at the very beginning of your function. Now consider what happens when temp has been set to 1 for the first time. What will happen to the next i you are testing? See the problem? It can easily be fixed, but that is left for you as it is your homework.
 
Share this answer
 
C++
if(n==1)
{

    cout<<2;
}
else if(n==2)
{

    cout<<2;

}

It is highly suspect that fist and second primes are 2.
It also look like the code do not compile.

Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[<a href="http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn" target="_blank" title="New
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2
Your prime number function isnt working. Google for some valid code...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900