I am using the sieve of sundaram to get an array of primes upto 4000 and then doing binary search on it to find the next greatest prime then the sum of X and Y and outputting the different between the prime and the sum. but it gives me wrong answer.
help?
link to my code
https://www.codechef.com/viewsolution/16379802
Your binary function is wrong:
correct one is here:
int binary(int z) {
int r = primes.size() - 1;
int l = 0;
while(l < r) {
int mid = (l + r) / 2;
if(primes[mid] <= z and primes[mid+1] > z)
return primes[mid +1];
if(primes[mid] > z && primes[mid-1] <= z)
return primes[mid];
else if(primes[mid] <= z) // <=
l = mid + 1;
else
r = mid - 1;
}
}