# 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, 2011 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, 2011 at 10:56
• Just a notice: any number ending with 0 becomes a shorter number of digits when reversed... Jun 11, 2011 at 12:44
• I know an algorithm that takes no time at all, but only works on palindromic numbers ;) Jun 11, 2011 at 14:47
• Found time to do the re-write myself. I hope this remain the puzzle that eltond meant to pose. Jun 12, 2011 at 0:37

# Java

""+new StringBuilder(""+i).reverse();


## Brainfuck, 11

>,[>,]<[.<]

• This accepts any string, not just numbers (ASCII 48-57). Apr 16, 2016 at 8:00

# K, 3 bytes:

.|$ Evaluate (.) the reverse (|) of casting to a string ($).

Usage example:

## Bash (15)

read a;rev<<<$a  As a number of other entries do, '01234' becomes '43210' and '2340' becomes '0432'; i.e. in Python terms it does print reverse(raw_input()). If behaviour like print int(reverse(str(int(raw_input())))) is expected, it is a bit longer: ### Takes care of trailing zeroes (36) read a;sed s.^0*..\;s.0*$..<<<$a|rev  • I think right count is 3: rev alone will suffice Dec 1, 2013 at 13:24 • “Write a function” – So I think the shortest solution is still 15 characters, but this way: r(){ rev<<<$1;}. (And even that is kind of cheating as the complete solutions is r(){ return rev<<<$1;}.) Dec 2, 2013 at 9:40 • @manatwork Shorter versions: r()(rev<<<$1) or r()(exit rev<<<$1). Jul 1, 2014 at 21:20 • Cool trick, @user23013. Jul 2, 2014 at 10:57 • str(int(raw_input())) really? [Python 2] [str(int(input()))] [from your example print int(reverse(str(int(raw_input()))))] Apr 16, 2016 at 7:58 ## in Q (20 characters) f:{"I"$reverse -3!x}


Sample Usage:

q)f 89478237
73287498


Use the k version of reverse (wrapped in parentheses) to make it shorter

{"I"$(|:) -3!x}  also just define it as a lambda to take 2 chars off for a total of 15 # ruby (26) The existing ruby answer wasn't a function/lambda, so here goes: f=->i{i.to_s.reverse.to_i}  ## Haskell, 61,23 Using the string reversing technique. digitToInt is available from the Data.Char library, and of course this code would be shorter if we'd be able to assume if Data.Char already was available. -- original version 61 chars foldl1((+).(*)10).map(Data.Char.digitToInt)$reverse$show 1234 --bonus foldonly version, 67 chars foldl1((+).(*)10).foldl(flip((:).Data.Char.digitToInt))[]$show 1234

--FireFly's suggestion: 23 chars. Note that read's return type is a
-- so you might want to tack on a +1 on ghci etc so Int can be derived.
read.reverse.show$1234  • Hm, why not read it back after reversing? I.e. read.reverse.show$1234. Dec 1, 2013 at 1:19
• hmm for some silly reason I thought read had to do with IO monads. You're right, read would work. Dec 1, 2013 at 1:51
• Added it. fold was more fun though :) Dec 1, 2013 at 1:55

Groovy 32

def r(n){print ((n+"").reverse())}


POWERSHELL, 25

-join(Read-Host)[-1..-9]


a longer version to take arbitrary input , 36

-join($a=Read-Host)[-1..-$a.length]


usage

PS C:\> -join(Read-Host)[-1..-9]
123456789
987654321
PS C:\>


# C# - 58

I think we need more C#

int.Parse(string.Concat(Enumerable.Reverse(x.ToString())))

• The spec requires a function or equivalent subprogram (a lambda would probably be acceptable, but a statement isn't); and it requires the return value to be similarly typed to the input (i.e. you're missing an int.Parse or a Convert.ToInt32). Jul 4, 2015 at 6:39
• @CoolerRanch still a statement I think Apr 16, 2016 at 8:06

Scheme, 75 characters

(string->number(list->string(reverse(string->list(number->string 76543)))))

• You can probably remove all the spaces but the last one.
– Omar
Nov 29, 2011 at 5:50
• Count in bytes, please. Apr 16, 2016 at 8:03

# k3, 4 chars

0$|$

0$|$76543 \-> gives 34567


See also the K4 solution here. The only difference (saving two chars) is that 0$ converts to int. ## PHP, 37 <?function f($n){return strrev($n);}  ## C, 44 x;m(n){for(;n;x=x*10+n%10,n/=10);return x;}  • Neither of these are functions, as specified. May 1, 2012 at 21:50 • @KevinReid Rectified! May 2, 2012 at 14:47 R (35 characters) > cat(rev(strsplit("12345","")[[1]])) 5 4 3 2 1  With help from my friends in R chat. • 12345->54321, not 12345->5 4 3 2 1. Apr 16, 2016 at 8:04 ## Game Maker Language, 44 Replace 12345 with whatever number or variable you want: n=12345while(n>0){r=r*10+(n mod 10)n div 10}  If you want to prompt for user input, use this 56 character long code instead: n=get_string('','')while(n>0){r=r*10+(n mod 10)n div 10}  In both pieces of code, the reverse number is stored in r • The question specifically asks for "a function (or equivalent subprogram)" which takes "a single integer valued argument". Neither of your code snippets meet that spec. Nov 30, 2013 at 17:27 • @PeterTaylor They're assumed to be scripts (function equivalents). Nov 30, 2013 at 21:48 # Rebol, 29 f: func[n][do reverse mold n]  Usage example in Rebol console: >> f 76543 == 34567  If you only wanted this to work on integer input then.... f: func [n [integer!]] [do reverse mold n]  # Julia, 23 r(x)=int(reverse("$x"))


\$ interpolates an argument (or the result of a function) into a string, denoted by the double quotes. int() automatically deletes leading zeroes.

Example:

julia> x=1234567890;r(x)
987654321


or

julia> r(1234567890)
987654321


# TI-BASIC, 58 54

Input N:0:While N>0:Ans+E3fpart(N,10:N/10→N:End:Ans


Usage

prgmREVERSE
?598028
820895
Done


## Burlesque, 2 bytes

Certain built-ins treat numbers as list of digits. The reverse built-in for example treats integers as a list of digits and thus reverses integers.

blsq ) 76543<-
34567


(For characters <- switches case).

# MATL, 3 bytes (non-competing)

VPU


This uses release 10.1.0 of the language/compiler, which is the current version at the time of writing.

Try it online!

### Explanation

V   % implicitly input a number and convert to string
P   % flip
U   % convert to number and implicitly display


# Python 2, 26 bytes

print int(input()[::-1])