Superman
Superman

Reputation: 105

Implementing Goldbach's Conjecture in C code

#include <stdio.h>

int prime(int num);

int main() {
    int upper, lower, tempL, x;

    printf("Enter lower limit:");
    scanf("%d", &lower);

    printf("Enter upper limit:");
    scanf("%d", &upper);

    for (lower; lower <= upper; lower + 2) {
        tempL = lower;
        for (lower; lower != 0; lower--) {
            if (prime(lower) == 0) { //after decrementing we find a prime
                x = tempL - (lower); x = the original lower limit minus the current decremented lower limit
                if (prime(x) == 0) { //if x if prime do following
                    printf("%d = %d + %d\n", tempL, lower, x); //print
                    break;
                }
            }
        }
    }
}

int prime(int number) { //returns 0 if number is prime
    int i;
    for (i = 2; i < number / 2; i++) {
        if (number % i == 0) {
            return 1;
        }
    }
    return 0;
}

This is what I have so far... all it does is endlessly print

999983 = 999983 + 0

I really am stuck here. I have been trying to get this for the past 8 hours.

I am trying to use Goldbach's Conjecture to print out numbers as sums of primes. Once it prints number n it should move onto n+1 until it reaches the upper limit.

Now I have this

#include <stdio.h>

int prime(int num);

int main() {
    int upper, lower, tempL, x;

    printf("Enter lower limit:");
    scanf("%d", &lower);

    printf("Enter upper limit:");
    scanf("%d", &upper);

    for (lower; lower <= upper; lower + 2) {
        tempL = lower;
        for (tempL; tempL != 0; tempL--) {
            if (prime(tempL == 0) { //after decrementing we find a prime
                x = lower - (tempL); x = the original lower limit minus the current decremented lower limit
                if (prime(x) == 0) { //if x if prime do following
                    printf("%d = %d + %d\n", tempL, lower, x); //print
                    break;
                }
            }
        }
    }
}          

int prime(int number) { //returns 0 if number is prime
    int i;
    for (i = 2; i < number / 2; i++) {
        if (number % i == 0) {
            return 1;
        }
    }
    return 0;
}

But I still get the same issue

Upvotes: 3

Views: 4360

Answers (2)

chqrlie
chqrlie

Reputation: 144951

There are multiple problems in your code:

  • You do not check the return value of scanf, leading to undefined behavior.
  • You do not increment lower in the loop for (lower; lower <= upper; lower + 2). Write this instead:

    for (; lower <= upper; lower += 2)
    
  • You misplaced the parentheses in if (prime(tempL == 0), it should be:

    if (prime(tempL) == 0)
    
  • There is a missing // after x = lower - (tempL);

  • your function prime should return non zero to indicate truth and zero to indicate falsehood. It would also be more readable to call it isprime()

Here is a corrected version:

#include <stdio.h>

int isprime(int number) { //returns non zero if number is prime
    for (int i = 2; i * i <= number; i++) {
        if (number % i == 0) {
            return 0;
        }
    }
    return 1;
}    

int main(void) {
    int upper, lower;

    printf("Enter lower limit: ");
    if (scanf("%d", &lower) != 1)
        return 1;

    printf("Enter upper limit: ");
    if (scanf("%d", &upper) != 1)
        return 1;

    for (int n = lower; n <= upper; n += 2) {
        for (int i = 1; i <= n / 2; i++) {
            if (isprime(i) && isprime(n - i)) {
                printf("%d = %d + %d\n", n, i, n - i);
                break;
            }
        }
    }
    return 0;
}          

Upvotes: 0

gnasher729
gnasher729

Reputation: 52592

There is a fatal error:

for(lower;lower<=upper; lower+2)

If you use a decent compiler and turn all warnings on, the compiler will tell you what's wrong. I'll tell you instead: lower+2 just evaluates the expression lower+2 which has no side effects. It doesn't modify lower. You probably wanted it to increase lower by 2, it doesn't.

There may be more errors, I stopped looking at the first one.

Oh well, I looked at the code again and found this:

if( prime(tempL == 0 )  //after decrementing we find a prime

tempL == 0 is an expression which compares tempL and 0; the result is 1 if tempL = 0 and 0 otherwise. So you call either prime (1) or prime (0). In both cases, the prime () function returns 0, so the if is never executed.

Oh, I've done it again...

Your function "prime" returns exactly the wrong value. It returns 0 if a number is prime, and 1 if it isn't. Except if number is from 0 to 4, where it always returns 0. Which is wrong for number = 2 or 3, but correct for 1 and 4...

Upvotes: 4

Related Questions