need help with Goldbach's Conjecture!
Mar 20, 2008 at 2:29am Mar 20, 2008 at 2:29am UTC
I can't seem to get this code to work:
it needs to find the sum of two prime numbers, and I need to do it it without any scanf or printf implements. Thanks to anyone who can help me out! XD
Here's what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include<iostream>
using namespace std;
bool is_divisible(int , int );
bool is_prime(int );
void main()
{
int n;
int p1, p2;
cout << "\n Enter a number: " ;
cin >> n;
for (p1=2, p2=2; p1 <= n; p1++)
if ((is_prime(p1)) && (is_prime(n-p1)))
cout << n << " = " << (is_prime(p1)) << " + " << (is_prime(p2));
cout << "\n\n" ;
}
bool is_divisible (int x, int y)
{
if (x%y == 0)
return true ;
else
return false ;
}
bool is_prime(int a)
{
for (int i = a/2; i >1; i--)
if (is_divisible(a, i))
return false ;
return true ;
}
Mar 20, 2008 at 8:09am Mar 20, 2008 at 8:09am UTC
I have made a few minor changes to the for loop
1 2 3 4 5 6 7
for (p1=2; p1 <= n; p1++)
{
if ((is_prime(p1)) && (is_prime(n-p1)))
{
cout << n << " = " << p1 << " + " << n-p1 << endl;
}
}
Topic archived. No new replies allowed.