Thread: Recuration Prime Numbers

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    27

    Recuration Prime Numbers

    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;
    }
    Last edited by megazord; 05-13-2010 at 09:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Perfect and Prime numbers
    By taggrath in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2009, 02:13 AM
  2. Finding Prime Numbers
    By dan724 in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 12:12 PM
  3. Checking very large prime numbers
    By password in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2008, 12:26 PM
  4. Replies: 18
    Last Post: 11-04-2005, 02:41 PM
  5. Replies: 2
    Last Post: 09-11-2002, 05:00 PM