12
\$\begingroup\$

Given an integer n, print out n * reversed(n)

reversed(n) is the number you get when you reverse the digits of n.


reverse(512) = 215

reverse(1) = 1

reverse(101) = 101

>>>>>>>>

func(5) = 5*5 = 25

func(12) = 12*21 = 252

func(11) = 11*11 = 121

func(659) = 659*956 = 630004

Shortest code wins!

Leaderboard

var QUESTION_ID=144816,OVERRIDE_USER=71625;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
5
  • 4
    \$\begingroup\$ What is the reverse of 100? \$\endgroup\$
    – tsh
    Oct 10, 2017 at 2:33
  • \$\begingroup\$ 001, but in any case, its fine if you have additional zeros in front \$\endgroup\$
    – K Split X
    Oct 10, 2017 at 2:41
  • 5
    \$\begingroup\$ Why downvote? Because this challenge is too trivial (compare to some other challenges, it is not!) or because it is badly-formatted / unclear? \$\endgroup\$
    – DELETE_ME
    Oct 10, 2017 at 10:31
  • 3
    \$\begingroup\$ @user202729 I downvoted because I didn't see or anticipate a lot of variety or depth to answers. This question is boring, I tried it. Part of the reason it is boring is because its trivial, which I think is a perfectly just cause to downvote a question on its own. \$\endgroup\$
    – Wheat Wizard
    Oct 10, 2017 at 19:46
  • 1
    \$\begingroup\$ What if the input is negative? \$\endgroup\$ Aug 29, 2022 at 17:50

78 Answers 78

1
\$\begingroup\$

Lua, 42 bytes

function f(n)print(n*(""..n):reverse())end

Try it online!

\$\endgroup\$
1
\$\begingroup\$

MATL, 5 bytes

VPUG*

Try it online!

Explanation: V converts to string, P flips, U converts back to numeric, G copies the original input again, and * multiplies them together.

\$\endgroup\$
1
\$\begingroup\$

><>, 33 bytes

:&v>&*nr
a,>:a(?^:a%:@-
2(?va*+>l

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Runic Enchantments, 11 10 bytes

i:0qr͍n*@

Try it online!

Takes input, duplicates it, coerces one copy to a string, reverses it, coerces it back to a numerical value, multiplies, and outputs the result.

0q performs the coercion to a string by concatenating a zero onto the end. There is no explicit ToString() operator, so this is the shortest method in this specific case where the 0 swapped to the beginning doesn't alter the resulting numerical value. Thanks to ASCII-only for this -1 byte.

\$\endgroup\$
2
  • \$\begingroup\$ 10 \$\endgroup\$
    – ASCII-only
    May 13, 2019 at 1:48
  • \$\begingroup\$ @ASCII-only Fair enough. I end up fiddling with most of these at work and don't always revisit when I go to post them at home. \$\endgroup\$ May 13, 2019 at 2:35
1
\$\begingroup\$

Pepe, 61 bytes

REeErEEEEErREEEeEerRREEEEEEEREEEEeEeerRREEEEEeEErRREEEEeEReEE

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Burlesque, 5 bytes

J<-|*

Try it online!

J  # Duplicate
<- # Reverse string
|* # string-to-val & multiply
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 72 bytes

Doesn't use strings. At all.

-10 by steffan

n=>(a=[],r=n=>(n>9&&r(n/10|0),a=[n%10,...a]))(n).reduce((a,v)=>a*10+v)*n
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can replace Math.floor(n/10) with n/10|0. \$\endgroup\$
    – naffetS
    Aug 28, 2022 at 0:38
  • \$\begingroup\$ seems I have a lot to learn... \$\endgroup\$
    – Leaf
    Aug 29, 2022 at 16:56
1
\$\begingroup\$

Desmos, 97 bytes,

f=\left\{x>0:floor(log(x))+1,0\right\}
g(x)=x∑_{n=0}^f10^{f-n}floor(mod(\frac{x}{10^{n-1}},10))

Try it on Desmos.

Not sure if this is legal as a Desmos answer, uses the fact that variables can be defined in terms of x, but functions of x can use those variables as functions? I'm not sure what actually is happening, but it seems to be... working. If I'm not allowed to use that, change f= to f(x)= for +3 bytes

Only works for nonnegative integers, which I think is fine since OP doesn't include any test cases for them, and they're not well defined for this problem (what is -1 backwards? 1-? That's not a number IMO). If I'm allowed to assume positive integers, the first line can become

f=floor(log(x))+1

for -21 bytes. Very nice, but the challenge says "integers", so it's iffy.

In general though, I know this is further golfable but I tried some of the Desmos tips and I must be doing something wrong because they weren't working. I'll come back and golf this eventually.

Code breakdown:

f=\left\{x>0:floor(log(x))+1,0\right\} definition of... function(?) f
f=                                     define f as
         x>0:                          if x is positive
             floor(log(x))+1             length(x)
                            ,0         else 0
                                       i dont remember why but this works to make g(0)=0

g(x)=x∑_{n=0}^f10^{f-n}floor(mod(\frac{x}{10^{n-1}},10)) definition of main function
g(x)=                                                    define g(x) as
     x                                                   x times
      ∑                                                  the sum of
       _{n=0}^f                                          for each n from 0 to f(x)
               10^{f-n}                                    10^(f-n) times
                       floor(mod(\frac{x}{10^{n-1}},10))   the nth digit from the right of x
                                                         essentially: sum of
                                                           (nth digit of x)
                                                           multiplied by
                                                           (place value of 
                                                           (length(x)-n)th digit of x
                                                         aka: reverse(x)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 66 bytes. Btw, variables that are defined in terms of function arguments are called wackscope variables, and is allowed and golfier to use them. In fact, one of my tips goes over this exact thing. \$\endgroup\$
    – Aiden Chow
    Sep 24, 2022 at 7:09
1
\$\begingroup\$

brev, 28 bytes

(fn(*((as-list reverse)x)x))

Crashes on negative

\$\endgroup\$
1
\$\begingroup\$

CJam, 8 bytes

l_W%i\i*

Try it online!

\$\endgroup\$
1
1
\$\begingroup\$

Julia, 33 32 bytes

~n=foldl((x,y)->10x+y,digits(n))n

  • 1 more byte saved by @MarcMush:
a^b=10a+b
~n=foldl(^,digits(n))n

Attempt This Online!

\$\endgroup\$
1
1
\$\begingroup\$

Pip, 4 bytes

a*Ra

Try It Online!

\$\endgroup\$
0
\$\begingroup\$

RProgN 2, 6 bytes

]Ø.in*

]Ø.in*
]       # Duplicate the input to the stack
 Ø.     # Append a blank string, stringifying the input.
   i    # Reverse it.
    n   # Cast to a number.
     *  # Multiply with the orignal input, implicitely output.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 4 bytes

*s_`

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Wow, I had the same answer \$\endgroup\$
    – Stan Strum
    Nov 24, 2017 at 2:59
0
\$\begingroup\$

Octave, 66 bytes

n=m=input('');j=0;while(m!=0) j=10*j+rem(m,10);m=fix(m/10);end
n*j

Since Octave doesn't have a function to reverse integers, I had to write one myself. That's what the while loop does. Any help will be much appreciated.

\$\endgroup\$
0
\$\begingroup\$

C# (.NET Core), 60 + 18 bytes

+18 bytes for using System.Linq;.

Reversing strings in C# is a pain.

n=>n*int.Parse(new string(n.ToString().Reverse().ToArray()))

Try it online!

\$\endgroup\$
2
0
\$\begingroup\$

Husk, 6 bytes

S*(d↔d

Try it online!

Explanation

S*(    Multiply the input with the result of the following function applied to the input
     d convert number to list of digits 
    ↔  reverse list
   d   interpret list of digits as number
\$\endgroup\$
0
\$\begingroup\$

LOGO, 13 bytes

[?*reverse ?]

Currently there is no TIO for Logo, but you can try it online at http://golf.shinh.org/check.rb . However that's UCBLogo where there is a feature that prints

Thank you for using Logo.
Have a nice day.

on program exit; if this is considered as a function invocation it does not count. (that is, the function does not prints the message)

Usage:

print invoke [?*reverse ?] 123

or

print apply [?*reverse ?] [123]

prints

39483
\$\endgroup\$
0
\$\begingroup\$

Recursiva, 6 bytes

*aI_Va

Try it online!

Explanation:

*aI_Va
*a      - multiply by a(input)
  I     - Integerify
   _    - Reverse   
    V   - Stringify 
     a  - a (Input)
\$\endgroup\$
0
\$\begingroup\$

Alice, 9 bytes

//.*
oR@i

Try it online!

Explanation

/    Switch to Ordinal. Move SE.
R.   Reverse and duplicate an implicit empty string. Does nothing.
i    Read all input as a string.
.    Duplicate.
R    Reverse.
/    Switch to Cardinal. Move W.
*    Implicitly convert the two strings to integers and multiply them.
.    Duplicate the result. Irrelevant.
/    Switch to Ordinal. Move NW. Immediately bounces off the wall to move SW instead.
o    Implicitly convert the result back to a string and print it.
/    Switch to Cardinal. Move S.
R    Reverse the other copy of te result. Irrelevant.
/    Switch to Ordinal. Move NE. Immediately bounces off the wall to move SE instead.
@    Terminate the program.
\$\endgroup\$
0
\$\begingroup\$

SAS, 54 bytes

%macro f(n);%put%eval(&n*%sysfunc(reverse(&n)));%mend;
\$\endgroup\$
0
\$\begingroup\$

Tcl, 34 bytes

proc F n {expr $n*[string rev $n]}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Swift 4, 37 bytes

{$0*Int(String("\($0)".reversed()))!}

Try it here!

Just my mandatory gigantic Swift entry.

\$\endgroup\$
0
\$\begingroup\$

R, 58 bytes

cat((n=scan())*sum(n%/%10^(d=1:nchar(n)-1)%%10*10^rev(d)))

Try it online!

reads from stdin, writes to stdout. n%/%10^(d=1:nchar(n)-1)%%10 is how we get the digits, and reversing them is as simple as taking that vector, multiplying by 10^rev(d), and summing.

R + stringi, 47 bytes

cat(strtoi(stringi::stri_reverse(n<-scan()))*n)

Try it online!

stringi (not stringr) has a stri_reverse command.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 5 4 bytes

*v_`

Try it online!

Explanation:

*v_`QQ : accepts a number as input
   `Q  : represent input as string
  _    : reverses string
 v     : evaluates the string as a number again
*    Q : multiply by the input
       : implicit output
(remove Q's as Pyth adds them for you)
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 31 Bytes

param($a)$a*-join("$a"[-1..-9])

Powershell is more than happy to multiply a number by a valid numerical string which is nice.

\$\endgroup\$
0
\$\begingroup\$

Batch, 105 bytes

@set c=
@set a=%1
:J
@set c=%c%%a:~-1%
@set a=%a:~0,-1%
@if NOT "%a%" == "" goto J
@set/ac=%1*%c%

The reverse is performed in a loop which appends the last character @set c=%c%%a:~-1% from the input to c, and then removes the last character from the input string @set a=%a:~0,-1% - rinse and repeat until there are no characters left on the input string. A quick multiplication @set/ac=%1*%c% and done.

\$\endgroup\$
0
\$\begingroup\$

QBIC, 12 bytes

_F!:$|?a*!A!

Explanation

   :    Get the input number from the cmd line and assign to 'a'
  ! $     Cast it to string
_F   |    and flip[ that, assigning it into A$
?a*     PRINT 'a' times
   !A!    A$ cast back to int
\$\endgroup\$
0
\$\begingroup\$

Aceto, 8 bytes

rd~isi*p
r  reads input
d  duplicates it
~  reverses the top value
i  convertes it to an integer
s  swaps the two top values
i  convertes the other value to an integer
*  multiplies the top two
p  prints the result

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Add++, 14 bytes

D,f,@,EDE@EdA*

Try it online!

How it works

With example input of 12

D,f,@,   - Create a monadic function, f
      ED - To digits;   STACK = [[1 2]]
      E@ - Reverse;     STACK = [[2 1]]
      Ed - From digits; STACK = [21]
      A  - Argument;    STACK = [21 12]
      *  - Product;     STACK = [252]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.