Code: Select all
generate a list of primes p
for(i = 0; ; i++)
break if you find n - p[i] in a binary search of p
Moderator: Board moderators
Code: Select all
generate a list of primes p
for(i = 0; ; i++)
break if you find n - p[i] in a binary search of p
Code: Select all
void findGold (vi &a, int &x, int &y, int e){
for(x = 0; ; x++) {
for(y = x; y < a.size() && a[x] + a[y] < e; y++);
if(a[x] + a[y] == e)
return;
}
}
Code: Select all
y= lower_bound(a.begin(), a.end(), e)-a.begin()+1;
if (y>=a.size())
y=a.size()-1;
getting TLE!! what is the wrong
Code: Select all
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
long int num1;
long int num2;
long int g=0;
bool prime_num[1100002];
long calculate_prime(long int p)
{
for(long int k=4;k<=p;k+=2)
{
prime_num[k]=true;
}
for(long int i=3;i<=(int)sqrt(p);i+=2)
{
for(long int k=(i*i);k<=p;k+=2*i)
{
prime_num[k]=true;
}
}
for(long int i=2;i<p;i++)
{
if(prime_num[i]==false && prime_num[p-i]==false)
{
num2=i;
num1=p-i;
g++;
break;
}
}
}
void show(int p)
{
if(g==0)
{
printf("Goldbach's conjecture is wrong. \n");
}
else
{
printf("%d = %d + %d\n",p,num2,num1);
g=0;
}
}
int main()
{
long int p;
while(scanf("%d",&p)==1)
{
if(p==0)
break;
else
{
calculate_prime(p);
show(p);
}
}
return 0;
}
Thanks for sharing. This is the approach that was the most clear to me - especially since I know that the code I wrote when I first thought of this would result in a TL.Bluefin wrote:I do not look throught your code, but my algorithm is like this:
(1) use sieve of Eratosthnenes to find all the prime numbers up tp 1000000
(2) add the smallest prime number and the largest prime number which is smaller than n
(3) if the sum is equal to n, print them, else continue step 2
Code: Select all
999968
6
568
43422
989838
46
10
999998
748386
8
0
Code: Select all
999968 = 7 + 999961
6 = 3 + 3
568 = 5 + 563
43422 = 11 + 43411
989838 = 7 + 989831
46 = 3 + 43
10 = 3 + 7
999998 = 19 + 999979
748386 = 7 + 748379
8 = 3 + 5
Code: Select all
#include<stdio.h>
#include<math.h>
int p(int n);
int main(void)
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i=0;
if(n==0) return 0;
if(n>=6 && n<1000000)
{
int as=1;
for(i=3;i<=(int)sqrt(n)+1;i=i+2)
{
int k=0;
int j=p(i);
if(j==2)
{
k=p(n-i);
if(k==2)
{
printf("%d = %d + %d\n",n,i,n-i);
as=0;
break;
}
}
}
if(as==1) printf("Goldbach\'s conjecture is wrong.\n");
}
}
return 0;
}
int p(int n)
{
int i;
int flag=3;
for(i=2;i<=(int)sqrt(n)+1;i=i+1)
{
if(n%i==0)
{
flag=0;
return 3;
break;
}
}
if(flag!=0&&n>1)
{
return 2;
}
else if(n<=1)
{
return 3;
}
}
]
Right. So if you're sharing code and do use code tags at least make sure they're actually being used. How legible do you think your code looks in the post above?sampad74 wrote:i got wa.here is my code...
Code: Select all
return 0;
308 = 31 + 277brianfry713 wrote:Code: Select all
generate a list of primes p for(i = 0; ; i++) break if you find n - p[i] in a binary search of p
Code: Select all
#include<stdio.h>
#include<math.h>
int p[100000];
int h;
int prime(int n)
{
int i=0,j=0,k=0,m=0;
for(i=2;i<n;i++)
{
m=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
m++;
break;
}
}
if(m==0)
{
p[k]=i;
k++;
}
}
}
int main()
{
int i,j,n,a=0,b=0;
//freopen("63f.txt","r",stdin);
while(scanf("%d",&n)==1)
{
if(n==0)
break;
a=0,b=0;
if(n%2==0)
{
prime(n);
i=0;
while(p[i])
{
//printf("%d ",p[i]);
i++;
}
h=i;
for(i=0;i<h;i++)
{
for(j=h-1;j>=0;j--)
{
if(p[i]+p[j]==n)
{
a=p[j];
b=p[i];
break;
}
}
}
}
if(a>0&&b>0)
{
printf("%d = %d + %d\n",n,a,b);
}
else
printf("Goldbach's conjecture is wrong.\n");
}
return 0;
}
brianfry713 wrote:Code: Select all
generate a list of primes p for(i = 0; ; i++) break if you find n - p[i] in a binary search of p