Getting TLE in hackerearth

Multi tool use
Multi tool use











up vote
-4
down vote

favorite












When i submiting this code at hackerearth i am getting TLE.



Any suggestions how can i optimise this
Code.



#include <stdio.h>
#include <stdlib.h>

int checkPrime(int);

int main() {
int a, b,
reminder,sum=0,n;

scanf("%d %d",&a,&b);

while(a <= b) {
n = a;
sum = 0;
reminder = 0;

while (n > 0) {
reminder = n % 10;
sum += reminder;
n = n / 10;
}

if(sum > 1 && checkPrime(sum) == 1 && checkPrime(a) == 1) {
printf("%d ",a);
}

++a;
}

return 0;
}

int checkPrime(int p) {

int i,flag=1;

for(i=2; i <= p / 2; i++){
if(p%i == 0) {
flag = 0;
break;
}
}

return flag;

}


Here is the problem i coded for



And how can i analysis this code and get
Time complexity.










share|improve this question






















  • and what is TLE ?
    – u__
    Nov 8 at 9:21






  • 1




    @u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
    – Weather Vane
    Nov 8 at 9:22










  • Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
    – Willem Van Onsem
    Nov 8 at 9:23






  • 1




    There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
    – Weather Vane
    Nov 8 at 9:24












  • Please give your question a more descriptive title, so that other users with a similar problem can find it.
    – m69
    Nov 9 at 3:26















up vote
-4
down vote

favorite












When i submiting this code at hackerearth i am getting TLE.



Any suggestions how can i optimise this
Code.



#include <stdio.h>
#include <stdlib.h>

int checkPrime(int);

int main() {
int a, b,
reminder,sum=0,n;

scanf("%d %d",&a,&b);

while(a <= b) {
n = a;
sum = 0;
reminder = 0;

while (n > 0) {
reminder = n % 10;
sum += reminder;
n = n / 10;
}

if(sum > 1 && checkPrime(sum) == 1 && checkPrime(a) == 1) {
printf("%d ",a);
}

++a;
}

return 0;
}

int checkPrime(int p) {

int i,flag=1;

for(i=2; i <= p / 2; i++){
if(p%i == 0) {
flag = 0;
break;
}
}

return flag;

}


Here is the problem i coded for



And how can i analysis this code and get
Time complexity.










share|improve this question






















  • and what is TLE ?
    – u__
    Nov 8 at 9:21






  • 1




    @u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
    – Weather Vane
    Nov 8 at 9:22










  • Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
    – Willem Van Onsem
    Nov 8 at 9:23






  • 1




    There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
    – Weather Vane
    Nov 8 at 9:24












  • Please give your question a more descriptive title, so that other users with a similar problem can find it.
    – m69
    Nov 9 at 3:26













up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











When i submiting this code at hackerearth i am getting TLE.



Any suggestions how can i optimise this
Code.



#include <stdio.h>
#include <stdlib.h>

int checkPrime(int);

int main() {
int a, b,
reminder,sum=0,n;

scanf("%d %d",&a,&b);

while(a <= b) {
n = a;
sum = 0;
reminder = 0;

while (n > 0) {
reminder = n % 10;
sum += reminder;
n = n / 10;
}

if(sum > 1 && checkPrime(sum) == 1 && checkPrime(a) == 1) {
printf("%d ",a);
}

++a;
}

return 0;
}

int checkPrime(int p) {

int i,flag=1;

for(i=2; i <= p / 2; i++){
if(p%i == 0) {
flag = 0;
break;
}
}

return flag;

}


Here is the problem i coded for



And how can i analysis this code and get
Time complexity.










share|improve this question













When i submiting this code at hackerearth i am getting TLE.



Any suggestions how can i optimise this
Code.



#include <stdio.h>
#include <stdlib.h>

int checkPrime(int);

int main() {
int a, b,
reminder,sum=0,n;

scanf("%d %d",&a,&b);

while(a <= b) {
n = a;
sum = 0;
reminder = 0;

while (n > 0) {
reminder = n % 10;
sum += reminder;
n = n / 10;
}

if(sum > 1 && checkPrime(sum) == 1 && checkPrime(a) == 1) {
printf("%d ",a);
}

++a;
}

return 0;
}

int checkPrime(int p) {

int i,flag=1;

for(i=2; i <= p / 2; i++){
if(p%i == 0) {
flag = 0;
break;
}
}

return flag;

}


Here is the problem i coded for



And how can i analysis this code and get
Time complexity.







c++ c algorithm primes






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 9:18









md. motailab

186




186












  • and what is TLE ?
    – u__
    Nov 8 at 9:21






  • 1




    @u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
    – Weather Vane
    Nov 8 at 9:22










  • Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
    – Willem Van Onsem
    Nov 8 at 9:23






  • 1




    There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
    – Weather Vane
    Nov 8 at 9:24












  • Please give your question a more descriptive title, so that other users with a similar problem can find it.
    – m69
    Nov 9 at 3:26


















  • and what is TLE ?
    – u__
    Nov 8 at 9:21






  • 1




    @u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
    – Weather Vane
    Nov 8 at 9:22










  • Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
    – Willem Van Onsem
    Nov 8 at 9:23






  • 1




    There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
    – Weather Vane
    Nov 8 at 9:24












  • Please give your question a more descriptive title, so that other users with a similar problem can find it.
    – m69
    Nov 9 at 3:26
















and what is TLE ?
– u__
Nov 8 at 9:21




and what is TLE ?
– u__
Nov 8 at 9:21




1




1




@u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
– Weather Vane
Nov 8 at 9:22




@u__ Time Limit Exceeded. And that's the problem that needs to be solved. The task at face value is usually not so difficult as finding an effiecient solution.
– Weather Vane
Nov 8 at 9:22












Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
– Willem Van Onsem
Nov 8 at 9:23




Typically being able to derive the time complexity for a specific algorithm is very useful to improve it.
– Willem Van Onsem
Nov 8 at 9:23




1




1




There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
– Weather Vane
Nov 8 at 9:24






There are plenty of examples on SO of more efficient prime algorithms. For example, why are you checking every even divisor? 2 is the only even prime. That change alone will halve the run time.
– Weather Vane
Nov 8 at 9:24














Please give your question a more descriptive title, so that other users with a similar problem can find it.
– m69
Nov 9 at 3:26




Please give your question a more descriptive title, so that other users with a similar problem can find it.
– m69
Nov 9 at 3:26












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Your checkprime function takes a lot of runtime. It runs for N/2 operations.



You are running this for all the numbers, so you are running for N*N/2 operations, which is too much.



I would suggest that you use a better method of generating the primes. Take a look at the Sieve of Eratosthenes






share|improve this answer





















  • Even checking all odd numbers (and 2) up to sqrt(N) would probably do the trick. Or all 6n+1 and 6n-1 if you want to be fancy.
    – Max Langhof
    Nov 8 at 9:32












  • Thanks man get accepted sqrt(n) worked #Max Langhof
    – md. motailab
    Nov 8 at 9:46










  • Thanks all for your kindness :)
    – md. motailab
    Nov 8 at 9:47


















up vote
1
down vote













There are some primitive ways like this one for instance, looping through the odd numbers and some more optimizations



int isPrime ( int n )
{
if (n <= 1) return 0; // zero and one are not prime
unsigned int i;
for (i=2; i*i<=n; i++) {
if (n % i == 0) return 0;
}
return 1;
}


Seive is kind of over-kill, in your case ( if you are not taking in to consideration your memory requirements ) because the range could be very very large 1000000 you might want to use some sort of bitmap to generate the Seive.



here is a very loosely written Idea of how to generate and use Seive.



char *seive;

void generate_seive( int n )
{
seive = calloc( 1, n );
if( !seive )
{
printf("E...");
exit(0);
}

// Generate Seive
for( int i = 2; i < n ; i ++)
{
if( seive[i] == 1 )
continue;
// Essentially mark all primes as 0 and
// non-primes as 1's
seive[i] = 0;
for( int j = i + i ; j < n ; j += i )
seive[j] = 1;
}
}

int main()
{
generate_seive(100);

// Iterating through the generated Seive
// should do
for( int i = 2; i < 100 ; i ++ )
if( seive[i] == 0 )
printf("%dn", i);
return 0;
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204676%2fgetting-tle-in-hackerearth%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Your checkprime function takes a lot of runtime. It runs for N/2 operations.



    You are running this for all the numbers, so you are running for N*N/2 operations, which is too much.



    I would suggest that you use a better method of generating the primes. Take a look at the Sieve of Eratosthenes






    share|improve this answer





















    • Even checking all odd numbers (and 2) up to sqrt(N) would probably do the trick. Or all 6n+1 and 6n-1 if you want to be fancy.
      – Max Langhof
      Nov 8 at 9:32












    • Thanks man get accepted sqrt(n) worked #Max Langhof
      – md. motailab
      Nov 8 at 9:46










    • Thanks all for your kindness :)
      – md. motailab
      Nov 8 at 9:47















    up vote
    1
    down vote



    accepted










    Your checkprime function takes a lot of runtime. It runs for N/2 operations.



    You are running this for all the numbers, so you are running for N*N/2 operations, which is too much.



    I would suggest that you use a better method of generating the primes. Take a look at the Sieve of Eratosthenes






    share|improve this answer





















    • Even checking all odd numbers (and 2) up to sqrt(N) would probably do the trick. Or all 6n+1 and 6n-1 if you want to be fancy.
      – Max Langhof
      Nov 8 at 9:32












    • Thanks man get accepted sqrt(n) worked #Max Langhof
      – md. motailab
      Nov 8 at 9:46










    • Thanks all for your kindness :)
      – md. motailab
      Nov 8 at 9:47













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    Your checkprime function takes a lot of runtime. It runs for N/2 operations.



    You are running this for all the numbers, so you are running for N*N/2 operations, which is too much.



    I would suggest that you use a better method of generating the primes. Take a look at the Sieve of Eratosthenes






    share|improve this answer












    Your checkprime function takes a lot of runtime. It runs for N/2 operations.



    You are running this for all the numbers, so you are running for N*N/2 operations, which is too much.



    I would suggest that you use a better method of generating the primes. Take a look at the Sieve of Eratosthenes







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 9:23









    Rishikesh Raje

    5,0261825




    5,0261825












    • Even checking all odd numbers (and 2) up to sqrt(N) would probably do the trick. Or all 6n+1 and 6n-1 if you want to be fancy.
      – Max Langhof
      Nov 8 at 9:32












    • Thanks man get accepted sqrt(n) worked #Max Langhof
      – md. motailab
      Nov 8 at 9:46










    • Thanks all for your kindness :)
      – md. motailab
      Nov 8 at 9:47


















    • Even checking all odd numbers (and 2) up to sqrt(N) would probably do the trick. Or all 6n+1 and 6n-1 if you want to be fancy.
      – Max Langhof
      Nov 8 at 9:32












    • Thanks man get accepted sqrt(n) worked #Max Langhof
      – md. motailab
      Nov 8 at 9:46










    • Thanks all for your kindness :)
      – md. motailab
      Nov 8 at 9:47
















    Even checking all odd numbers (and 2) up to sqrt(N) would probably do the trick. Or all 6n+1 and 6n-1 if you want to be fancy.
    – Max Langhof
    Nov 8 at 9:32






    Even checking all odd numbers (and 2) up to sqrt(N) would probably do the trick. Or all 6n+1 and 6n-1 if you want to be fancy.
    – Max Langhof
    Nov 8 at 9:32














    Thanks man get accepted sqrt(n) worked #Max Langhof
    – md. motailab
    Nov 8 at 9:46




    Thanks man get accepted sqrt(n) worked #Max Langhof
    – md. motailab
    Nov 8 at 9:46












    Thanks all for your kindness :)
    – md. motailab
    Nov 8 at 9:47




    Thanks all for your kindness :)
    – md. motailab
    Nov 8 at 9:47












    up vote
    1
    down vote













    There are some primitive ways like this one for instance, looping through the odd numbers and some more optimizations



    int isPrime ( int n )
    {
    if (n <= 1) return 0; // zero and one are not prime
    unsigned int i;
    for (i=2; i*i<=n; i++) {
    if (n % i == 0) return 0;
    }
    return 1;
    }


    Seive is kind of over-kill, in your case ( if you are not taking in to consideration your memory requirements ) because the range could be very very large 1000000 you might want to use some sort of bitmap to generate the Seive.



    here is a very loosely written Idea of how to generate and use Seive.



    char *seive;

    void generate_seive( int n )
    {
    seive = calloc( 1, n );
    if( !seive )
    {
    printf("E...");
    exit(0);
    }

    // Generate Seive
    for( int i = 2; i < n ; i ++)
    {
    if( seive[i] == 1 )
    continue;
    // Essentially mark all primes as 0 and
    // non-primes as 1's
    seive[i] = 0;
    for( int j = i + i ; j < n ; j += i )
    seive[j] = 1;
    }
    }

    int main()
    {
    generate_seive(100);

    // Iterating through the generated Seive
    // should do
    for( int i = 2; i < 100 ; i ++ )
    if( seive[i] == 0 )
    printf("%dn", i);
    return 0;
    }





    share|improve this answer

























      up vote
      1
      down vote













      There are some primitive ways like this one for instance, looping through the odd numbers and some more optimizations



      int isPrime ( int n )
      {
      if (n <= 1) return 0; // zero and one are not prime
      unsigned int i;
      for (i=2; i*i<=n; i++) {
      if (n % i == 0) return 0;
      }
      return 1;
      }


      Seive is kind of over-kill, in your case ( if you are not taking in to consideration your memory requirements ) because the range could be very very large 1000000 you might want to use some sort of bitmap to generate the Seive.



      here is a very loosely written Idea of how to generate and use Seive.



      char *seive;

      void generate_seive( int n )
      {
      seive = calloc( 1, n );
      if( !seive )
      {
      printf("E...");
      exit(0);
      }

      // Generate Seive
      for( int i = 2; i < n ; i ++)
      {
      if( seive[i] == 1 )
      continue;
      // Essentially mark all primes as 0 and
      // non-primes as 1's
      seive[i] = 0;
      for( int j = i + i ; j < n ; j += i )
      seive[j] = 1;
      }
      }

      int main()
      {
      generate_seive(100);

      // Iterating through the generated Seive
      // should do
      for( int i = 2; i < 100 ; i ++ )
      if( seive[i] == 0 )
      printf("%dn", i);
      return 0;
      }





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        There are some primitive ways like this one for instance, looping through the odd numbers and some more optimizations



        int isPrime ( int n )
        {
        if (n <= 1) return 0; // zero and one are not prime
        unsigned int i;
        for (i=2; i*i<=n; i++) {
        if (n % i == 0) return 0;
        }
        return 1;
        }


        Seive is kind of over-kill, in your case ( if you are not taking in to consideration your memory requirements ) because the range could be very very large 1000000 you might want to use some sort of bitmap to generate the Seive.



        here is a very loosely written Idea of how to generate and use Seive.



        char *seive;

        void generate_seive( int n )
        {
        seive = calloc( 1, n );
        if( !seive )
        {
        printf("E...");
        exit(0);
        }

        // Generate Seive
        for( int i = 2; i < n ; i ++)
        {
        if( seive[i] == 1 )
        continue;
        // Essentially mark all primes as 0 and
        // non-primes as 1's
        seive[i] = 0;
        for( int j = i + i ; j < n ; j += i )
        seive[j] = 1;
        }
        }

        int main()
        {
        generate_seive(100);

        // Iterating through the generated Seive
        // should do
        for( int i = 2; i < 100 ; i ++ )
        if( seive[i] == 0 )
        printf("%dn", i);
        return 0;
        }





        share|improve this answer












        There are some primitive ways like this one for instance, looping through the odd numbers and some more optimizations



        int isPrime ( int n )
        {
        if (n <= 1) return 0; // zero and one are not prime
        unsigned int i;
        for (i=2; i*i<=n; i++) {
        if (n % i == 0) return 0;
        }
        return 1;
        }


        Seive is kind of over-kill, in your case ( if you are not taking in to consideration your memory requirements ) because the range could be very very large 1000000 you might want to use some sort of bitmap to generate the Seive.



        here is a very loosely written Idea of how to generate and use Seive.



        char *seive;

        void generate_seive( int n )
        {
        seive = calloc( 1, n );
        if( !seive )
        {
        printf("E...");
        exit(0);
        }

        // Generate Seive
        for( int i = 2; i < n ; i ++)
        {
        if( seive[i] == 1 )
        continue;
        // Essentially mark all primes as 0 and
        // non-primes as 1's
        seive[i] = 0;
        for( int j = i + i ; j < n ; j += i )
        seive[j] = 1;
        }
        }

        int main()
        {
        generate_seive(100);

        // Iterating through the generated Seive
        // should do
        for( int i = 2; i < 100 ; i ++ )
        if( seive[i] == 0 )
        printf("%dn", i);
        return 0;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 9:33









        u__

        2,501828




        2,501828






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204676%2fgetting-tle-in-hackerearth%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            yhRGF9HdNpDqCM,ZOV0lf6ErvTl42,H hklRFBR05EmL0t,Y9Xx7,ExX,D2MHqpe
            n s Q2nnH,jktKG,Vsvh7KQR uUSmOMnLtjIxKZ80ZqGziqccir,H,ZfsyIY184bmTx2OOLPqY sv,8m2

            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            Guess what letter conforming each word

            Run scheduled task as local user group (not BUILTIN)