Pedro Caseiro
Pedro Caseiro

Reputation: 161

Segmentation Fault C++ Sundaram

The main goal of my program is to print all the prime numbers between 2 and a limit. When I run it with limit = 35000 or even limit = 50000 it prints correctly, but when I try to run it with limit = 75000 it gives me a segmentation fault. Any idea why? And is there any thing I could do to get a better performance? (Solution based on the Sundaram Algorithm)

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


#define MAX 1000000



int main() {
    int vetor[MAX];
    int limite;

    scanf("%d", &limite);


    for (int i = 0; i <= limite; i++){
      vetor[i] = i;
    }

    /* Marcar todos os compostos */
    for (int i = 1; i < limite/2; i++){
      for (int j = i;  j< limite/2; j++){
        long aux = i + j + 2 * i * j;
        if(aux <= limite)
          vetor[aux] = 0;
      }
    }

    /* Imprimir os verdadeiros primos, incluíndo o 2 */
    printf("2\n");

    for (int i = 1; i <= limite; i++){
      if (vetor[i] != 0){
        long aux = i*2 + 1;
        if (aux < limite){
          printf("%ld\n",aux);
        }
      }
    }

   return 0;
}

Upvotes: 1

Views: 77

Answers (2)

Prosen Ghosh
Prosen Ghosh

Reputation: 655

Type long (or long int) is an integral type that is larger than or equal to the size of type int .

Type long long Larger than an unsigned long.

Type unsigned long long Larger than long long.

Read this Data Type Ranges.

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

    #define MAX 1000000



    int main() {
        int vetor[MAX];
        int limite;

        scanf("%d", &limite);


        for (int i = 0; i <= limite; i++){
          vetor[i] = i;
        }

        /* Marcar todos os compostos */
        for (int i = 1; i < limite/2; i++){
          for (int j = i;  j< limite/2; j++){
            unsigned long long aux = i + j + 2 * i * j;
            if(aux <= limite)
              vetor[aux] = 0;
          }
        }

        /* Imprimir os verdadeiros primos, incluíndo o 2 */
        printf("2\n");

        for (int i = 1; i <= limite; i++){
          if (vetor[i] != 0){
            long aux = i*2 + 1;
            if (aux < limite){
              printf("%ld\n",aux);
            }
          }
        }

       return 0;
    }

Upvotes: 1

fnc12
fnc12

Reputation: 2237

Try this. I changed i, j and aux to 'unsigned long long'. Your problem is overflow. It means your integer number became greater than maximum integer value. Hopefully int is not the largest integer data type. The largest is unsigned long long. But anyway remember that there is stil limit when you will meet this problem.

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


#define MAX 1000000



int main() {
    int vetor[MAX];
    int limite;

    scanf("%d", &limite);


    for (int i = 0; i <= limite; i++){
        vetor[i] = i;
    }

    /* Marcar todos os compostos */
    for (unsigned long long i = 1; i < limite/2; i++){    
        for (unsigned long long j = i;  j< limite/2; j++){
            unsigned long long aux = i + j + 2 * i * j;
            if(aux <= limite)
                vetor[aux] = 0;
        }
    }

    /* Imprimir os verdadeiros primos, incluíndo o 2 */
    printf("2\n");

    for (int i = 1; i <= limite; i++){
        if (vetor[i] != 0){
            long aux = i*2 + 1;
            if (aux < limite){
                printf("%ld\n",aux);
            }
        }
    }

    return 0;
}

Upvotes: 3

Related Questions