You are not logged in. Please login at www.codechef.com to post your questions!

×

really need help

If you like numbers, you may have been fascinated by prime numbers. Sometimes we obtain by concatenating two primes. For example, concatenating 2 and 3, we obtain the prime 23. The aim is to find all such distinct "concatenated primes" that could be obtained by concatenating primes ≤ a given integer N. Input Format:

Integer N Output Format:

M, the number of distinct primes that could be obtained by concatenating two primes ≤ N Constraints:

N ≤ 70

Example 1

Input 10

Output 4

Explanations The primes ≤ 10 are 2, 3, 5, 7. These can be used to form the following concatenated numbers: 22, 23, 25, 27, 32, 33, 35, 37, 52, 53, 55, 57, 72, 73, 75, 77. Of these, there are four primes: 23 37 53 and 73. Hence the output is 4.

Example 2

Input 20

Output 17

Explanation The prime numbers up to 20 are 2 3 5 7 11 13 17 and 19.

Concatenating these two at a time in all possible ways, we get the following numbers:

22 23 25 27 211 213 217 219 32 33 35 37 311 313 317 319 52 53 55 57 511 513 517 519 72 73 75 77 711 713 717 719 112 113 115 117 1111 1113 1117 1119 132 133 135 137 1311 1313 1317 1319 172 173 175 177 1711 1713 1717 1719 192 193 195 197 1911 1913 1917 1919

We have the following 17 primes numbers in this list: 23 37 53 73 113 137 173 193 197 211 311 313 317 719 1117 1319 1913 Hence the output would be 17.

and my code is following but it says wrong ans can anyone tell me where it is failing

include<stdio.h>

int x[100]={0}; int main() {

int N, i, j, isPrime, n,p=0,c=0,t=0,k,f=0;

// printf("To print all prime numbers between 1 to N\n"); // printf("Enter the value of N\n"); scanf("%d",&N);

if(N<=70) {

// printf("Prime numbers between %d to %d\n", 1, N);

for(i = 2; i <= N; i++){
    isPrime = 0;

    for(j = 2; j <= i/2; j++){

         if(i % j == 0){
             isPrime = 1;
             break;
         }
    }

    if(isPrime==0 && N!= 1){
      x[p]=i;
      printf("%d ",x[p]);
      p++;
    }

}
p=0;
while(x[p]>0)
{
    c++;
    p++;
}
for(i=0;i<c;i++)
{
    for(j=0;j<c;j++)
    {
        if(x[j]/10==0)
        t=x[i]*10+x[j];
        else if(x[j]/10>0)
                t=x[i]*100+x[j];
        printf("\n--%d",t);
        for(k=2;k<t/2;k++){
            isPrime=0;
        if(t%k==0){
                isPrime=1;
        break;
        }


    }
    if(isPrime==0)
        f++;

}

printf("\n%d",f);

}

printf("\n%d",f);

} return 0; }

asked 29 Jul '17, 07:45

reebhu1206's gravatar image

0★reebhu1206
1
accept rate: 0%

Seems like a probable contest problem, can you please share a link to the problem and your code instead typing it out here? Apart from proving that this isn't a running contest problem, it also helps make the code be more readable.

(29 Jul '17, 07:51) ista2000 ♦4★

If anything, start by learning Sieve of Eratostenes. This for-loop is incorrectly optimized:

for(j = 2; j <= i/2; j++){

     if(i % j == 0){
         isPrime = 1;
         break;
     }
}
link

answered 29 Jul '17, 08:02

liaojh's gravatar image

5★liaojh
1825
accept rate: 7%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,739
×78
×71
×35

question asked: 29 Jul '17, 07:45

question was seen: 862 times

last updated: 29 Jul '17, 08:02