## 543 - Goldbach's Conjecture

Moderator: Board moderators

felipej
New poster
Posts: 2
Joined: Tue Mar 06, 2012 5:35 pm

### 543 - WA "Goldbach's"

i can't see where i'm getting wa... can any 1 help?

code:

Code: Select all

``````GOT AC
``````
thanks...
Last edited by felipej on Wed Mar 07, 2012 6:27 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 543 - WA "Goldbach's"

14372=3+14369
Check input and AC output for thousands of problems on uDebug!

felipej
New poster
Posts: 2
Joined: Tue Mar 06, 2012 5:35 pm

### Re: 543 - WA "Goldbach's"

thanks a lot! found bug and got AC!

Honour_00
New poster
Posts: 4
Joined: Tue Jul 17, 2012 4:04 pm

### 543 why TLE?help plz

here is my code

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
long long pr[1000080],T;
bool isp[1000080];
void prime()
{
long long i,j;
T=0;
for(i=2;i<1000000;i++)
{
if(isp==0)
{
for(j=i*i;j<1000000;j=j+i)
{
isp[j]=1;
}
pr[T]=i;
T++;
}
}
}
int main()
{
prime();
long long a,s,d,f,g,h,k,l;
while(scanf("%lld",&a)==1)
{
if(a==0) break;
else
{
s=1;
for(d=T-1;d>=0;d--)
{
if(pr[d]>=a) continue;
for(f=0;f<d;f++)
{
if(a==pr[d]+pr[f])
{
s=0;
printf("%lld = %lld + %lld\n",a,pr[f],pr[d]);
break;
}
}
if(s==0) break;
}
if(s==1)
{
printf("Goldbach's conjecture is wrong.\n");
}
}
}
return 0;
}

plz help

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 543 why TLE?help plz

Your code is too slow. Try stopping the inner loop when pr[d]+pr[f]>a
Check input and AC output for thousands of problems on uDebug!

goldenbird299
New poster
Posts: 5
Joined: Sun Aug 05, 2012 6:56 am

### Re: 543-goldbach's conjecture..RE??

hi everyone
can you help me with this code?
it gets RE from judge >_<

Code: Select all

``Removed after AC``
Last edited by goldenbird299 on Thu Aug 16, 2012 7:18 am, edited 3 times in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 543-goldbach's conjecture..WHY AM I GETTING TLE??

Try 999998
Check input and AC output for thousands of problems on uDebug!

goldenbird299
New poster
Posts: 5
Joined: Sun Aug 05, 2012 6:56 am

### Re: 543-goldbach's conjecture..WHY AM I GETTING TLE??

OMG!
what a mistake!
i thought n < 100000 !!
Thanks Brian

gr81
New poster
Posts: 46
Joined: Wed Sep 26, 2012 7:52 pm

### Re: 543-goldbach's conjecture..WHY AM I GETTING TLE??

getting WA, please have a look at my code...at http://ideone.com/dR8w5y

gr81
New poster
Posts: 46
Joined: Wed Sep 26, 2012 7:52 pm

### Re: 543-goldbach's conjecture..WHY AM I GETTING TLE??

got AC, i forgot if ( n < 4 )...would not get processed...

kier.guevara
New poster
Posts: 30
Joined: Thu Jul 19, 2012 11:24 pm

### Re: 543-goldbach's conjecture..WHY AM I GETTING TLE??

Code: Select all

``````#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

void primeNumbers(vector <int> &prime, int n)
{
short array[1000000] = {0};

for(int i = 2; i <= sqrt(n); i++)
{
if(array[i] == 0)
{
for(int k = i+1; k < n; k++)
{
if(k % i == 0)
array[k] = 1;
}
}
}

for(int i = 3; i < n; i++)
{
if(array[i] == 0)
prime.push_back(i);
}
}

void gold(vector <int> &prime, int n)
{

for(int i = prime.size() - 1; i >= prime.size()/2; i--)
{
for(int k = 0; k <= prime.size()/2; k++)
{
if(prime[i] + prime[k] == n)
{
cout << n << " = " <<  prime[k] << " + " << prime[i] << endl;
return;
}
if(prime[i] + prime[k] > n)
break;
}
}
cout << "Goldbach's conjecture is wrong." << endl;
}

int main()
{
vector <int> prime;
int n;

while(cin >> n)
{
if(n == 0)
break;

gold(prime,n);

}

return 0;
}
``````
Im getting TLE. Is there any way I can make my code efficient?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 543-goldbach's conjecture..WHY AM I GETTING TLE??

http://www.algorithmist.com/index.php/UVa_543
First use the Sieve of Eratosthenes or hard code a list of primes.
Don't regenerate a list of primes for every input. Also make that prime list global.
Check input and AC output for thousands of problems on uDebug!

hansyulian
New poster
Posts: 3
Joined: Sat Dec 22, 2012 1:30 pm

### 543 - WA

What is wrong with my code?

Code: Select all

``````#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

bool prima[1000005];

int main()
{
memset(prima,true,sizeof(prima));
prima[0] = false;
prima[1] = false;
for (long i = 2; i < 1000000; i++)
if (prima[i])
{
long j = 2*i;
while (j < 1000000)
{
prima[j] = false;
j+=i;
}
}
long n;
scanf("%ld",&n);
while (n)
{
long i = 2;
while (!ada && i < n/2)
{
if (prima[i] && prima[n-i])
{
printf("%ld = %ld + %ld\n",n,i,n-i);
}
i++;
}
if ( !ada) printf("Goldbach's conjecture is wrong.\n");
scanf("%ld",&n);
}
return 0;
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 543 - WA

6 = 3 + 3
Check input and AC output for thousands of problems on uDebug!

Yusif
New poster
Posts: 27
Joined: Tue Jun 25, 2013 2:24 am

### Re: 543-goldbach's conjecture..WHY AM I GETTING TLE??

I'm getting TL but I cant get why

Code: Select all

``AC``
Last edited by Yusif on Tue Nov 05, 2013 10:38 pm, edited 1 time in total.