Originally Posted by
anduril462
Don't use recursion to implement numPrime. A for loop is a much better approach. For larger numbers, recursion will likely blow out your stack and your program may crash. If you have to use global or static local variables to make your recursive solution work, it's probably not something you should do recursively.
Also, there are a couple simple speedups you can do to your basic isPrime check:
1. Instead checking divisors < num, just check divisors <= sqrt(num). Don't calculate sqrt(num) in the loop though, it's slow. Calculate it once and store the result in a temporary variable. Use the temp variable in your loop condition.
2. Check for divisibility by 2 explicitly, then start your loop at 3 and increment i by 2 each iteration.
There are much more efficient ways to check for primality, but your implementation is the simplest to code and most intuitive, so stick with that for now.