Hello everyone. this is my first post here. I am working on a prime number program that will output all primes from 1 to 100,000. I have the program working and running. It is to calculate the prime numbers and print seven per line. As of now it is printing a 0 for all non prime number instead of omitting them.
I am using code blocks software.
There are a lot of ways to find the prime numbers
Sieve of Eratosthenes is one of them, you could try it if you liked to
Besides, your code still have some space to improve, try to find out by yourself
And please no questions asked on why I did it this way. It's fast and that's all that matters. The only problem is that you might get one prime number over the value you want but... that's the only problem with it.
#define MAX 100000
for(int a = 0; a <= MAX; a++)
array[a] += a+1;
for( int j = 1, j<= MAX; j++)
int i = 2;
while ( i <= sqrt(static_cast<double>(array[j]));
if (array[j] % i != 0)
array[j] = 0;
for(int a = 1; a <= MAX; a++)
cout << array[a] << " ";
cout << endl;
the method is from C++ Without Fear -- it's modified though... as if (array[j] % i != 0) would be if (array[j] % i == 0) and that would tell if it's not prime while mine tells that it is in fact prime... which i'm not sure is actually correct..
And why I did the +=6 thing? It's because of this
2 is prime
3 is prime
4 is not because %2
5 is prime
6 is not because %2 and %3
7 is prime
now I skipped to 12 and checked 12-1 and 12+1
8 and 10 are not because % 2
and 9 is not because % 3
skip to 18
14 and 16 are not because %2
15 is not because % 3
And all the numbers are like this. So it's useless to check them all. You increment with 6 and you check the one before and the one after. Then you increment again with 6 and repeat.
Sorry for my bad way of explaining things. I'm not good at explaining things. I just hope you got it.