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\$ – Sriotchilism O'Zaic Oct 10 '17 at 19:46

60 Answers 60

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\$
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\$

Python 2, 25 bytes

lambda n:n*int(`n`[::-1])

Try it online!

\$\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\$
0
\$\begingroup\$

Excel VBA, 27 22 Bytes

Anonymous VBE Immediate window function that takes input from [A1] and outputs to the VBE immediate window

?[A1]*StrReverse([A1])
\$\endgroup\$
0
\$\begingroup\$

Implicit, 5 bytes

:ì\ì*

Try it online!

        « implicit integer input   »;
:       « duplicate                »;
 ì      « convert to string        »;
  \     « reverse                  »;
   ì    « back to int              »;
    *   « multiply                 »;
        « implicit integer output  »;
\$\endgroup\$
0
\$\begingroup\$

REXX, 22 bytes

arg n
say n*reverse(n)
\$\endgroup\$
0
\$\begingroup\$

Jelly, 4 bytes

DUV×

Try it online!

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

Pyt, 3 bytes

Đ₫*

Try it online!

Đ    Duplicate
 ₫   reverse digits
  *  duh
     implicit output
\$\endgroup\$
0
\$\begingroup\$

Java 8, 50 bytes

n->n*new Long(new StringBuffer(n+"").reverse()+"")

Try it here.

Pretty straightforward.

  • n->: Lambda-function with 64bit-number as input
  • n+"": Convert the input-number to String
  • new StringBuffer(...): Convert this String to a StringBuffer
  • .reverse(): Reverse it
  • ...+"": Convert the StringBuffer back to String
  • new Long(...): Convert this String back to a 64bit-number
  • n*...: Multiply it with the original input, and return it

Arithmetic approach would be slightly longer (55 bytes):

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

Try it here.

Explanation can be found in my C# .NET port.

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

Gol><>, 21 bytes

I:WaSD$|r}l3-Fa*+|+*h

Try it online!

An example > A million words

I :D WaSD$D| Dr}l3-D Fa*+D| D+*h
^659
   [659 659]
          [659 9 65]
          [659 9 5 6]
          [659 9 5 6 0]
             [659 9 5 6 0]
                   [659 0 6 5 9 2] <- 2 = operations to do
                         [659 0 6 95]
                         [659 0 956]
                            [659 0 956]

Alternate

I:WaSD$|~r}l2-Fa*+|*h
I:&WaSD$|~rlMFa*+|&*h
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog Unicode), 9 5 bytes

⍎×⍎∘⌽

Try it online!

Tacit prefix function. Takes input as a string of digits.

Thanks to Adám for 4 bytes.

How it works:

⍎×⍎∘⌽ ⍝ Anonymous, tacit function.
    ⌽ ⍝ Reverse the input
  ⍎∘  ⍝ Convert to number
 ×    ⍝ and multiply
⍎     ⍝ by input converted to number
\$\endgroup\$
  • \$\begingroup\$ ⍎×⍎∘⌽ \$\endgroup\$ – Adám Feb 22 '18 at 11:36
  • \$\begingroup\$ @Adám wow, I think this is one of my first APL answers. Thanks for the heads up :) \$\endgroup\$ – J. Sallé Feb 22 '18 at 13:19
  • \$\begingroup\$ What's up with the spurious Input ⍵.? \$\endgroup\$ – Adám Feb 22 '18 at 13:41
  • \$\begingroup\$ No idea, probably a hanger-on from an old edit or something :p It's fixed now. \$\endgroup\$ – J. Sallé Feb 22 '18 at 13:42
0
\$\begingroup\$

Java 8, 52 bytes

n->new Long(new StringBuilder(n+"").reverse()+"")*n;

Try it online
This will give every linter/IDE/compiler a stroke, but hey, it works

\$\endgroup\$
  • \$\begingroup\$ Hi there. This is the same as my Java answer from 1.5 years ago, except that you won't have to count the trailing semicolon in lambda submissions, and StringBuilder is golfed to StringBuffer. :) \$\endgroup\$ – Kevin Cruijssen Mar 8 at 11:56
  • \$\begingroup\$ @KevinCruijssen oh nice. \$\endgroup\$ – Benjamin Urquhart Mar 8 at 21:47
0
\$\begingroup\$

APL(NARS), 8 char, 16 bytes

{⍵×⍎⌽⍕⍵}

test:

  f←{⍵×⍎⌽⍕⍵}
  f 23
736
  23×32
736
  f 11
121
\$\endgroup\$
0
\$\begingroup\$

><>, 33 bytes

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

Try it online!

\$\endgroup\$
0
\$\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\$
  • \$\begingroup\$ 10 \$\endgroup\$ – ASCII-only May 13 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\$ – Draco18s May 13 at 2:35
0
\$\begingroup\$

Pepe, 61 bytes

REeErEEEEErREEEeEerRREEEEEEEREEEEeEeerRREEEEEeEErRREEEEeEReEE

Try it online!

\$\endgroup\$

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.