Hi guys can you look over my code for PRIME1 (spoj.com/problems/PRIME1/) and see what the issue is?
#include <iostream>
#include <cstring>
void SieveOfEratosthenes(bool arr[])
{
int n = 2; // smallest prime is 2
int num = 0;
for (int i = 1; i < 1000000000; ++i) { //1,000,000,000 is the constraint in the question
while ((num = n * (i + 1)) <= 1000000000){
arr[num - 1] = 1; //if a number is not a prime, its position is set to 1
++n;
}
}
}
int main()
{
bool arr[1000000000]; //create boolean array of size 1000000000
memset(arr, 0, sizeof(arr));
SieveOfEratosthenes(arr);
int t = 0;
scanf("%d", &t);
typedef unsigned long ul;
for (int i = 0; i < t; ++t){
unsigned long n=0, m=0;
scanf("%lu%lu", &m, &n);
for (int i = m-1; m<=(n-1); ++m){
if (arr[i] == 0){
//if it has not been changed to 1, it is a prime.
printf("%d\n", arr[i]);
}
}
printf("\n");
}
return 0;
}
On the ideone.com C++ 4.3.2 compiler, it gives the following error:
prog.cpp:4: error: variable or field 'SieveOfEratosthenes' declared void
prog.cpp:4: error: 'arr' was not declared in this scope
In my code, I want to create an array that is the size of the constraint in the problem. Then set all non-prime positions in the array to 1.
Then, print all numbers whose position in the array is still 0 (if left as 0, it means it is a prime).
Additionally, does my algorithm work?