-1

I have used divider logic for filter out prime number by 2, 3, 5, 7 As per my understanding It's working fine but still seems to me this logic will miss out some prime pick up Please help me out for find out counter example for this logic.

Function Call:generatePrime(200 , 10000);

// List of prime number between given range
int generatePrime(int start, int end){
    long int diviser, *rangeList;
    int count;
    if(start <= end){
        while(start <= end){
            count = 0;
            if((start != 2 && start%2 == 0 )|| (start != 3 && start%3 == 0) || (start!= 5 && start%5 == 0 )|| (start != 7 && start%7 == 0) ){
                 count = 1;
            }
            if(!count && start != 1){
                cout << start << "\n";
            }
            start++;
        }
    }
    return 0;
}

Working Example of code: http://ideone.com/yOCBLd

Nishant Kumar
  • 5,629
  • 17
  • 63
  • 94
  • Your idea about how prime numbers are defined is rather unusual. – n. 'pronouns' m. Aug 03 '14 at 04:42
  • @n.m. Is this code able to generate correct prime number ? – Nishant Kumar Aug 03 '14 at 04:45
  • make sure ur definition of prime number is correct –  Aug 03 '14 at 04:46
  • @KenWhite My approch is diff and question is diff and the link which u are pointing I allready know that solutions however that is not the my question at all. – Nishant Kumar Aug 03 '14 at 04:47
  • Run it, then compare the results with any list of primes you find on the internets. I don't want to spoil the fun. – n. 'pronouns' m. Aug 03 '14 at 04:49
  • The question is the same, despite the fact you've posted (incorrect) code trying to do so. The linked question also has information about a better solution. (Also, for future reference: C++ != C. If you have a question about C++, don't add the C tag. If your question is about C, don't tag it C++. They're not the same language.) – Ken White Aug 03 '14 at 04:54

1 Answers1

2

The algorithm you developed is following the perfect idea: All numbers that can factor a prime (with the exception of that primes itself) are NOT prime. However you are only testing the first 4 prime numbers. You need to test all of them. The solution would be to keep a record of of the primes you have found and test them all.

// List of prime number between given range
int generatePrime(int start, int end){
    int current_number = 2;
    int all_primes [end-start];
    int number_of_primes_found=0;
    bool found;
    if(start <= end){
        while(current_number <= end){
            found = false;
            for (int i=0; i<number_of_primes_found; i++) {
                int current_prime_to_test = all_primes[i];
                if (current_number%current_prime_to_test == 0) {
                    all_primes[i+1] = start;
                    number_of_primes_found++;
                    found = true;
                    break;
                }
            }

            if(found && current_number >= start){
                cout << current_number << "\n";
            }
            current_number++;
        }
    }
    return 0;
}

I also changed int count to bool found to simplify things since you were essentially using it as a boolean. Let me know if you have any questions!

CorbinMc
  • 558
  • 2
  • 8