re: Goldbach Conjecture And A Simple Approach in C VIEW POST


Your code probably does its job (haven't tried it) but I suggest some refactorings:

  • You can easily avoid the global variables primes and j by declaring them inside main. Yes, you will have to pass additional arguments to goldbach but that's fine.

  • It is hard to understand what the meaning of j is. You should use a more expressive name like last_prime_index.

  • In is_prime you declare another variable j which shadows the global j. This can easily cause errors.

  • In is_prime, instead of j < n/2, you should check if j <= sqrt(n). This will make your code much more performant for larger numbers.

  • In is_prime, instead of declaring a flag, you could simply return 0 or 1.

  • In goldbach, the flag variable is not used.

  • The goldbach function does two different things, calculation and output. It could be split into two separate functions.

  • You should check if j < 100000 before incrementing it.

code of conduct - report abuse