stack overflow - How do I avoid stackoverflowexception within a finite loop (C#) -
i'm trying write code find prime numbers within given range. unfortunately i'm running problems many repetitions that'll give me stackoverflowexception after prime nr: 30000. have tried using 'foreach' , not using list, (doing each number comes) nothing seems handle problem in hand.
how can make program run forever without causing stackoverflow?
class program { static void main(string[] args) { stopwatch stopwatch = new stopwatch(); stopwatch.start(); list<double> primes = new list<double>(); const double start = 0; const double end = 100000; double counter = 0; int lastint = 0; (int = 0; < end; i++) primes.add(i); (int =0;i< primes.count;i++) { lastint = (int)primes[i] - roundoff((int)primes[i]); primes[i] = (int)checkforprime(primes[i], math.round(primes[i] / 2)); if (primes[i] != 0) { console.write(", {0}", primes[i]); counter++; } } stopwatch.stop(); console.writeline("\n\nnumber of prime-numbers between {0} , {1} is: {2}, time took calc this: {3} (millisecounds).\n\n" + " end\n", start, end, counter, stopwatch.elapsedmilliseconds); } public static double checkforprime(double prim, double devider) { if (prim / devider == math.round(prim / devider)) return 0; else if (devider > 2) return checkforprime(prim, devider - 1); else return prim; } public static int roundoff(int i) { return ((int)math.floor(i / 10.0)) * 10; } }
Comments
Post a Comment