ID:195035
 
//Title: Palindromic Check
//Contributed By: Haywire
//Inspired By: Project Euler, Problem 4.

/*
This is a simple palindromic number check which returns true if the number (n) is symmetrical.
*/


proc
is_palindromic(var/n)
return (n==reverse_num(n))

reverse_num(var/n)
n = num2text(n)
for(var/i = lentext(n), i > 0, i--)
. += copytext(n, i, i+1)
.=text2num(.)



mob
verb
Test(n as num)
//9009 is an example of a symmetrical number (palindromic number)
world << "[n] is [(is_palindromic(n)? "a":"not a")] palindromic number."

This proc crashes if you give it a number argument with 7 digits or more, because of scientific notation.
In response to Metamorphman
Metamorphman wrote:
This proc crashes if you give it a number argument with 7 digits or more, because of scientific notation.

Thanks for spotting that out but that partly at fault of BYOND (however it doesn't crash but it does indeed fail).

Due to scientific notation and BYOND's inaccurate precision of larger numbers I couldn't really modify it to work exactly how I wanted it to.

However I have discovered a different method (thanks to Popisfizzy), by taking text as an input I can compare the value with precision as scientific notation isn't taken in-account when handling non-numerical values.

//Title: Palindromic Check
//Contributed By: Haywire
//Inspired By: Project Euler, Problem 4.

/*
This is a simple palindromic number check which returns true if the number (n) is symmetrical.
*/


proc
is_palindromic(var/n)
return (n==reverse_num(n))

reverse_num(var/n)
for(var/i = lentext(n), i > 0, i--)
. += copytext(n, i, i+1)




mob
verb
Test(n as text)
//9009 is an example of a symmetrical number (palindromic number)
world << "[n] is [(is_palindromic(n)? "a":"not a")] palindromic number."