Thread: Goldbach's Conjecture problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    7

    Goldbach's Conjecture problem

    I just started using C++ no more than a week ago. I have to write a program that solves Goldbach's conjecture (Wikipedia provides a simple overview if you don't know what this is), but since my program will be dealing mostly will smaller values, speed and efficiency aren't of the highest importance. I have found codes that are already written online, but I need to come up with one of my own. So far, this is what I have written:


    Code:
    #include <iostream> 
    using namespace std; 
     
    int main () {
     
        long int x; 
        int a,b;
     
        cout << "Enter the number: ";
        cin >> x; 
        cout << endl; 
     
        if (x<=2) {
     
        cerr << "Goldbach's conjecture only works for even integers greater than 2. ";
        cout << endl;
        cout << endl;
     
        return 0; 
    } 
     
        if ((x%2)!=0) {
        cerr << "Goldbach's conjecture only works for even integers. ";
        cout << endl; 
        cout << endl;
     
        return 0;
    }
     
         for (a=2; a<=x; a++)
        {
            for (b=2; b<=x; b++)
        {
     
            if ((x>2) && ((x%2)==0) && (a<=b) && (a+b==x)){
            cout << a << " and " << b << endl;
     
            }
    }
    }
    return 0; 
    }
    This program takes the value that is entered and splits it into as many pairs as possible that add up to the original input. Is there a way for me to limit this so that only the pairs containing two primes appear? Or if this is not possible, what else should I do? Remember, this doesn't have to very fast or that efficient.

    Thanks for reading.
    Last edited by Salem; 08-01-2011 at 02:03 AM. Reason: restored original post for context

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collatz's Conjecture
    By KAUFMANN in forum C Programming
    Replies: 15
    Last Post: 06-29-2011, 07:01 PM
  2. Goldbach's Conjecture
    By cashmerelc in forum C Programming
    Replies: 7
    Last Post: 07-19-2010, 10:41 PM
  3. Goldbach's conjecture
    By dcwang3 in forum C Programming
    Replies: 10
    Last Post: 10-14-2008, 01:34 AM
  4. Replies: 3
    Last Post: 11-13-2005, 02:53 AM
  5. Goldbach Conjecture
    By StarOrbs in forum C++ Programming
    Replies: 19
    Last Post: 03-17-2005, 04:42 PM