0

I am fairly new to the C language and I wanted to try and would like to find as many primes I can using the sieve of eratosthenes however when I try to do numbers bigger than 2000000(two million) I get a segmentation error. Which I assume is from the array that I created since before when I was doing prints sometimes it was going outside of int and returning absurd values in my debuggings however this does not affect the code when i use smaller numbers. Is there somtehing that I am doing wrong or did I got to the limit of arrays. Is there a solution to this problem.

#include <stdio.h>
#include <assert.h>
#include <time.h>

int slow_prime_finder(int n)
{
    int i;
    for (i = 2; i * i <= n; i++)
        if (n % i == 0)
            return 0;
    return 1;
}

int premier(int n) // 6k+-1 optimisiation
{
    if (n == 2 || n == 3)
        return 1;
    if (n <= 1 || n % 2 == 0 || n % 3 == 0)
        return 0;
    for (int i = 5; i * i <= n; i += 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return 0;
    return 1;
}

void affichageNombresPremiers(int n_max)
{
    int est_prime[n_max + 1], i, a, b, c;
    for (i = 0; i <= n_max; i++)
        est_prime[i] = i; // initialise the array
    for (a = 2; a * a <= n_max; a++)
        if (a <= n_max)
        {
            if (est_prime[a])
            {
                for (b = a * a; b <= n_max; b += a)
                    if (b <= n_max)
                        est_prime[b] = 0; /*printf("%d, %d\n", est_prime[b], b);*/
            }
        } //*if you want to see some out of array printing remove the comment in the print. I dont know how it goes outside of the array and the for limit but somehow it does*/
    for (c = 2; c <= n_max; c++)
        if (est_prime[c])
            printf("%d, ", c);
    printf("\n");
}

int main()
{
    // ex7
    clock_t t;
    double cpu_time_used;

    int i, n_max = 2000000; // if i go bigger i have a segmentation fault
    t = clock();
    for (i = 2; i <= n_max * 1000; i++)
        if (slow_prime_finder(i))
            printf("%d, ", i);
    t = clock() - t;
    double time_taken_slow = ((double)t) / CLOCKS_PER_SEC;
    t = clock();
    for (i = 2; i <= n_max; i++)
        if (premier(i))
            printf("%d, ", i);
    t = clock() - t;

    double time_taken = ((double)t) / CLOCKS_PER_SEC;
    t = clock();
    affichageNombresPremiers(n_max);

    t = clock() - t;
    printf("Slow prime finder time: %f\n", time_taken_slow);
    printf("6k+-1 optimisation time: %f\n", time_taken);
    double time_taken1 = ((double)t) / CLOCKS_PER_SEC;
    printf("Sieve of eras time: %f\n", time_taken1);
    return 0;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Erdi
  • 1
  • 2
    `int est_prime[n_max+1]` is too large for the stack when `n_max` is very large. You could use `malloc` instead or use a smaller type since your code doesn't seem to use the value stored in the array for anything other than flag. You might consider not putting a ton of code all on one line. It makes it harder to read, especially when posted here. – Retired Ninja Jan 26 '22 at 00:22
  • 1
    Fwiw, measuring clock time with console IO-intense operations is utterly pointless. – WhozCraig Jan 26 '22 at 00:27

0 Answers0