Results 1 to 9 of 9

Thread: Optimize the code!

  1. #1
    Join Date
    Mar 2011
    Location
    India
    Beans
    10
    Distro
    Ubuntu 10.04 Lucid Lynx

    Optimize the code!

    Hey, I tried to solve this problem.(http://www.codechef.com/problems/K2)using this code.


    Though my code was right , it exceeded the time limit.


    #include<stdio.h>
    void convert(long long int number,long long int base,int *converted,int *index);
    void palcheck( int converted[1000], int *index, int *test);
    main()
    {
    long long int number,base;
    int converted[1000],test=1,index,t,i;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
    test=1;
    scanf("%lld",&number);
    for(base=2;base<=number-1;base++)
    {
    convert(number,base,converted,&index);
    palcheck(converted,&index,&test);
    if(test==0) printf("%lld \n",base) && (base=number+1);
    }
    if(test!=0) printf("%lld \n",number+1);

    }
    }



    void convert(long long int number,long long int base, int *converted,int *index)
    {
    int i;
    for(i=1;number>0;i++)
    {
    converted[i]=number%base;
    number=number/base;
    }
    *index=i-1;
    }

    void palcheck(int *converted , int *index, int *test)
    {
    int i,k=0;
    for(i=1;i<=*index;i++)
    {
    if(converted[i]==converted[*index-i+1]) k++ ;
    else (i=*index+1);
    }
    if(k==*index) *test=0;
    else *test=1;
    }

    Basically the pseudo code is :

    1: Take in number.
    2. Convert it to base n in this method and store each digit in array.

    for(i=1;number>0;i++)
    {
    converted[i]=number%base;
    number=number/base;
    }

    3. Check if 1st and last digit are same,if yes then if 2nd and 2nd last and so on for palindromic condition.....


    Any ideas how to optimize the code?

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Optimize the code!

    What does this code do? Perhaps before attempting the scanf(), you could issue a prompt.

    It would better for us if you would post the problem, rather than the solution... which is impossible to say if it "works", since I don't know what problem you are trying to solve.


    P.S. Why are you converting the input? Read it in as a string, and leave it as such when doing the comparisons!
    Last edited by dwhitney67; September 5th, 2011 at 07:23 PM.

  3. #3
    Join Date
    Mar 2009
    Location
    Brazil
    Beans
    475
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Optimize the code!

    I think the part where you check if it's a palindrome can be optimized.

    It seems you're testing every number twice, for example, the number 123321:

    1 = 1 (ok)
    2 = 2 (ok)
    3 = 3 (ok)
    3 = 3 (ok)
    2 = 2 (ok)
    1 = 1 (ok)

    If the first half of those tests is true, the second half will also be, so there is no need to test it. Just remember that there will be two slightly different cases, an even string length or an odd one.

    I'm not sure something similar can be done in your case, but in Python, to test a number "n", I'd just do something like:
    Code:
    if str(n) == str(n)[::-1] :
    The [::-1] part inverts the whole string, so a single test is done.
    Last edited by Marlonsm; September 5th, 2011 at 07:09 PM.
    Ubuntu User #27453 | Linux User #490358
    "Don't preach Linux, mention it"
    "Linux is not Windows"
    73% of statistics in forums are made up on the spot

  4. #4
    Join Date
    Jun 2011
    Beans
    67
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Optimize the code!

    if(test==0) printf("%lld \n",base) && (base=number+1);
    Alright, I am confused. This line looks like Perl or Bash script.

  5. #5
    Join Date
    Mar 2011
    Location
    India
    Beans
    10
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Optimize the code!

    @dwhitney67: I already posted the link to the problem. http://www.codechef.com/problems/K2. And yes I'll surely add what to input in print.



    @marlonsm : Thanks dude. But for some strange reason its not giving output for large nos like(9876543219) after making the change. I have added a comment in the line I made the change
    Have a look : http://dl.dropbox.com/u/19182824/current_change.c


    @gardnan : Its in C.


    #include<stdio.h>
    void convert(long long int number,long long int base,int *converted,int *index);
    void palcheck( int converted[1000], int *index, int *test);
    main()
    {
    long long int number,base;
    int converted[1000],test=1,index,t,i;
    printf("Enter the number of test cases : ");
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
    test=1;
    printf("Enter the number : ");
    scanf("%lld",&number);
    for(base=2;base<=number-1;base++)
    {
    convert(number,base,converted,&index);
    palcheck(converted,&index,&test);
    if(test==0) printf("The smallest base in which given number is palindromic is %lld \n",base) && (base=number+1);
    }
    if(test!=0) printf("The smallest base in which given number is palindromic is %lld \n",number+1);

    }
    }



    void convert(long long int number,long long int base, int *converted,int *index)
    {
    int i;
    for(i=1;number>0;i++)
    {
    converted[i]=number%base;
    number=number/base;
    }
    *index=i-1;
    }

    void palcheck(int *converted , int *index, int *test)
    {
    int i,k=0,p;
    for(i=1;i<=*index;i++)
    {
    if(converted[i]==converted[*index-i+1]) k++ ;
    else (i=*index+1);
    }
    if(k==*index) *test=0;
    else *test=1;
    }

    Or here is the file: http://dl.dropbox.com/u/19182824/current_4.c

    Thanks to everyone for your prompt replies!
    Last edited by sjs7007; September 5th, 2011 at 07:37 PM.

  6. #6
    Join Date
    May 2006
    Beans
    1,790

    Re: Optimize the code!

    Quote Originally Posted by sjs7007 View Post
    @dwhitney67: I already posted the link to the problem. http://www.codechef.com/problems/K2. And yes I'll surely add what to input in print.



    @marlonsm : Thanks dude. But for some strange reason its not giving output for large nos like(9876543219) after making the change. I have added a comment in the line I made the change
    Have a look : http://dl.dropbox.com/u/19182824/current_change.c


    @gardnan : Its in C.


    #include<stdio.h>
    void convert(long long int number,long long int base,int *converted,int *index);
    void palcheck( int converted[1000], int *index, int *test);
    main()
    {
    long long int number,base;
    int converted[1000],test=1,index,t,i;
    printf("Enter the number of test cases : ");
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
    test=1;
    printf("Enter the number : ");
    scanf("%lld",&number);
    for(base=2;base<=number-1;base++)
    {
    convert(number,base,converted,&index);
    palcheck(converted,&index,&test);
    if(test==0) printf("The smallest base in which given number is palindromic is %lld \n",base) && (base=number+1);
    }
    if(test!=0) printf("The smallest base in which given number is palindromic is %lld \n",number+1);

    }
    }



    void convert(long long int number,long long int base, int *converted,int *index)
    {
    int i;
    for(i=1;number>0;i++)
    {
    converted[i]=number%base;
    number=number/base;
    }
    *index=i-1;
    }

    void palcheck(int *converted , int *index, int *test)
    {
    int i,k=0,p;
    for(i=1;i<=*index;i++)
    {
    if(converted[i]==converted[*index-i+1]) k++ ;
    else (i=*index+1);
    }
    if(k==*index) *test=0;
    else *test=1;
    }

    Or here is the file: http://dl.dropbox.com/u/19182824/current_4.c

    Thanks to everyone for your prompt replies!
    Please use CODE tags around your code, so the indendation is kept (you can use the # tool in the toolbar).

  7. #7
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Optimize the code!

    Quote Originally Posted by sjs7007 View Post
    @dwhitney67: I already posted the link to the problem. http://www.codechef.com/problems/K2.
    Here's my rebuttal: http://dictionary.reference.com/browse/proofread

    Please re-read your original post (OP) and please let the world know what you see after you click on the URL you supplied.

  8. #8
    Join Date
    Mar 2011
    Location
    India
    Beans
    10
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Optimize the code!

    .
    Last edited by sjs7007; September 6th, 2011 at 03:54 AM.

  9. #9
    Join Date
    Mar 2011
    Location
    India
    Beans
    10
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Optimize the code!

    @arndt:
    Code:
    
    
    #include<stdio.h>
    void convert(long long int number,long long int base,int *converted,int *index);
    void palcheck( int converted[1000], int *index, int *test);
    main()
    {
        long long int number,base;
        int converted[1000],test=1,index,t,i;
        printf("Enter the number of test cases : ");
        scanf("%d",&t);
        for(i=1;i<=t;i++)
        {
            test=1;
            printf("Enter the number : ");
            scanf("%lld",&number);
            for(base=2;base<=number-1;base++)
            {
                convert(number,base,converted,&index);
                palcheck(converted,&index,&test);
                if(test==0) printf("The smallest base in which given number is palindromic is %lld \n",base) && (base=number+1);
            }
        if(test!=0) printf("The smallest base in which given number is palindromic is %lld \n",number+1);
    
        }
    }
    
    
    
    void convert(long long int number,long long int base, int *converted,int *index)
    {
        int i;
        for(i=1;number>0;i++)
        {
            converted[i]=number%base;
            number=number/base;
        }
        *index=i-1;
    }
    
    void palcheck(int *converted , int *index,  int *test)
    {
        int i,k=0,p;
        for(i=1;i<=*index;i++)
        {
            if(converted[i]==converted[*index-i+1]) k++ ;
            else (i=*index+1);
        }
        if(k==*index) *test=0;
        else *test=1;
    }

    @dwhitney67: I had realized my mistake after your comment. But in my defense I had put the link was in parenthesis. But since I was wrong , I am sorry.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •