|
|
back to boardWrong Answer #2 Posted by nushrat 15 Nov 2016 09:19 I have been stuck on this test 2 for a while now. I gave up then came back, but still don't know what I need to change. Here is my solution, I used 'sieve of eratosthenes' to get out the primes. Please help me out. Should I change my input range for array. #include <iostream> #include <math.h> using namespace std; int main() { __int64 n; cin >> n; __int64 *input_list = new __int64[n]; //__int64 *primes = new __int64[15000]; long int primes[20000] = { 0 };
// input the numbers for (int i = 0; i < n; i++) { cin >> input_list[i]; } // Let A be an array of Boolean values, indexed by integers 2 to n, //initially all set to true. //for i = 2, 3, 4, ..., not exceeding √n: //if A[i] is true: //for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n : //A[j] := false
//Output: all i such that A[i] is true. long int a[20000] = { 0 };
for (long int i = 2; i <= 20000; i++) { if (a[i] == 0) { for (long int j = i * i, c=1; j < 20000; j +=i, c++) { a[j] = 1; } } //cout << a[i] << ends; } for (long int i = 2, p=0; i <= 20000; i++) { if (a[i] == 0) { //cout << i << ends; primes[p] = i; p++; } else { continue; } } for (int i = 0; i < n; i++) cout << primes[input_list[i] - 1] << endl; return 0; } Edited by author 15.11.2016 09:33 Re: Wrong Answer #2 For a test 5 2260 2261 2262 2263 2264 your program gives 19991 19993 19997 0 0. I tried an "obvious" fix, changing all 20000's into 200000's, to fit for 1 15000 test, but got a stack overflow error. I don't want to look too deep into it, so i'd rather suggest going an easy way. Just make IsPrime(int value), which would determine if the number is prime or not, then for i=1..infinity, add i into your array if it's prime, and stop after adding 15000 numbers. After that it should be easy. |
|
|