You're probably running out of memory: every recursive subroutine call will take up some amount of memory until it returns. By the way, your code looks very strange: you're passing all kinds of parameters to prime() but you never use them. Instead you appear to be modifying global variables. In fact, it doesn't really look like a "traditional" recursive algorithm at all.