Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

whats wrong with the code

$
0
0

include <stdio.h>

int main() { int i,j,flag[2000],temp,num1,num2,temp2,val,rept; for(i=1;i<1000;i++) flag[i]=1; flag[1]=0; //the 32 acts as a handle on the number of primes that there can be for(j=2;j<44;j++) {

      //check if the current nummber is prime

      if(flag[j]==1)
       {
                    //now divide rest of the numbers beginning with this prime
                    temp=2*j;
                    while(temp<2000)
                    {
                           flag[temp]=0;
                           temp+=j;
                    }       
        //the numners which havent been crossed are primes
        }


    }
   /* for(i=1;i<1000;i++)
    {
                       if(flag[i]== 1)
                       printf("%d \n",i);
    }
    */
    //printf("\nenter the repetetions");
    scanf("%d",&rept);
    if(rept >1000)
    {
            //printf("incorrect input");
            return 0;
    }
    while(rept !=0)
    {
    //printf("Enter the two numbers \n");

    scanf("%d %d",&num1,&num2);
    temp2=num1+num2;

    if(num1 >1000 || num2>1000)
    {
            //printf("incorrect input");
            return 0;
    }


    val=temp2+1;//since otherwise it would give the answer as 0 when we have a sum leading into a prime
    while(flag[val]!=1)
       val++;
    //when the loop breaks we get the first prime number of our case
    printf( "%d \n",(val-temp2));

    rept--;
    }
    fflush(stdin);
    getchar();
    return 0;

}


Viewing all articles
Browse latest Browse all 40121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>