Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: Challenge: Nearest Palindromic Number

by herveus (Prior)
on Feb 02, 2005 at 19:51 UTC ( [id://427400]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Challenge: Nearest Palindromic Number
in thread Challenge: Nearest Palindromic Number

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

Replies are listed 'Best First'.
Re^5: Challenge: Nearest Palindromic Number
by tilly (Archbishop) on Feb 03, 2005 at 06:55 UTC
    Try 199. The answer should be 202, but you give 191.

    Update: After the fix, try 91. This gives 99 when 88 should be the answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://427400]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2024-04-24 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found