Hi guys,
I'm working on a program that has to do with Goldbach's second conjecture (any even number larger than 5 can be expressed as the sum of three prime numbers). I have to write a program that takes integer n (larger than or equal to 6) and finds three prime numbers p1, p2 and p3. The equation result is n = p1+p2+p3 and I have to find all possible combinations of said primes as well.
I've written somewhat of a code so far, can anyone help me with this problem? It would be greatly appreciated. Thanks guys :)

// goldbach2.cpp
// Author : Elspeth Eastman
// March 18th, 2008

#include <iostream>
using namespace std;

bool is_prime (int); // letting compiler know about the certain function

#include <cmath>

void main (void)
{
	int n; // the even number
	int p1, p2, p3; // the two prime numbers
	int prime = is_prime(n);
	p1=n;
	p2=2;
                p3=n+2;


	cout << "Enter an even integer number (larger than 5) : ";
	cin >> n; 

    for (p1=2; p1<=n; p1++) // for statement repeatedly checks p1 until it is true
	{((p1!=n) && !(p1/=n)); // checks whether the numbers are prime
	  cout << n << " = " << is_prime(p1) << " + " << is_prime(n-p1) << is_prime(p3) << endl;//displays the end result of two prime numbers
	  
	}
}

bool is_prime (int x) // declaring the functions
{
int num;

if (!(x%2)) // check whether or not x is even
{
	return (x==2);
}
	for (num=3; num<x; num++)
	{
		if (!(x % num)) 
			return 0;
	}
	return 1; 
}

Recommended Answers

All 4 Replies

It's generally considered rude to post your code and say "please help". Such a request is far too vague. Perhaps you could ask a specific question or describe exactly what you're having trouble with.

Ah, I gotcha. ;)
Well, let's see. For some reason I keep getting the warning that 'n' is being used without being defined. But the thing is, it IS being defined by the user's input. I'm also having trouble with the
is_prime function. I think I may have the idea of how to get it, but something tells me it's probably not working the way I want it to.
Also, not quite sure whether to put (is_prime)(p1) or (is_prime)(n-p2) in the cout command.
It's pretty messy, really, now that I look at it. I should have cleaned it up before posting. :/

>it IS being defined by the user's input
But you use it before it's defined by the user's input. That's probably what your compiler is complaining about. Everywhere you use n before filling it with input, the value is indeterminate.

Ha ha I go to iusb too, betchya can't guess who i am Elspeth!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.