-2

I have an array of random numbers and i have to return the prime numbers from that array. I am familiar with root(n) solution(n being that particular number not the size of the array). I can not apply sieve of Eratosthenes as it works with numbers in certain range but here the numbers are completely random.

Please correct me if i am missing something. Thanks in advance!

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • Either get the maximum from the input and use the sieve up to that number, or just solve it individually for each number. What is the problem? – trincot Jan 12 '19 at 10:04
  • dont we have to use extra space(like hash) to store all prime using sieve and then check if the given number is present in it ? i want to do it without using space. – Kundan Ranjan Jan 12 '19 at 12:12
  • 1
    *"without using space"*: That is not mentioned in your question. On the contrary, you tell us you considered the sieve, but rejected it, not because it needs space, but because the numbers are random. – trincot Jan 12 '19 at 12:26
  • `I am familiar with root(n) solution` I can't even tell whether I am: please sketch it in your post. – greybeard Jan 12 '19 at 18:22

3 Answers3

1

You are looking for a primality test. You should be able to search and find lots of possibilities. Here is an answer I wrote a few years ago that is probably far more than you want:

Fastest way to find if a given number is prime

Most of the details there are for numbers larger than 64-bit where there are lots of possible digressions and choices. For 64-bit inputs, the simple and reasonable answer is to use a little trial division followed by a crafted set of Miller-Rabin tests which give deterministic results (there is both no randomness used and no possibility of error if correctly implemented). If you want to optimize a bit then there are hashed sets and BPSW to consider.

Addendum: There are cases that could be done faster if the number of inputs is much larger than the maximum input size or the number of unique inputs, or if there is some distribution such as an expectation of many repeated inputs. Then solutions such as caching or generating a bitset for fast lookup could be faster. Knowledge of the input set helps a lot.

DanaJ
  • 724
  • 6
  • 9
0

If space is important and you are familiar with c++ you could use bitset instead of bool which would be an 8 times improvement because bool uses 8 bits for an element, whereas bitset uses only one bit to store a value of 1 or 0;

const int SIZE = 1000000;
const int LIMIT = sqrt(SIZE)+1;

bitset<SIZE> prime;

void sieve() {
    prime.flip();
    prime[1]=0;
    for(int i=2;i<=LIMIT;i++) {
        if (prime[i])
            for(int j=2*i;j<SIZE;j+=i)
                prime[j]=0;
    }
}

bool isPrime(int n) {
    return prime[n];
}
Dan Butmalai
  • 100
  • 1
  • 8
  • You can halve the size of `prime` by only holding odd numbers and dealing with 2 separately in `isPrime()`. – rossum Jan 13 '19 at 23:52
0

Every prime number is adjacent to 6n (where n>=1 ) .

Frist check that a number is adjacent to multiple of 6 .

If the number is adjacent then apply primality test algorithm on that number . Justification for 6n method : https://www.youtube.com/watch?v=ZMkIiFs35HQ