# Shortest way to reverse a number [closed]

Write a function (or equivalent subprogram) to accept a single integer valued argument and return a (similarly typed) value found by reversing the order of the base-10 digits of the argument.

For example given 76543 return 34567

• Go back to the time the number was a string, then reverse the string
– pmg
Jun 11 '11 at 10:54
• The idea of a "shortest algorithm" is somewhat specious, especially if you'll allow "any language." Think up an algorithm, and I'll give you a DSL with an appropriate "~" operator ...
– Pointy
Jun 11 '11 at 10:56
• Just a notice: any number ending with 0 becomes a shorter number of digits when reversed... Jun 11 '11 at 12:44
• I know an algorithm that takes no time at all, but only works on palindromic numbers ;) Jun 11 '11 at 14:47
• Found time to do the re-write myself. I hope this remain the puzzle that eltond meant to pose. Jun 12 '11 at 0:37

# Python 3, 33 bytes

I decided to make a function instead of hardcoding it.

def r(n):print(int(str(n)[::-1]))


The code is pretty self-explanatory.

Usage: r(12345)

# Oracle SQL 11.2, 35 34 bytes

SELECT 0+REVERSE(:1||'')FROM DUAL;


# JavaScript (ES6) 35

a=b=>b.split("").reverse().join("")

• This doesn't assume b is an integer. Jul 4 '15 at 18:54

# Javascript, 34

([]+n).split("").reverse().join("")