The algorithm consist in that the user give a number (n) and the program have to find the prime numbers and the multiples of it, and when it finds the multiple the program delete the multiple number.
#include <iostream>
usingnamespace std;
int ssys(int user_n);
int main() {
int user_n;
cout << "Enter a number: ";
cin >> user_n;
cout << "Your Sieve of Eratosthenes algorithm is:\n" << ssys(user_n);
system("pause");
return 0;
}
int ssys(int user_input){
int x;
int number_array[user_input];
for (int i = (user_input - 1); i > -1; i--){
number_array[i] = i+1;
}
//For 2 to be a prime number
for (int i = 1; i < user_input; i++){
x = 0;
for (int j = 1; j < i; j++){
if (number_array[i]%j == 0){
x = 1;
if (number_array[i] == 2){x = 0;}
}
}
if (x == 0){
cout << number_array[i] << " is a prime number\n";
//------------------------Find Multiples------------------------
for (int p = i; p < user_input; p++){
x = 0;
if (number_array[p]%number_array[i] == 0){
x = 1;
}
if (x == 1){
cout << number_array[p] << " is a multiple of " << number_array[i] << endl;
}
}
}
}
//For rest to be a prime number
for (int i = 2; i < user_input; i++){
x = 0;
for (int j = 2; j < i; j++){
if (number_array[i]%j == 0){
x = 1;
if (number_array[i] == 2){x = 0;}
}
}
if (x == 0){
cout << number_array[i] << " is a prime number\n";
//------------------------Find Multiples------------------------
for (int p = i; p < user_input; p++){
x = 0;
if (number_array[p]%number_array[i] == 0){
x = 1;
}
if (x == 1){
cout << number_array[p] << " is a multiple of " << number_array[i] << endl;
}
}
}
}
}
I know that it does not affect the algorithm, however I want it to print something like this.
User Number: 13
Print:
2 is a prime number.
4 is multiple of 2.
6 is multiple of 2.
8 is multiple of 2.
10 is multiple of 2.
12 is multiple of 2.
3 is a prime number.
9 is multiple of 3.
5 is a prime number.
7 is a prime number.
13 is a prime number
Without saying:
6 is multiple of 3, 12 is multple of 3 and 10 is multiple of 5, because it was said first that it was multiple of 2.
There should be no division or modulo operations in your code. +1
All the sieve of Eratosthenes is something like this:
01 2 3 4 5 6 7 8 9 10
01 2 3 4 5 6 7 8 9 10 //removed 2's: 4 6 8 10
01 2 3 4 5 6 7 8910 //removed 3's: 9
//5*5 > 10 so no need to go further
Prime: 2 3 5 7
The algorithm I use is a slightly optimized but not fully.
pseudo code:
1 2 3 4
for i = 3 to sqrt(N) increment i by 2 <--sqrt(N) is same as i * i < N
if i is prime
for j = i * i to N increment j by i
j is not prime
PS reason I didn't set the 0, 1, or the evens to not prime is because we already know they must be odd so no need to use the extra processing. We can just start at 3 and increment by 2 again when we output the primes. PS you won't be able to output ALL the primes in the sieve so I would output after.
By the way a hint is to use an array of type bool that is size of N + 1 (so we can go from 0->N) or a std::vector<bool>
2 is a prime number.
4 is multiple of 2.
6 is multiple of 2.
8 is multiple of 2.
10 is multiple of 2.
12 is multiple of 2.
3 is a prime number.
9 is multiple of 3.
5 is a prime number.
7 is a prime number.
13 is a prime number
Thanks you very much, I found an error that comes here:
sort(begin(vec), end(vec));
because it says that begin() and end() are not declare.
However, I think I can't use this code because it has things that I havent study like Vectors. But thanks you very much, I got some ideas from your code and from giblit code too.
OK, i got why giblit differs. i just saw the Wikipedia page of the sieve. the intention of the sieve is to 'sieving' prime numbers in range - by 'sieve method' rather than other prime algorithm. so don't use the prime function i used!
i had read in a book: "programmers will work the exact you asked for" so did i for the comment
JJRR26 (3)
however your code conforms to the concept of the sieve.
Here is my finished code, because I cant delete in arrays, I organized them at the begining of the Array the prime numbers, If there is a way to reduce the array size, I will be grateful for showing me how. Also if there is a better way to do my code, It will be a pleasure to know it, because there is always a better way!
#include <iostream>
usingnamespace std;
int ssys(int user_n);
int main() {
int user_n;
cout << "Enter a number: ";
cin >> user_n;
cout << "Your Sieve of Eratosthenes algorithm is:\n" << endl;
ssys(user_n);
system("pause");
return 0;
}
int ssys(int user_input){
int x;
int n=2;
int number_array[user_input];
for (int i = (user_input - 1); i > -1; i--){
number_array[i] = i+1;
}
//Find prime number
for (int i = 1; i < user_input; i++){
x = 0;
for (int j = 2; j < i; j++){
if (number_array[i]%j == 0){
x = 1;
}
if (number_array[i] == 2){x = 0;}
}
if (x == 0){
cout << number_array[i] << " is a prime number\n";
if(n<=i){
number_array[n]=number_array[i];//moving the prime numbers to the new position
n=n+1;
}
//Find multiples of the number
for (int p = i; p < user_input; p++){
x = 0;
if (number_array[p]%number_array[i] == 0){
x = 1;
}
if (x == 1){
cout << number_array[p] << " is a multiple of " << number_array[i] << endl;
}
}
}
}
cout << "Your prime numbers are oreganized from lowest to highest from the position 0 to " << n-1 << ". \n";
for(int w=0; w<n; w++){//Showing the prime numbers
cout << number_array[w] << " ";
}
}