Help with Recursion.

• 05-07-2005
blindman858
Help with Recursion.
I am not sure I fully understand recursion. I understand that you need a base case that will terminate the code and that recursive call needs to get simpler. I am trying to write a recursive function that will determine whether an inputted number is prime or not. I keep getting a stack overflow error when I input a number. I beleive a stack overflow means that the base condition is never checked or something. If you can look at my code and give me advise on what I am doing wrong I would greatly appreciate it. At this point I am not conscerned with program efficiency so much. I am just starting out. Thanks again.

Code:

```bool checkprime(int nv) {                 int k =2;         if(nv ==2 || k>nv/2) //Base Case ???                 return true;                 if(nv%k ==0)                 return false;         else{                 k++;             return checkprime(nv); //Recursive call         } }```
• 05-07-2005
Salem
> return checkprime(nv); //Recursive call
This is exactly the same as what was input, so obviously nothing happens except consumption of stack space.
• 05-07-2005
blindman858
Code:

```bool checkprime(int kv,int nv) {         if(nv ==2 || kv<=2)                 return true;                 if(nv%kv ==0)                 return false;         else                 return checkprime(kv--,nv);         }```
I changed it so that the functions has two arguements. I still get the stack flow. I am making it simpler by reducing kv by one each recursive call. In main, i set k = n/2, where n is the number to be checked and called the function as follows checkprime(k,n). Why is it that even though the function parameter changed it still does nothing? Afterall n has to remain constant because thats the number being checked. K which represents the divisors has to be dynamic, it must change to determine primality. Any help appreicated.
• 05-07-2005
PJYelton
>>return checkprime(kv--,nv);<<

Try "return checkprime(--kv,nv);"
• 05-07-2005
blindman858
Quote:

Originally Posted by PJYelton
>>return checkprime(kv--,nv);<<

Try "return checkprime(--kv,nv);"

omg lol now it works. So i am guessing it kv-- assigns kv first and then decrement where as --kv decrements then assigns?
• 05-07-2005
Salem
How about using kv-1 and stop trying to outsmart yourself
• 05-07-2005
blindman858
Quote:

Originally Posted by Salem
How about using kv-1 and stop trying to outsmart yourself

WTF is ur problem man. I am trying to learn. If you dont want to help newbies then dont bother responding. Go satisfy your ego somewhere else instead of trying to hinder someone elses growth. :mad:
• 05-07-2005
Salem
Sod the insults, I couldn't give a rats ass whether you listen or not.
You fell down the same hole twice in a row - there's nothing else to add.
• 05-07-2005
blindman858
oh i am sorry i am not as smart as you. I shall pray to god that I become only a fraction as smart as you oh intelligent one. Please show mercy on this retarded insignificant mortal. :rolleyes: I took your advise the first time and fixed that aspect of the code. The second time around was a matter of understanding syntax.