Program that finds prime numbers in a specific range











up vote
2
down vote

favorite












I'm kinda new to programming, and as a solution to an exercise, I made a program to find prime numbers in a range. It runs just right for small ranges of numbers, but for this exercise we are given a high range of nums, and it just takes much time to finish examining. Any suggestions how can I make it faster?



#include <stdio.h>

#define START 190000000
#define END 200000000

int main()
{
int primenum = 0, i = 0, j = 0, c = 0;
for (i = START; i <= END; i++)
{
printf("EXMINING %drn", i);
c = 2;
for (j = 2; j <= i-1; j++)
{
if (i%j == 0)
{ c=1;
break;
}
}
if (c == 2) primenum = primenum + 1;
printf("Prime Numbers Found so far: %drn", primenum);
}
printf("THE PRIME NUMBERS ARE %d", primenum);
return 0;
}









share|improve this question




















  • 4




    You probably are after a sieve of eratosthenes.
    – Gerrit0
    Oct 20 at 21:05















up vote
2
down vote

favorite












I'm kinda new to programming, and as a solution to an exercise, I made a program to find prime numbers in a range. It runs just right for small ranges of numbers, but for this exercise we are given a high range of nums, and it just takes much time to finish examining. Any suggestions how can I make it faster?



#include <stdio.h>

#define START 190000000
#define END 200000000

int main()
{
int primenum = 0, i = 0, j = 0, c = 0;
for (i = START; i <= END; i++)
{
printf("EXMINING %drn", i);
c = 2;
for (j = 2; j <= i-1; j++)
{
if (i%j == 0)
{ c=1;
break;
}
}
if (c == 2) primenum = primenum + 1;
printf("Prime Numbers Found so far: %drn", primenum);
}
printf("THE PRIME NUMBERS ARE %d", primenum);
return 0;
}









share|improve this question




















  • 4




    You probably are after a sieve of eratosthenes.
    – Gerrit0
    Oct 20 at 21:05













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm kinda new to programming, and as a solution to an exercise, I made a program to find prime numbers in a range. It runs just right for small ranges of numbers, but for this exercise we are given a high range of nums, and it just takes much time to finish examining. Any suggestions how can I make it faster?



#include <stdio.h>

#define START 190000000
#define END 200000000

int main()
{
int primenum = 0, i = 0, j = 0, c = 0;
for (i = START; i <= END; i++)
{
printf("EXMINING %drn", i);
c = 2;
for (j = 2; j <= i-1; j++)
{
if (i%j == 0)
{ c=1;
break;
}
}
if (c == 2) primenum = primenum + 1;
printf("Prime Numbers Found so far: %drn", primenum);
}
printf("THE PRIME NUMBERS ARE %d", primenum);
return 0;
}









share|improve this question















I'm kinda new to programming, and as a solution to an exercise, I made a program to find prime numbers in a range. It runs just right for small ranges of numbers, but for this exercise we are given a high range of nums, and it just takes much time to finish examining. Any suggestions how can I make it faster?



#include <stdio.h>

#define START 190000000
#define END 200000000

int main()
{
int primenum = 0, i = 0, j = 0, c = 0;
for (i = START; i <= END; i++)
{
printf("EXMINING %drn", i);
c = 2;
for (j = 2; j <= i-1; j++)
{
if (i%j == 0)
{ c=1;
break;
}
}
if (c == 2) primenum = primenum + 1;
printf("Prime Numbers Found so far: %drn", primenum);
}
printf("THE PRIME NUMBERS ARE %d", primenum);
return 0;
}






beginner c time-limit-exceeded primes






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 20 at 22:14









200_success

127k15148411




127k15148411










asked Oct 20 at 20:35









Δημήτρης Σπανάκης

112




112








  • 4




    You probably are after a sieve of eratosthenes.
    – Gerrit0
    Oct 20 at 21:05














  • 4




    You probably are after a sieve of eratosthenes.
    – Gerrit0
    Oct 20 at 21:05








4




4




You probably are after a sieve of eratosthenes.
– Gerrit0
Oct 20 at 21:05




You probably are after a sieve of eratosthenes.
– Gerrit0
Oct 20 at 21:05










1 Answer
1






active

oldest

votes

















up vote
0
down vote













I combine here all the above and look up tables.




  1. Use the threshold of sqrt(test_prime) to shrink the range to be tested, as said by @Gaurav.

  2. Increase the prime number to be tested by 2, as said by @1201ProgramAlarm.

  3. Use a look up tables to check only with the prime numbers we have detected until that moment (we remove a lot of unnecessary checks).

  4. Load/Save the look up table for future executions.

  5. Use SIMD instrinsics (not implemented in this solution), so that you can check 4 primes into the look up table at the same time.


My tests took about 4 minutes without pre-calculated lookup table, and about 30 seconds using pre-calculated lookup table.



The code here



#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define START 190000000
#define END 200000000

int is_prime(long test, long n_primes, long* list_primes) {
long max = sqrt(test);
long index = 1;
long prime = list_primes[index];
while (prime <= max) {
if (test % prime == 0)
return 0;
if (++index >= n_primes)
break;
prime = list_primes[index];
}
return 1;
}


void append_prime(long prime, long* size, long* n_primes, long** list_primes) {
if (*n_primes == *size) {
*list_primes = (long*)realloc(*list_primes, (*size + 4096)*sizeof(long));
*size += 1024;
}
(*list_primes)[*n_primes] = prime;
*n_primes += 1;
}


int load_from_disk(long* size, long* n_primes, long** list_primes) {
FILE* f = fopen("primes.dat", "rb");
if (f == NULL)
return 0;
fread(size, sizeof(long), 1, f);
fread(n_primes, sizeof(long), 1, f);
*list_primes = (long*)malloc( ( (*n_primes + 4095) / 4096 ) * 4096 * sizeof(long) );
fread(*list_primes, sizeof(long), *n_primes, f);
fclose(f);
f = NULL;
return 1;
}


int save_to_disk(long* size, long* n_primes, long** list_primes) {
FILE* f = fopen("primes.dat", "w");
if (f == NULL)
return 0;
fwrite(size, sizeof(long), 1, f);
fwrite(n_primes, sizeof(long), 1, f);
fwrite(*list_primes, sizeof(long), *n_primes, f);
fclose(f);
f = NULL;
return 1;
}


long find_primes_until(long threshold, long* size, long* n_primes, long** list_primes) {
if (!load_from_disk(size, n_primes, list_primes)) {
*size = 4096;
*n_primes = 0;
*list_primes = (long*)malloc((*size) * sizeof(long));
memset(*list_primes, 0, (*size) * sizeof(long));

if (threshold > 2) {
(*list_primes)[(*n_primes)++] = 2;
} else {
return *n_primes;
}

if (threshold > 3) {
(*list_primes)[(*n_primes)++] = 3;
} else {
return *n_primes;
}

if (threshold > 5) {
(*list_primes)[(*n_primes)++] = 5;
} else {
return *n_primes;
}

if (threshold > 7) {
(*list_primes)[(*n_primes)++] = 7;
} else {
return *n_primes;
}
}


long prime = (*list_primes)[(*n_primes)-1] + 2;
while (prime < threshold) {
//printf("Examining number: %ld / %ld r", prime, threshold);
if (is_prime(prime, *n_primes, *list_primes)) {
//printf("nPrime number found: %ldn", prime);
append_prime(prime, size, n_primes, list_primes);
}
prime += 2;
}
save_to_disk(size, n_primes, list_primes);

return *n_primes;
}


void find_primes_interval(long start, long end)
{
long* list_primes = NULL;
long size = 0;
long n_primes = 0;

find_primes_until(start, &size, &n_primes, &list_primes);

if ((start & 0x01) == 0)
start++;
while (start < end) {
printf("Examining number: %ld r", start);
if (is_prime(start, n_primes, list_primes)) {
printf("nPrime number found: %ldn", start);
append_prime(start, &size, &n_primes, &list_primes);
}
start += 2;
}
}


int main()
{
find_primes_interval(START, END);
return 0;
}





share|improve this answer























    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205955%2fprogram-that-finds-prime-numbers-in-a-specific-range%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I combine here all the above and look up tables.




    1. Use the threshold of sqrt(test_prime) to shrink the range to be tested, as said by @Gaurav.

    2. Increase the prime number to be tested by 2, as said by @1201ProgramAlarm.

    3. Use a look up tables to check only with the prime numbers we have detected until that moment (we remove a lot of unnecessary checks).

    4. Load/Save the look up table for future executions.

    5. Use SIMD instrinsics (not implemented in this solution), so that you can check 4 primes into the look up table at the same time.


    My tests took about 4 minutes without pre-calculated lookup table, and about 30 seconds using pre-calculated lookup table.



    The code here



    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>

    #define START 190000000
    #define END 200000000

    int is_prime(long test, long n_primes, long* list_primes) {
    long max = sqrt(test);
    long index = 1;
    long prime = list_primes[index];
    while (prime <= max) {
    if (test % prime == 0)
    return 0;
    if (++index >= n_primes)
    break;
    prime = list_primes[index];
    }
    return 1;
    }


    void append_prime(long prime, long* size, long* n_primes, long** list_primes) {
    if (*n_primes == *size) {
    *list_primes = (long*)realloc(*list_primes, (*size + 4096)*sizeof(long));
    *size += 1024;
    }
    (*list_primes)[*n_primes] = prime;
    *n_primes += 1;
    }


    int load_from_disk(long* size, long* n_primes, long** list_primes) {
    FILE* f = fopen("primes.dat", "rb");
    if (f == NULL)
    return 0;
    fread(size, sizeof(long), 1, f);
    fread(n_primes, sizeof(long), 1, f);
    *list_primes = (long*)malloc( ( (*n_primes + 4095) / 4096 ) * 4096 * sizeof(long) );
    fread(*list_primes, sizeof(long), *n_primes, f);
    fclose(f);
    f = NULL;
    return 1;
    }


    int save_to_disk(long* size, long* n_primes, long** list_primes) {
    FILE* f = fopen("primes.dat", "w");
    if (f == NULL)
    return 0;
    fwrite(size, sizeof(long), 1, f);
    fwrite(n_primes, sizeof(long), 1, f);
    fwrite(*list_primes, sizeof(long), *n_primes, f);
    fclose(f);
    f = NULL;
    return 1;
    }


    long find_primes_until(long threshold, long* size, long* n_primes, long** list_primes) {
    if (!load_from_disk(size, n_primes, list_primes)) {
    *size = 4096;
    *n_primes = 0;
    *list_primes = (long*)malloc((*size) * sizeof(long));
    memset(*list_primes, 0, (*size) * sizeof(long));

    if (threshold > 2) {
    (*list_primes)[(*n_primes)++] = 2;
    } else {
    return *n_primes;
    }

    if (threshold > 3) {
    (*list_primes)[(*n_primes)++] = 3;
    } else {
    return *n_primes;
    }

    if (threshold > 5) {
    (*list_primes)[(*n_primes)++] = 5;
    } else {
    return *n_primes;
    }

    if (threshold > 7) {
    (*list_primes)[(*n_primes)++] = 7;
    } else {
    return *n_primes;
    }
    }


    long prime = (*list_primes)[(*n_primes)-1] + 2;
    while (prime < threshold) {
    //printf("Examining number: %ld / %ld r", prime, threshold);
    if (is_prime(prime, *n_primes, *list_primes)) {
    //printf("nPrime number found: %ldn", prime);
    append_prime(prime, size, n_primes, list_primes);
    }
    prime += 2;
    }
    save_to_disk(size, n_primes, list_primes);

    return *n_primes;
    }


    void find_primes_interval(long start, long end)
    {
    long* list_primes = NULL;
    long size = 0;
    long n_primes = 0;

    find_primes_until(start, &size, &n_primes, &list_primes);

    if ((start & 0x01) == 0)
    start++;
    while (start < end) {
    printf("Examining number: %ld r", start);
    if (is_prime(start, n_primes, list_primes)) {
    printf("nPrime number found: %ldn", start);
    append_prime(start, &size, &n_primes, &list_primes);
    }
    start += 2;
    }
    }


    int main()
    {
    find_primes_interval(START, END);
    return 0;
    }





    share|improve this answer



























      up vote
      0
      down vote













      I combine here all the above and look up tables.




      1. Use the threshold of sqrt(test_prime) to shrink the range to be tested, as said by @Gaurav.

      2. Increase the prime number to be tested by 2, as said by @1201ProgramAlarm.

      3. Use a look up tables to check only with the prime numbers we have detected until that moment (we remove a lot of unnecessary checks).

      4. Load/Save the look up table for future executions.

      5. Use SIMD instrinsics (not implemented in this solution), so that you can check 4 primes into the look up table at the same time.


      My tests took about 4 minutes without pre-calculated lookup table, and about 30 seconds using pre-calculated lookup table.



      The code here



      #include <stdio.h>
      #include <stdlib.h>
      #include <math.h>
      #include <string.h>

      #define START 190000000
      #define END 200000000

      int is_prime(long test, long n_primes, long* list_primes) {
      long max = sqrt(test);
      long index = 1;
      long prime = list_primes[index];
      while (prime <= max) {
      if (test % prime == 0)
      return 0;
      if (++index >= n_primes)
      break;
      prime = list_primes[index];
      }
      return 1;
      }


      void append_prime(long prime, long* size, long* n_primes, long** list_primes) {
      if (*n_primes == *size) {
      *list_primes = (long*)realloc(*list_primes, (*size + 4096)*sizeof(long));
      *size += 1024;
      }
      (*list_primes)[*n_primes] = prime;
      *n_primes += 1;
      }


      int load_from_disk(long* size, long* n_primes, long** list_primes) {
      FILE* f = fopen("primes.dat", "rb");
      if (f == NULL)
      return 0;
      fread(size, sizeof(long), 1, f);
      fread(n_primes, sizeof(long), 1, f);
      *list_primes = (long*)malloc( ( (*n_primes + 4095) / 4096 ) * 4096 * sizeof(long) );
      fread(*list_primes, sizeof(long), *n_primes, f);
      fclose(f);
      f = NULL;
      return 1;
      }


      int save_to_disk(long* size, long* n_primes, long** list_primes) {
      FILE* f = fopen("primes.dat", "w");
      if (f == NULL)
      return 0;
      fwrite(size, sizeof(long), 1, f);
      fwrite(n_primes, sizeof(long), 1, f);
      fwrite(*list_primes, sizeof(long), *n_primes, f);
      fclose(f);
      f = NULL;
      return 1;
      }


      long find_primes_until(long threshold, long* size, long* n_primes, long** list_primes) {
      if (!load_from_disk(size, n_primes, list_primes)) {
      *size = 4096;
      *n_primes = 0;
      *list_primes = (long*)malloc((*size) * sizeof(long));
      memset(*list_primes, 0, (*size) * sizeof(long));

      if (threshold > 2) {
      (*list_primes)[(*n_primes)++] = 2;
      } else {
      return *n_primes;
      }

      if (threshold > 3) {
      (*list_primes)[(*n_primes)++] = 3;
      } else {
      return *n_primes;
      }

      if (threshold > 5) {
      (*list_primes)[(*n_primes)++] = 5;
      } else {
      return *n_primes;
      }

      if (threshold > 7) {
      (*list_primes)[(*n_primes)++] = 7;
      } else {
      return *n_primes;
      }
      }


      long prime = (*list_primes)[(*n_primes)-1] + 2;
      while (prime < threshold) {
      //printf("Examining number: %ld / %ld r", prime, threshold);
      if (is_prime(prime, *n_primes, *list_primes)) {
      //printf("nPrime number found: %ldn", prime);
      append_prime(prime, size, n_primes, list_primes);
      }
      prime += 2;
      }
      save_to_disk(size, n_primes, list_primes);

      return *n_primes;
      }


      void find_primes_interval(long start, long end)
      {
      long* list_primes = NULL;
      long size = 0;
      long n_primes = 0;

      find_primes_until(start, &size, &n_primes, &list_primes);

      if ((start & 0x01) == 0)
      start++;
      while (start < end) {
      printf("Examining number: %ld r", start);
      if (is_prime(start, n_primes, list_primes)) {
      printf("nPrime number found: %ldn", start);
      append_prime(start, &size, &n_primes, &list_primes);
      }
      start += 2;
      }
      }


      int main()
      {
      find_primes_interval(START, END);
      return 0;
      }





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        I combine here all the above and look up tables.




        1. Use the threshold of sqrt(test_prime) to shrink the range to be tested, as said by @Gaurav.

        2. Increase the prime number to be tested by 2, as said by @1201ProgramAlarm.

        3. Use a look up tables to check only with the prime numbers we have detected until that moment (we remove a lot of unnecessary checks).

        4. Load/Save the look up table for future executions.

        5. Use SIMD instrinsics (not implemented in this solution), so that you can check 4 primes into the look up table at the same time.


        My tests took about 4 minutes without pre-calculated lookup table, and about 30 seconds using pre-calculated lookup table.



        The code here



        #include <stdio.h>
        #include <stdlib.h>
        #include <math.h>
        #include <string.h>

        #define START 190000000
        #define END 200000000

        int is_prime(long test, long n_primes, long* list_primes) {
        long max = sqrt(test);
        long index = 1;
        long prime = list_primes[index];
        while (prime <= max) {
        if (test % prime == 0)
        return 0;
        if (++index >= n_primes)
        break;
        prime = list_primes[index];
        }
        return 1;
        }


        void append_prime(long prime, long* size, long* n_primes, long** list_primes) {
        if (*n_primes == *size) {
        *list_primes = (long*)realloc(*list_primes, (*size + 4096)*sizeof(long));
        *size += 1024;
        }
        (*list_primes)[*n_primes] = prime;
        *n_primes += 1;
        }


        int load_from_disk(long* size, long* n_primes, long** list_primes) {
        FILE* f = fopen("primes.dat", "rb");
        if (f == NULL)
        return 0;
        fread(size, sizeof(long), 1, f);
        fread(n_primes, sizeof(long), 1, f);
        *list_primes = (long*)malloc( ( (*n_primes + 4095) / 4096 ) * 4096 * sizeof(long) );
        fread(*list_primes, sizeof(long), *n_primes, f);
        fclose(f);
        f = NULL;
        return 1;
        }


        int save_to_disk(long* size, long* n_primes, long** list_primes) {
        FILE* f = fopen("primes.dat", "w");
        if (f == NULL)
        return 0;
        fwrite(size, sizeof(long), 1, f);
        fwrite(n_primes, sizeof(long), 1, f);
        fwrite(*list_primes, sizeof(long), *n_primes, f);
        fclose(f);
        f = NULL;
        return 1;
        }


        long find_primes_until(long threshold, long* size, long* n_primes, long** list_primes) {
        if (!load_from_disk(size, n_primes, list_primes)) {
        *size = 4096;
        *n_primes = 0;
        *list_primes = (long*)malloc((*size) * sizeof(long));
        memset(*list_primes, 0, (*size) * sizeof(long));

        if (threshold > 2) {
        (*list_primes)[(*n_primes)++] = 2;
        } else {
        return *n_primes;
        }

        if (threshold > 3) {
        (*list_primes)[(*n_primes)++] = 3;
        } else {
        return *n_primes;
        }

        if (threshold > 5) {
        (*list_primes)[(*n_primes)++] = 5;
        } else {
        return *n_primes;
        }

        if (threshold > 7) {
        (*list_primes)[(*n_primes)++] = 7;
        } else {
        return *n_primes;
        }
        }


        long prime = (*list_primes)[(*n_primes)-1] + 2;
        while (prime < threshold) {
        //printf("Examining number: %ld / %ld r", prime, threshold);
        if (is_prime(prime, *n_primes, *list_primes)) {
        //printf("nPrime number found: %ldn", prime);
        append_prime(prime, size, n_primes, list_primes);
        }
        prime += 2;
        }
        save_to_disk(size, n_primes, list_primes);

        return *n_primes;
        }


        void find_primes_interval(long start, long end)
        {
        long* list_primes = NULL;
        long size = 0;
        long n_primes = 0;

        find_primes_until(start, &size, &n_primes, &list_primes);

        if ((start & 0x01) == 0)
        start++;
        while (start < end) {
        printf("Examining number: %ld r", start);
        if (is_prime(start, n_primes, list_primes)) {
        printf("nPrime number found: %ldn", start);
        append_prime(start, &size, &n_primes, &list_primes);
        }
        start += 2;
        }
        }


        int main()
        {
        find_primes_interval(START, END);
        return 0;
        }





        share|improve this answer














        I combine here all the above and look up tables.




        1. Use the threshold of sqrt(test_prime) to shrink the range to be tested, as said by @Gaurav.

        2. Increase the prime number to be tested by 2, as said by @1201ProgramAlarm.

        3. Use a look up tables to check only with the prime numbers we have detected until that moment (we remove a lot of unnecessary checks).

        4. Load/Save the look up table for future executions.

        5. Use SIMD instrinsics (not implemented in this solution), so that you can check 4 primes into the look up table at the same time.


        My tests took about 4 minutes without pre-calculated lookup table, and about 30 seconds using pre-calculated lookup table.



        The code here



        #include <stdio.h>
        #include <stdlib.h>
        #include <math.h>
        #include <string.h>

        #define START 190000000
        #define END 200000000

        int is_prime(long test, long n_primes, long* list_primes) {
        long max = sqrt(test);
        long index = 1;
        long prime = list_primes[index];
        while (prime <= max) {
        if (test % prime == 0)
        return 0;
        if (++index >= n_primes)
        break;
        prime = list_primes[index];
        }
        return 1;
        }


        void append_prime(long prime, long* size, long* n_primes, long** list_primes) {
        if (*n_primes == *size) {
        *list_primes = (long*)realloc(*list_primes, (*size + 4096)*sizeof(long));
        *size += 1024;
        }
        (*list_primes)[*n_primes] = prime;
        *n_primes += 1;
        }


        int load_from_disk(long* size, long* n_primes, long** list_primes) {
        FILE* f = fopen("primes.dat", "rb");
        if (f == NULL)
        return 0;
        fread(size, sizeof(long), 1, f);
        fread(n_primes, sizeof(long), 1, f);
        *list_primes = (long*)malloc( ( (*n_primes + 4095) / 4096 ) * 4096 * sizeof(long) );
        fread(*list_primes, sizeof(long), *n_primes, f);
        fclose(f);
        f = NULL;
        return 1;
        }


        int save_to_disk(long* size, long* n_primes, long** list_primes) {
        FILE* f = fopen("primes.dat", "w");
        if (f == NULL)
        return 0;
        fwrite(size, sizeof(long), 1, f);
        fwrite(n_primes, sizeof(long), 1, f);
        fwrite(*list_primes, sizeof(long), *n_primes, f);
        fclose(f);
        f = NULL;
        return 1;
        }


        long find_primes_until(long threshold, long* size, long* n_primes, long** list_primes) {
        if (!load_from_disk(size, n_primes, list_primes)) {
        *size = 4096;
        *n_primes = 0;
        *list_primes = (long*)malloc((*size) * sizeof(long));
        memset(*list_primes, 0, (*size) * sizeof(long));

        if (threshold > 2) {
        (*list_primes)[(*n_primes)++] = 2;
        } else {
        return *n_primes;
        }

        if (threshold > 3) {
        (*list_primes)[(*n_primes)++] = 3;
        } else {
        return *n_primes;
        }

        if (threshold > 5) {
        (*list_primes)[(*n_primes)++] = 5;
        } else {
        return *n_primes;
        }

        if (threshold > 7) {
        (*list_primes)[(*n_primes)++] = 7;
        } else {
        return *n_primes;
        }
        }


        long prime = (*list_primes)[(*n_primes)-1] + 2;
        while (prime < threshold) {
        //printf("Examining number: %ld / %ld r", prime, threshold);
        if (is_prime(prime, *n_primes, *list_primes)) {
        //printf("nPrime number found: %ldn", prime);
        append_prime(prime, size, n_primes, list_primes);
        }
        prime += 2;
        }
        save_to_disk(size, n_primes, list_primes);

        return *n_primes;
        }


        void find_primes_interval(long start, long end)
        {
        long* list_primes = NULL;
        long size = 0;
        long n_primes = 0;

        find_primes_until(start, &size, &n_primes, &list_primes);

        if ((start & 0x01) == 0)
        start++;
        while (start < end) {
        printf("Examining number: %ld r", start);
        if (is_prime(start, n_primes, list_primes)) {
        printf("nPrime number found: %ldn", start);
        append_prime(start, &size, &n_primes, &list_primes);
        }
        start += 2;
        }
        }


        int main()
        {
        find_primes_interval(START, END);
        return 0;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 21 at 9:58

























        answered Oct 21 at 9:44









        Alejandro Visiedo

        1012




        1012






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205955%2fprogram-that-finds-prime-numbers-in-a-specific-range%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Сан-Квентин

            8-я гвардейская общевойсковая армия

            Алькесар