1

when i run this line of code, when it gets to prime number 199, it stack overflows. when I run it as a normal exe it gets up to 300 and somehting.

public class primemake
{
    public Int64 prime = 2;
    public Int64 check;

    public void text()
    {
        Console.Clear();
        Console.WriteLine("welcome to prime genorator!");

        Console.WriteLine("prime:" + prime );

        Console.ReadKey();
        check = prime;
        checker();            
    }

    public void checker()       
    {   
        prime = prime + 1;
        if (check == 1)
        {                    
            text();
        }
        else if (prime % check == 0)
        {
            check = prime;
            checker();                    
        }
        else if (prime % check != 0)
        {
            check = check - 1;
            prime = prime - 1;
            checker();
        }
    }
}

anyone know what is happening? help would be very much liked.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • recursion. calling the same function from itself until you've used up the stack provided by your process. – kenny Aug 16 '14 at 19:32
  • There are too many nested method calls in a language/environment that does not apply TCO (in this case).. – user2864740 Aug 16 '14 at 19:32
  • "anyone know what is happening" ... uhm, yes, you are exhausting the stack. What is the **real** question here? – Lasse V. Karlsen Aug 16 '14 at 19:36
  • @user2864740 Tail Call Optimization (TCO) as an acronym for Tail Call Optimization is not a well known acronym. Please don't use acronyms that aren't well established. – Lasse V. Karlsen Aug 16 '14 at 19:50
  • @LasseV.Karlsen I will try to remember to spell out less common acronyms in the future. – user2864740 Aug 16 '14 at 19:52
  • On a side-note, recursive methods w/o parameters, operating on a global state are very hard to debug. This is a bad approach. – H H Aug 16 '14 at 20:14
  • 1
    I'm not even sure this [actually checks for prime numbers](http://en.wikipedia.org/wiki/Primality_test) – alstonp Aug 16 '14 at 20:20

1 Answers1

4

You call the method checker within checker. That means that it's recursive, which means it might (or in your case, will) dig deeper and deeper into itself with every iteration, eventually having so many recursive calls that it, as you observe, throws a StackOverflowException. Fortunately, you can write this without recursion, just using loops. That will solve your problem.

Matthew Haugen
  • 12,916
  • 5
  • 38
  • 54
  • The result/Exception is [not always the case](http://stackoverflow.com/questions/14085783/why-doesnt-this-recursion-produce-a-stackoverflowexception). (However, I do not believe that the above code is eligible for the transformation - perhaps due to co-recursion.) – user2864740 Aug 16 '14 at 19:36
  • Not at all. I am saying that a function *could* "dig deeper and deeper into itself with every iteration" (forever) and not cause an exception. Just something to keep in mind. – user2864740 Aug 16 '14 at 19:38
  • 1
    This is the only exception that directs directly to the website with the correct anser ("StackOverflow") – Emile Aug 16 '14 at 19:39
  • @user2864740 Yep, I saw your edit refreshed when I posted that comment. I deleted mine. That is interesting, though. I didn't know the compiler did that. – Matthew Haugen Aug 16 '14 at 19:43
  • @CarlosCosta please mark this as an answer if it helped you, so people in the future with a similar problem can find the solution as well. – Matthew Haugen Aug 16 '14 at 19:43