Hi all,
I wrote a code that finding the N'th prime number according to an index that given, for example: "Prime number #7 is 17."
my program working for very small index's, and I need it to work on an index till 1000, but after app. 50 I get "Stuck overFlow"
I need some help to improve my code.
(the int findNthPrime(int n); prototype is a must).
Thanx in advance.
my code:
Code:
#include <stdio.h>
#include <string.h>
#define INPUT_LEN 256
int print_menu(int menu);
int findNthPrime(int n);
int checkingINPUT_LEN(char choise[],int x);
int findingprime(int number,int i,int counter,int j);
void main()
{
print_menu(1);
}
int print_menu(int menu)
{
int n=0,temp;
char choice[INPUT_LEN]={0};
printf("Welcome to Math-N-Stuff.\nPlease select an option from the following menu (1, 2, 3 or 4)."
"\n0. Quit.\n1. Finding prime number:\n2. Find all primes on a list of numbers.\n"
"3. Calculate mathematical formula.\nPlease enter your choice\n");
gets(choice);
switch(choice[0]){
case '0':
/*Exit the program*/
printf("Quitting.\nGoodbye");
return -1;
case '1':
/* Option one, Finding the Nth prime number,
and the smallest prime number bigger then N */
printf("Please enter parameter:\n");
scanf("%d",&n);
//getchar();
if(n>1000){
printf("\nParameter too big,\n\n");
break;
}
if(n<1){
printf("\nIlegal input in option 1\n\n");
break;
}
temp=findNthPrime(n);
print_menu(1);
return -1;
case '2':
/*Finding all Prime's in arr*/
print_menu(1);
return -1;
case '3':
/*The function Calculates a formula given in char*/
print_menu(1);
return -1;
default:
/*if the input is wrong*/
printf("Ilegal input in main menu.\n\n");
print_menu(1);
return -1;
}
return -1;
}
int findingprime(int number,int i,int counter,int j)
{
if(counter==number)
return --i;
if(i%j==0){
i++;
j=1;}
if(j>=i/2){
i++;
counter++;
j=1;}
findingprime(number,i,counter,j+1);
}
int findNthPrime(int n)
{
int i=2,j=2,counter=1,prime=0;
prime=findingprime(n,i,counter,j);
printf("Prime number #%d is %d.\n\n",n,prime);
return prime;
}