MY name is ruhul amin

ISLAMIC UNIVERSITY OF KUSHTIA

Tuesday, March 5, 2013

uva solve problem 10311

#incluide<stdio.h>
#include<math.h>
int N = 100000002;
bool status[100000002];
long prime[8000000];
int search(long value,int left,int right)
{
    int mid;
    if(right<left)
    return right;
    mid=floor((left+right)/2);
         if(prime[mid]==value)
         return mid;
         if(prime[mid]>value)
          search(value,left,mid-1);
        else
          search(value,mid+1,right);

}

int main()
{
    long i, j, sqrtN,k=0;
    status[0]=1;status[1]=1;
    for( i = 2; i <= N; i++ )
    status[i] = 0;
    prime[0]=2;
            k++;
    sqrtN = long( sqrt((double)N) ); // have to check primes up to (sqrt(N))
    for( i = 3; i <= sqrtN; i += 2 )
    {
        if( status[i] == 0 )
        {
            prime[k]=i;
            k++;
            for( j = i * i; j <= N; j += i + i )
            status[j] = 1; // status of the multiple is 1
        }
    }
    for(;i<100000000;i+=2)
    if( status[i] == 0 )
    {
        prime[k]=i;
        k++;
    }

    // printf("%ld\n",k);
   long n,p,q,m,l=k-1,a,b;

   while(scanf("%ld",&n)!=EOF)
   {
    k=0;
    if(n%2==1)
    {
        m=n-2;
        if(status[m]==0&&m>1)
        {
            b=m;a=2;
            k=1;
        }
    }
    else
    {
    p=search(n/2,0,l);
    if(p>=0)
    {
      while(prime[p]<n)
      {
           m=n-prime[p];

        if((status[m]==0&&m%2!=0)||m==2)
        {
            if(m!=prime[p])
            {
                k=1;
                b=prime[p];a=m;
                break;
            }
        }
        p=p+1;
      }
    }
   }
      if(k==0)
      printf("%ld is not the sum of two primes!\n",n);
      else
      {
          if(a>b)
          swap(a,b);
          printf("%ld is the sum of %ld and %ld.\n",n,a,b);
      }

   }

    return 0;
}

No comments:

Post a Comment