Click here to Skip to main content
15,885,665 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<iostream>
using namespace std;
int main()
{
    int a,j=2,c=0;
    cin>>a;
    for(int i=2;i<(a*a);i++)
    {   for( j=2;j<i;j++)>
        {
            if(i%j==0)
                break;

        }
        if(i==j)
        {
            c++;
            if(c==a)
            {
                cout<<i;  return 0;
            }
        }
    }
    return 0;
}


What I have tried:

i have written this code to print the nth prime number and want to develop it through recursive function.
Posted
Updated 7-May-16 3:40am
v2
Comments
Sergey Alexandrovich Kryukov 7-May-16 11:10am    
Why, why recursion? How many numbers do you want to find? This is, obviously enough, a problem for the iteration algorithm.
—SA

Bug report: On first look I would say that your program fail to find the first prime.

Question: why do you want to use recursion to find if a number is prime ?
Ok it may simplify the code, but it is at expense of PC resources, for large numbers you are at rick of stack overflow.
If you write the recursive code very carefully, it is possible to take advantage of the terminal recursion optimization depending on compiler capabilities.

Also your program is far from optimized, you use a very naive method. If you want to optimize the code, huge gains can be achieved.
 
Share this answer
 
v3
Google is your friend: recursive prime - Google Search[^]
 
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