# Thread: ****.exe has stopped working

1. ## ****.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. 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.

3. 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);
}```

4. 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. 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. 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. 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.

8. 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. for learning purpose, How could we possibly use an recursive call from master recursive call? would be interesting to know..

10. 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.

11. 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);
}
}```