Howdy!

sub palindrate { my $X = shift; my $front = substr($X, 0, (length($X)+1)/2); my $back = reverse $front; substr($back, 0, 1) = '' if length($X) % 2; my $front2 = $front+1; my $back2 = reverse $front2; substr($back2, 0, 1) = '' if length($X) % 2; my $N1 = $front.$back; my $N2 = $front2.$back2; return (abs($X-$N1) < abs($X-$N2)) ? $N1 : $N2; }

Update: fixed typo in code so it really does work as coded now. The algorithm was correct; the implementation was flawed...and use warnings would have pointed it right out to me...

Further update: *busted*...It's still not quite right... 91 breaks... ...but I'm gonna leave the code as it stands

yours,
Michael

In reply to Re^4: Challenge: Nearest Palindromic Number by herveus
in thread Challenge: Nearest Palindromic Number by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.