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.

7
  • recursion. calling the same function from itself until you've used up the stack provided by your process. Commented Aug 16, 2014 at 19:32
  • There are too many nested method calls in a language/environment that does not apply TCO (in this case).. Commented Aug 16, 2014 at 19:32
  • "anyone know what is happening" ... uhm, yes, you are exhausting the stack. What is the real question here? Commented Aug 16, 2014 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. Commented Aug 16, 2014 at 19:50
  • 1
    I'm not even sure this actually checks for prime numbers Commented Aug 16, 2014 at 20:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The result/Exception is not always the case. (However, I do not believe that the above code is eligible for the transformation - perhaps due to co-recursion.)
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.
This is the only exception that directs directly to the website with the correct anser ("StackOverflow")
@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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.