9
\$\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\$
  • 3
    \$\begingroup\$ What is the reverse of 100? \$\endgroup\$ – tsh Oct 10 '17 at 2:33
  • \$\begingroup\$ 001, but in any case, its fine if you have additional zeros in front \$\endgroup\$ – K Split X Oct 10 '17 at 2:41
  • 3
    \$\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\$ – user202729 Oct 10 '17 at 10:31
  • 2
    \$\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 '17 at 19:46

60 Answers 60

10
\$\begingroup\$

05AB1E, 2 bytes

R*

Try it online!

In 05AB1E, integers and strings are treated as equivalent types, so reversal (R) converts to string and reverses, whilst multiplication (*) treats the reverse and the input as integers.

\$\endgroup\$
  • 4
    \$\begingroup\$ Â* is valid too :D. \$\endgroup\$ – Magic Octopus Urn Oct 10 '17 at 19:45
4
\$\begingroup\$

JavaScript (SpiderMonkey), 45 35 33 28 bytes

n=>n*[...n].reverse().join``

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! You don't have to count f=; anonymous functions are allowed by default. \$\endgroup\$ – Dennis Oct 10 '17 at 3:11
  • \$\begingroup\$ Switch out (n+"").split("") for [...(n+"")] to save a few bytes. You don't need the unary plus, and the parentheses around the reversed string are extraneous. All in all, that saves you 10 bytes. \$\endgroup\$ – kamoroso94 Oct 10 '17 at 4:51
  • \$\begingroup\$ You can replace .join("") with .join`` to save 2 bytes. \$\endgroup\$ – ATaco Oct 10 '17 at 5:25
  • \$\begingroup\$ Save 5 bytes by taking input as a string: tio.run/##BcFRCoAgDADQu/ilQYP6t4tEoNgMzTZxIXR6ey/… \$\endgroup\$ – Shaggy Oct 10 '17 at 6:37
4
\$\begingroup\$

Jelly, 3 bytes

×ṚḌ

I'm new to Jelly, so please let me know if there is a way to do this in 1 or 2 bytes!

Try it online!

Explanation

×ṚḌ    (Input: 12)

 Ṛ     Reversed decimal digits (Stack: [2, 1])
×      Multiply by input       (Stack: [24, 12])
  Ḍ    Convert to decimal      (Stack: 252)
       Implicit print
\$\endgroup\$
  • \$\begingroup\$ Actually ṚḌ× would be a better version. Of course this would still work, due to how bases work. \$\endgroup\$ – Erik the Outgolfer Oct 10 '17 at 11:33
  • \$\begingroup\$ @EriktheOutgolfer I originally wrote that first, but moved the × for fanciness ;) \$\endgroup\$ – JungHwan Min Oct 10 '17 at 12:35
3
\$\begingroup\$

Perl 5, 11 + 1 (-p) = 12 bytes

$_*=reverse

Try it online!

\$\endgroup\$
3
\$\begingroup\$

ARBLE, 12 bytes

Takes input as an int.

a*reverse(a)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Thats why I said integer input ;), does int still work? \$\endgroup\$ – K Split X Oct 10 '17 at 2:23
  • \$\begingroup\$ @KSplitX Oh, I didn't notice that. I'd think that restriction a bit unnecessary, but it's up to you. \$\endgroup\$ – ATaco Oct 10 '17 at 2:24
  • \$\begingroup\$ @KSplitX Fixed. \$\endgroup\$ – ATaco Oct 10 '17 at 2:25
  • \$\begingroup\$ Should use n * reverse(n) so it matches the question's specification word for word (Given an integer n, print out n * reversed(n)) \$\endgroup\$ – Okx Oct 11 '17 at 15:39
3
\$\begingroup\$

Python 3, 35 28 bytes

lambda m:m*int(str(m)[::-1])

Try it online!

Saved 7 bytes by fixing a bug pointed out by Dennis.

\$\endgroup\$
  • \$\begingroup\$ Nah its clear, you convert to string, the [::-1] reverses it, then we eval xD \$\endgroup\$ – K Split X Oct 10 '17 at 3:48
  • \$\begingroup\$ This doesn't work for input 80, as 08 is an invalid (octal) literal. \$\endgroup\$ – Dennis Oct 10 '17 at 4:47
  • 3
    \$\begingroup\$ Saved 7 bytes by fixing a bug Don't you just love it when that happens... \$\endgroup\$ – ETHproductions Oct 10 '17 at 18:25
3
\$\begingroup\$

Haskell, 27 23 bytes

4 bytes saved thanks to Lynn and Laikoni

(*)=<<read.reverse.show

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ 23 bytes without import: Try it online! \$\endgroup\$ – Laikoni Oct 10 '17 at 6:07
  • \$\begingroup\$ With Control.Monad it is also 23 bytes: ap(*)$read.reverse.show. \$\endgroup\$ – Laikoni Oct 10 '17 at 6:09
  • 1
    \$\begingroup\$ No need for Control.Monad. (*)=<<read.reverse.show works fine. \$\endgroup\$ – Lynn Oct 10 '17 at 13:22
  • \$\begingroup\$ @Lynn Thanks! I feel like I should have seen that. \$\endgroup\$ – Wheat Wizard Oct 10 '17 at 13:26
3
\$\begingroup\$

Perl 6, 10 bytes

{$_*.flip}

Try it

Expanded

{       # bare block lambda with implicit parameter $_
  $_
  *
  .flip # implicit method call on $_
}
\$\endgroup\$
3
\$\begingroup\$

C# .NET, 55 bytes

n=>{int i=n,j=0;for(;i>0;i/=10)j=j*10+i%10;return n*j;}

Explanation:

Try it here.

n=>{           // Method with integer as both parameter and return-type
  int i=n,     //  Integer `i` (starting at the input)
      j=0;     //  Integer `j` (starting at 0)
  for(;i>0;    //  Loop as long as `i` is not 0
      i/=10)   //    After every iteration: Remove the last digit of `i`
    j=j*10     //   Add a trailing zero to `j`,
      +i%10;   //   and then sum this new `j` with the last digit of `i`
               //  End of loop (implicit / single-line body)
  return n*j;  //  Return the input multiplied with `j`
}              // End of method
\$\endgroup\$
3
\$\begingroup\$

Ruby, 25 24 bytes

->n{n*eval(n.digits*'')}

Try It Online!.

Integer#digits returns a list of reversed digits, so further reversing is not necessary.

Thanks to @benj2240 for golfing a byte!

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

Batch, 87 bytes

@set s=%1
@set r=
:l
@set/ar=r*10+s%%10,s/=10
@if %s% gtr 0 goto l
@cmd/cset/a%1*r

Need to take the arithmetic route here as string reversal fails for some numbers such as 80.

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

J, 7 bytes

*|.&.":

Try it online!

Couldn't think of a shorter way, though I feel like this is pretty elegant.

Explanation

*|.&.":
   &.":  Convert to string, apply next function, then undo conversion
 |.      Reverse
*        Multiply by input
\$\endgroup\$
2
\$\begingroup\$

C (gcc), 49 bytes

i,a;f(n){for(i=0,a=n;a>0;a/=10)i=i*10+a%10;n*=i;}

Try it online!

\$\endgroup\$
  • 5
    \$\begingroup\$ Suggest replacing a>0 with just a \$\endgroup\$ – ceilingcat Oct 10 '17 at 17:53
2
\$\begingroup\$

LISP, 91 64 bytes

(defun R (N)(defvar M (write-to-string N)) (parse-integer (reverse M))) (write (* x (R x)))

(defun R(N)(write(* N(parse-integer(reverse(write-to-string N))))))

Where x N is your integer you want to work with, of course.

I'm pretty new to programming, but I've found that trying these Code Golf problems has been nice practice. Is there something I'm missing that could help with this?

EDIT: Thanks to some tips from ceilingcat, I was able to shave off a few bytes. Old program preserved in strikethrough for reference.

\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Golf! You can eliminate some whitespace and maybe ditch a variable assignment. Also, by convention you may be able to just return the output instead of (write ...) \$\endgroup\$ – ceilingcat Oct 10 '17 at 17:56
  • \$\begingroup\$ You can save a byte using a lambda instead of defun. Also, read tips for golfing in lisp \$\endgroup\$ – ceilingcat Oct 10 '17 at 19:59
2
\$\begingroup\$

bash, 52 48 bytes

for((i=n=$1;r=r*10+i%10*n,i/=10;));{ :;};echo $r
\$\endgroup\$
  • \$\begingroup\$ You can replace do...done with curly braces to save some bytes: Try it online!. This is from the tips for Bash post. \$\endgroup\$ – Justin Mariner Oct 10 '17 at 17:28
  • \$\begingroup\$ thank-you, I'm glad to learn it, I had never seen this syntax, updating my post \$\endgroup\$ – Nahuel Fouilleul Oct 10 '17 at 19:42
  • \$\begingroup\$ Isn’t that shellshock? \$\endgroup\$ – Stan Strum Feb 24 '18 at 23:45
2
\$\begingroup\$

Batch, 150 125 121 bytes (+ 5 bytes? cmd/q)

set l=%1
set n=0
set r=
:L
call set t=%%l:~%n%,1%%%
set/an+=1
if [%t%] neq [] set r=%t%%r%&goto L
set/ar=%r%*%l%
echo %r%

Saved 25 bytes thanks to user202729!

Saved 4 bytes thanks to Matheus Avellar!

\$\endgroup\$
  • \$\begingroup\$ 97 bytes \$\endgroup\$ – user202729 Oct 10 '17 at 13:29
  • \$\begingroup\$ Or, 87 bytes \$\endgroup\$ – user202729 Oct 10 '17 at 13:40
  • \$\begingroup\$ Isn't this Batch, not Bash? The TIO for bash does not work for this. \$\endgroup\$ – Justin Mariner Oct 10 '17 at 17:26
  • \$\begingroup\$ Yes, yes it is; sorry about that \$\endgroup\$ – Ephellon Dantzler Oct 10 '17 at 17:44
  • \$\begingroup\$ You can inline that if to be at 121 bytes: if [%t%] neq [] set r=%t%%r%&goto L. However, I think you gotta include 1 byte for the /Q flag passed tocmd so it runs with implicit @echo off \$\endgroup\$ – Matheus Avellar Oct 11 '17 at 1:48
2
\$\begingroup\$

><>, 41 39 Bytes

:&>:a%:}-\
/~\?)0:,a/
>l1-?\&*n;
\ +*a/

How it works:

:&

Assume input has been pushed to the stack (https://codegolf.meta.stackexchange.com/a/8493/76025). Duplicate it and store a copy in the register.

   >:a%:}-\
   \?)0:,a/

Converts this to its individual digits, leaving them on the stack.

/~
>l1-?\
\ +*a/

The top value will always be a zero due to the number-to-digit conversion process; drop it from the stack. Now, while the length is >1, multiply the first item by ten and add it to the item below it. This results in the number reversed.

      &*n;

Multiply the original number by the reverse, print the answer, and stop.

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

Ohm v2, 2 bytes

œΠ

Try it online!

Explanation:

œΠ   Main wire, arguments: n

œ    Pushes [n, n.reverse]
 Π   Multiplies that array together
     Implicit output
\$\endgroup\$
  • 2
    \$\begingroup\$ stays on the phone for 4 months :P \$\endgroup\$ – K3v1n Feb 22 '18 at 15:37
  • \$\begingroup\$ You must be put on hold for a long time for there to be no explanation \$\endgroup\$ – MilkyWay90 Apr 25 at 2:34
  • \$\begingroup\$ @MilkyWay90 We've all been stuck on hold for a year and a half before, right? \$\endgroup\$ – Nick Clifford Apr 26 at 14:27
  • \$\begingroup\$ @NickClifford Not all of us have \$\endgroup\$ – MilkyWay90 Apr 26 at 20:11
1
\$\begingroup\$

Mathematica, 19 bytes

# IntegerReverse@#&

Takes an integer input.

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

cQuents 0, 8 bytes

#|1:A\rA

Try it online!

Explanation

#|1:      Output first term in sequence
    A\rA  Each term in the sequence equals:
           A * \reverse(A)
\$\endgroup\$
1
\$\begingroup\$

Ly, 7 bytes

nsSrJl*

Try it online!

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

Casio-Basic (fx-CP400), 44 bytes

ExpToStr n,a
StrInv a,a
Print n*strToExp(a)

There's no built-in for reversing an integer, but there is one for reversing a string.

ExpToStr n,a turns n into a string and stores it in a, then StrInv a,a overwrites a with the reversed version of itself. The last line turns a into a number, and prints n*a.

43 bytes for the code, +1 to input n into the parameters box.

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

Japt, 2 bytes

Takes input as a string, outputs an integer.

*w

Try it

\$\endgroup\$
  • \$\begingroup\$ Wow, I thought it would have to be 4 bytes at least... I suppose it's only 3 even if taking input as an integer *sw \$\endgroup\$ – ETHproductions Oct 10 '17 at 18:26
  • \$\begingroup\$ @ETHproductions: yeah, that's what I had originally. \$\endgroup\$ – Shaggy Oct 10 '17 at 18:40
1
\$\begingroup\$

MATLAB/Octave, 33 31 bytes

@(n)str2num(flip(int2str(n)))*n

Try it online!

Octave/MATLAB anonymous function. This is a pretty naïve approach - converts the integer to a string, flips the string, converts the result back to an integer and multiplies it by the original.


  • Save 2 bytes by using flip instead of fliplr.
\$\endgroup\$
1
\$\begingroup\$

Bash + GNU utilities, 18

bc<<<$1*`rev<<<$1`

Try it online.

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

PHP, 23+1 bytes

<?=$argn*strrev($argn);

Save to file and run as pipe with -nF.

\$\endgroup\$
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\$
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\$
  • \$\begingroup\$ Wow, I had the same answer \$\endgroup\$ – Stan Strum Nov 24 '17 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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