Error Segmentation fault(core dumped) in C

Asked

Viewed 173 times

0

When trying to calculate which prime can make a division of rest 0 with the number 600851475143 I came across the error segementation fault(core dumped) (although I used the sieve of Eratosthenes for the calculation of primes). I had already solved this problem in other algorithms, but I’m not getting on this.

I believe that the use of dynamic allocation will be necessary, but I did not do very well with it. It follows original code.

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

int main()
{
  int count_1 = 0, count_2 = 0, soma = 0, maior = 0;
  int numero_maximo[600851475144];

  for (count_1 = 2; count_1 <= 600851475143; count_1++)
  {
    numero_maximo[count_1] = count_1;
  }

  for (count_1 = 2; count_1 <= 600851475143; count_1++)
  {
    if (numero_maximo[count_1] == count_1)
    {
      if (600851475143 % count_1 == 0)
      {

        if (count_1 > maior)
        {
          maior = 0;
          maior = count_1;
        }
      }

    }
  }
  printf("%d\n", maior);

  return 0;
}
  • 1

    An int does not hold the number 600851475143. Search for other data types. Maximum acceptable value for an int: 2147483647.

  • 1

    get a machine with more than 600 GB of RAM :) , or create a swap file (for virtual memory) of more than 600 GB of RAM (600GB is more or less the size of your "numero_maximo" array)... ops, and need to put as global variable, I don’t think any system in the world will have a 600GB stack run...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.