Thread: next palindrome number

  1. #1
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342

    next palindrome number

    I am given a lower limit and an upper limit.. (integers)
    I am given a palindrome in this interval..
    I am supposed to find the next palindrome in this interval
    ...
    Cud think of something for smaller intervals when I know the number of digits in the number is 3,4,5...
    Can some one give a generic method if the upper limit is given to be of the order 10^5..
    In the middle of difficulty, lies opportunity

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Was there a question in there?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    the upper limit is given to be of the order 10^5
    In that case you'll probably want to use longs.

    Finding the next palindrome is pretty easy. Say you have the number 123321; in that case, the next palindrome would be 124421. For 12321, it would be 12421. If you count the digits in the original palindrome you can figure out the middle digit[s] and increment them.

    It's a little harder when the number's something like 15951. You have to check if the middle number[s] are 9; if they are, increment the next columns: 16061.

    You also have to handle a number like 999, which would become 1001. You'd probably want a special case for this, when all the digits are 9.

    Hope this helps.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    A brute force approach would be
    Starting N
    Convert N to a string using sprintf (Nstr)
    Check if Nstr is a "palindrome string"
    Increase N and go back to the first until your final condition

    ---------------------------------
    http://www.uberum.com

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, that would be pretty silly. But yes, it's one way to do it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    [offtopic] Salem -- cool avatar [/offtopic]

    You might want to use strings for this -- handling specific digits in an integer may become tricky. You could use strcmp() to check the range as strings can't use the normal operators.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM