Thread: ****.exe has stopped working

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    16

    ****.exe has stopped working

    I wrote a code below to find out the prime factors using recursion. it runs fine but terminates with a message: primfactor.exe has stopped working. check for online solution.. any idea why so?

    Code:
    /*prime factor by recursion*/
    #include<stdio.h>
    int rec (int,int); /* this semi colon is must*/
    int main(void)
    {
    	int n;
    	printf("Enter any positive integer for prime factorization ? ");
    	scanf("%d",&n);
    	rec(n,2);
    	return (0);
    }
    int rec(int n, int l)
    {
    	
    	while (1)
    		{
    			if (n%l==0)
    			{
    				printf("%d ",l);
    				n=n/l;
    			}
    			else
    			{
    				if (n==l)
    					break;
    				rec(n,l+1);
    			}
    		}
    	return (0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You use "while(1)" OR recursion - not both together at the same time.

    while(1) calling another while(1) calling another while(1) calling another while(1) is a special brand of infinity.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Salem, I feel while(1) is must in this case since we don't know the number of loops required. I put the if condition at right place and got the code running fine. here is the edited version:
    Code:
    /*prime factor by recursion*/
    #include<stdio.h>
    int rec (int,int); /* this semi colon is must*/
    int main(void)
    {
    	int n;
    	printf("Enter any positive integer for prime factorization ? ");
    	scanf("%d",&n);
    	rec(n,2);
    	return (0);
    }
    int rec(int n, int l)
    {
    	
    	while (1)
    		{
    			if (n%l==0)
    			{
    				printf("%d ",l);
    				n=n/l;
    			}
    			else
    				break;
    		}
    	if (n>l)
    		rec(n,l+1);
    	if (n==l || n<l)
    		return (0);
    }
    Last edited by kawaikx15; 11-19-2011 at 04:06 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You are thinking iteratively, not recursively.

    The number of times you recursively call the function doesn't need to be known. What needs to be known is:

    1) that you have in fact a stopping point for the recursion. It's essential that the recursion not go horribly deep with nested calls.

    2) that you are calling the function with the right parameters. Generally, some progress must be made, with each execution through the recursive function.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    so what you think must be the right recursive solution for this problem? shall I replace the while loop with another recursive call to says rec1?

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    thinking more recursively, I changed the while(1) loop into another recursive call:
    Code:
    /*prime factor by recursion*/
    #include<stdio.h>
    int rec (int,int);
    int rec1 (int, int); /* this semi colon is must*/
    int main(void)
    {
    	int n;
    	printf("Enter any positive integer for prime factorization ? ");
    	scanf("%d",&n);
    	rec(n,2);
    	return (0);
    }
    int rec(int n, int l)
    {
    	n=rec1(n,l);
    	if (n>l)
    		rec(n,l+1);
    	return 0;
    }
    int rec1(int n, int l)
    {
    	if (n%l==0)
    	{
    		printf("%d ",l);
    		n=n/l;
    		rec1(n,l);
    	}
    	return n;
    	return 0;
    }
    But the code is wrong answer for n=288

    any help possible?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    what is a "prime" factor?

    A prime number less than n, the greatest common denominator, any factor at all, or ??

    Complimentary recursive functions are quite elegant, but more than you require, I believe.

    Edit: I just looked up what prime factorization was. We use that fundamental theorem of Arithmetic for some programs that find primes, but I've never heard of this term before.
    Last edited by Adak; 11-19-2011 at 06:50 AM.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    you are right.. I finally found a pure recursive solution with just one recursive function.
    Code:
    /*prime factor by recursion*/
    #include<stdio.h>
    int rec (int,int); /* this semi colon is must*/
    int main(void)
    {
    	int n;
    	printf("Enter any positive integer for prime factorization ? ");
    	scanf("%d",&n);
    	rec(n,2);
    	return (0);
    }
    int rec (int n, int l)
    {
    	if (n%l==0)
    	{
    		printf("%d ",l);
    		n=n/l;
    		rec(n,l);
    	}
    	else
    	{
    		if (n<l)
    			return 0;
    		else
    		{
    			l=l+1;
    			rec(n,l);
    		}
    	}
    	return 0;
    }

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    for learning purpose, How could we possibly use an recursive call from master recursive call? would be interesting to know..

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    May I note that using l's and 1's in the same loops is asking for errors? Try using i, or j, or k, or m, or anything else except O ("oh"), and l ("el"), and capital I.

    I haven't seen them arranged as recursive functions with master and slave varieties. I've seen complimentary or symbiotic recursive functions that called each other, in old style mini-max functions. (used in chess, checkers, and other two party games of perfect information, in the early days). When the move was for white, one function was called recursively, and when the move changed one ply (half-move, where each players move is a half of a whole move), deeper, the other recursive function was called. This is a look ahead function, so each time the computer is thinking, both white and black moves have to be considered. Thus the back and forth calls.

    Nowadays, they just use the same function, but scores are multiplied by -1 at odd or even ply depths, depending on which color the computer is, and how scoring is set up.
    Last edited by Adak; 11-19-2011 at 07:03 AM.

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    I found this code working fine. it uses another recursive call from a recursive call.. I don't know if calling them master-slave is valid or not.. but it is good learning about recursive functions and calling them from each other..
    Code:
    /*prime factor by recursion*/
    /* calling another recursive function from a recursive function*/
    #include<stdio.h>
    int rec (int,int);
    int rec1 (int,int);
    int main(void)
    {
    	int n;
    	printf("Enter any positive integer for prime factorization ? ");
    	scanf("%d",&n);
    	rec(n,2);
    	return (0);
    }
    int rec(int n, int l)
    {
    	int x;
    	x=rec1(n,l);
    	l=l+1;
    	if (x>=l)
    		rec(x,l);
    	else
    		return (0);
    
    }
    int rec1(int n, int l)
    {
    	if (n%l!=0)
    		return (n);
    	else
    	{
    		printf("%d ",l);
    		n=n/l;
    		rec1(n,l);
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. .exe has stopped working
    By bluesky16 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2011, 12:58 PM
  2. vshost.exe has stopped working
    By Marty21 in forum C# Programming
    Replies: 1
    Last Post: 06-08-2009, 10:41 AM
  3. Replies: 4
    Last Post: 03-26-2008, 08:48 AM
  4. Mouse scroll stopped working
    By Waldo2k2 in forum Tech Board
    Replies: 2
    Last Post: 10-12-2004, 11:25 AM