This question came from our site for professional and enthusiast programmers.

5

Go back to the time the number was a string, then reverse the string
–
pmgJun 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 ...
–
PointyJun 11 '11 at 10:56

2

Just a notice: any number ending with 0 becomes a shorter number of digits when reversed...
–
powtacJun 11 '11 at 12:44

31

I know an algorithm that takes no time at all, but only works on palindromic numbers ;)
–
schnaaderJun 11 '11 at 14:47

Found time to do the re-write myself. I hope this remain the puzzle that eltond meant to pose.
–
dmckee♦Jun 12 '11 at 0:37

Haskell, 28 24 characters

(+0): Legit man! Though technically you don't need the .(+0) at all, as f would be more polymorphic than what the problem requires (it is allowed to return a 'similarly typed' output). I would shave off those 5 characters.
–
Thomas EdingAug 10 '11 at 22:14

It is possible to convert a number a string, then reverse the string and then convert that string back to number. This kind of feature is probably available in all language. If you are looking for a more mathematical method then this might help:

APL (3)

Language-independent/mathematics

Inspired by Kiril Kirov's answer above. I got curious about the mathematical properties of reversing a number, so I decided to investigate a bit.

Turns out if you plot the difference n - rev(n) for natural numbers n in some base r, you get patterns like this ((n - rev(n)) / (r - 1), for r=10, wrapped at r columns, red denotes negative number):

This sequence could be generated as such (pseudocode):

for i=1 to r:
output 0
for m=0, 1, …
for k=1 to (r-1):
for d=1 to r^m:
for i=0 to (r-1):
output (r-1) * (r+1)^m * (k - i)

If you store these values in a list/array, then n - arr[n] would get you the reversed form of n. Now, to "mathematically golf" this, we'd ideally want a closed-form expression that gives us the n:th value in the sequence, so that we could have a closed-form expression for solving the entire task. Unfortunately, I haven't been able to find such an expression... but it looks like it should be possible. :(

So yeah, not so much a code-golf as a mathematical curiosity, but if there is a closed-form expression of the above sequence it might actually be useful in proper PL golf submissions.

It is a mathematical solution. Mathematics is not numbers is not arithmetics. Mathematics also deals with strings of symbols. And in this special case, the conversion to and from string is just conversion to and from base-10.
–
R. Martinho FernandesJun 11 '11 at 14:20

What I meant by "not a mathematical solution" is that we're not doing any math ourselves. The methods are doing all of the parsing and mathematics for us. As opposed to e.g. Kiril Kirov's answer.
–
VictorJun 13 '11 at 7:38