I have a problem that I am frustrated with. I have to allocate an array of size 1000, with all values set to 1.
The values in this array are used to determine if a number is prime or not, non prime numbers, at the end of the algoritm, will have the corresponding value set to 0. I then need to print out all prime numbers to the screen.
My teacher states no modulus operator is to be used!!!!!
I have been staring at my compiler for hours trying to work this. This has me lost. Any help would be appreciated.
To compute all of the prime numbers in the range 1 ... N, begin with an array of N booleans
with each boolean set to true. (Let us assume here that arrays are 1-based -- we know in
C/C++ they are not, so take care).
Now run a loop L from 2 to N. If the Lth index is set to true, then for all indices I = L*k, k>1,
set the Ith index to false.
At the end of the loop, run another loop Z from 1 to N. If the Zth index is set to true, then Z
is prime.
So this is what I have for psuedocode. Does this make any sense?
1) allocate an array of size 1000, with all values set to 1. non prime numbers, at the end of the algoritm, will have the corresponding value set to 0.
2) Do the following loop:
for each number from 2 to 999 do
if (primeIndicators[number] is equal to 1)
for all multiple of number between 2 * number and 999
jsmith - I am running into a problem with my program. It compiles but is not printing out Prime numbers 1 -1000. Can you look at it and give me a hint or two please?
/ ******************************************
// * Decalre & Initialize array elements to 1.
// ******************************************
int prime_number_array[1000];
int arraysize = 1000;
int i = 0;
int multiple_of_number;