Best C++ Program to generate prime numbers. Best C++ Program to generate prime numbers. @Computer Science

For any given candidate number, the smallest possible divisor is 2, and the largest possible divisor is (candidate / 2). So, for that range of divisors, check the result of (candidate % [modulo] divisor). If any result is zero, then the candidate is not a prime. If none of the divisor yield a zero modulo result, the candidate is a prime. Leave it to you to do your own coding of this algorithm. It's really quite simple.

im ganna write it in c language which might help ya: all i understanf is that you wanna generate all possible prime numbers (and lets say from 0 to 1000) int count; int i; for(count=0;count<=1000;count ++) { int p=0; for(i=0;i<=count;i++) if(count % i ==0) p++; if(p==2) printf("count\t); }

hope it will work....try it :D

I read a webpage that said the best-performing algorithm was the Sieve of Eratosthenes. But it requires O(n) space, where n is the max prime you're looking for. A simpler way is to check divisors from 2 to sqrt(n). To optimize it a little more only test odd n's (ignore 2). To optimize more, only test odd divisors. To optimize more, only test prime divisors. To only test prime divisors, keep a list of primes that you find, and iterate over it when testing divisors (stop when you get to sqrt(n)).

Join our real-time social learning platform and learn together with your friends!