45
\$\begingroup\$

An uninteresting number (which I totally didn't make up only for this challenge) is created like this:

  1. Take a positive integer N
  2. Create a new number O by adding the digits of N at the end of N
  3. The final uninteresting number is O*N

For example for N=12:

  1. O = 1212
  2. O*N = 1212 * 12
  3. Final number is 14544

Input

A positive integer N (N > 0) or your language's equivalent. You don't have to catch incorrect input.

Output

The corresponding uninteresting number.

Test cases

  1 -> 11
  2 -> 44
  3 -> 99
 10 -> 10100
174 -> 30306276

Scoring

Shortest Code in Bytes wins.

\$\endgroup\$
19
  • 12
    \$\begingroup\$ There must be a relevant OEIS entry... \$\endgroup\$
    – MKII
    Jul 7, 2016 at 10:52
  • 2
    \$\begingroup\$ @Seims It was a joke, based on the "uninsteresting" name \$\endgroup\$
    – MKII
    Jul 7, 2016 at 11:10
  • 12
    \$\begingroup\$ @MKII my bad, i don't speak joke \$\endgroup\$
    – Seims
    Jul 7, 2016 at 11:13
  • 1
    \$\begingroup\$ Is taking the number as a string argument bending the rules a bit too much? \$\endgroup\$ Jul 7, 2016 at 11:39
  • 2
    \$\begingroup\$ Go ahead, bend the rules! :P \$\endgroup\$
    – Seims
    Jul 7, 2016 at 11:44

86 Answers 86

39
\$\begingroup\$

05AB1E, 3 bytes

Ы*

Explained

Ð    # triplicate input
 «   # conactenate
  *  # multiply

Try it online

\$\endgroup\$
11
  • 2
    \$\begingroup\$ Ahh, nice! Ninja'd me by seconds :p. \$\endgroup\$
    – Adnan
    Jul 7, 2016 at 10:44
  • 3
    \$\begingroup\$ @Adnan Hehe. Revenge for that time you did it to me :P \$\endgroup\$
    – Emigna
    Jul 7, 2016 at 10:46
  • 1
    \$\begingroup\$ 3 operations, 3 bytes, I don't think you could make it any shorter than that. \$\endgroup\$ Jul 7, 2016 at 11:20
  • 2
    \$\begingroup\$ @busukxuan Yep. Concatenate automatically converts the number to str and * interprets the string as a number. Very useful :) \$\endgroup\$
    – Emigna
    Jul 7, 2016 at 11:50
  • 2
    \$\begingroup\$ @busukxuan Yeah, a combination of Pyth and 05AB1E could have done it in 2 bytes :) \$\endgroup\$
    – Emigna
    Jul 7, 2016 at 12:01
30
\$\begingroup\$

JavaScript (ES6), 10 bytes

_=>(_+_)*_

Needs to be called with the argument as a String, not a Number.

Usage:

(_=>(_+_)*_)('3')
99

-3 bytes thanks to @Quill's suggestion.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ If you can pass the parameter as a string you can cut two bytes off this solution: _=>(_+_)*+_ \$\endgroup\$
    – Quill
    Jul 7, 2016 at 11:34
  • 3
    \$\begingroup\$ How exactly does this work? If I understand correctly, are you using _ as an arbitrary character for a variable? (PS - (_+_) totally looks like a butt) \$\endgroup\$ Jul 7, 2016 at 13:11
  • \$\begingroup\$ @charredgrass $ would also work \$\endgroup\$
    – cat
    Jul 7, 2016 at 13:13
  • 13
    \$\begingroup\$ type casting abuse eleven \$\endgroup\$
    – Downgoat
    Jul 7, 2016 at 15:41
  • 3
    \$\begingroup\$ Out of interest, the best I could do purely mathematically was 30 bytes in ES7 n=>(1+10**-~Math.log10(n))*n*n (sadly -~ have higher precedence than **) or 31 in ES6 n=>-~`1e${-~Math.log10(n)}`*n*n. Even recursion took me 32 bytes: f=(n,m=1)=>n<m?-~m*n*n:f(n,m*10) \$\endgroup\$
    – Neil
    Jul 7, 2016 at 18:48
26
\$\begingroup\$

Java 8, 29 26 25 21 Bytes

God bless lambda

c->new Long(c+""+c)*c

c->Long.decode(c+""+c)*c;

\$\endgroup\$
14
  • 30
    \$\begingroup\$ You gotta love Java; even with lambdas of Java 8 and one of the shortest Java answers ever here on codegolf, it's still outgolfed by all other current answers. xD \$\endgroup\$ Jul 7, 2016 at 11:15
  • 3
    \$\begingroup\$ java is bae, lambda is bae \$\endgroup\$
    – Seims
    Jul 7, 2016 at 11:31
  • 3
    \$\begingroup\$ @KevinCruijssen i still have a hope, one day java will win codegolf contest \$\endgroup\$
    – user902383
    Jul 7, 2016 at 11:51
  • 1
    \$\begingroup\$ After your edit you outgolfed @MartinEnder with his Retina answer by 1 byte! o.Ô \$\endgroup\$ Jul 7, 2016 at 12:39
  • 1
    \$\begingroup\$ @KevinCruijssen but still not enough to win or at least beat python:( \$\endgroup\$
    – user902383
    Jul 7, 2016 at 13:08
20
\$\begingroup\$

vim, 11

C<C-r>=<C-r>"<C-r>"*<C-r>"<cr>

crcrcrcrcr...

C       change (delete and enter insert mode) until the end of the line
<C-r>=  insert an expression via the special "expression register"
<C-r>"  insert the contents of the default register (what we just C'd)
<C-r>"  ... again
*       multiplied by
<C-r>"  the input (again)
<cr>    insert the result of this expression
\$\endgroup\$
5
  • \$\begingroup\$ 11 what? bytes? \$\endgroup\$
    – Insane
    Jul 7, 2016 at 22:35
  • 3
    \$\begingroup\$ @Insane Bytes if you call it from the command line, keystrokes if you do it directly from vim. I usually omit the unit from my vim answers because it can be either one. \$\endgroup\$
    – Doorknob
    Jul 7, 2016 at 22:44
  • \$\begingroup\$ v.tryitonline.net/#code=QxI9EiISIioSIgo&input=MTI To bad <C-r> is unprintable. \$\endgroup\$
    – DJMcMayhem
    Jul 9, 2016 at 7:56
  • \$\begingroup\$ Is <C-r> a carriage return? \$\endgroup\$ Jul 11, 2016 at 13:58
  • \$\begingroup\$ @CaptainMan No, <C-r> is control plus r. Carriage return is <cr>. \$\endgroup\$
    – Doorknob
    Jul 11, 2016 at 14:15
16
\$\begingroup\$

Pyth, 5 4 bytes

*s+`

Explanation:

    Q    input
   `     representation, basically str(Q)
  +  Q   add Q to its own string form
 s       parse int
*     Q  multiply by input
         print

Test it here.

\$\endgroup\$
15
\$\begingroup\$

Emacs, 17 bytes

(*SPACEC-SPACEC-EM-YSPACEC-YC-Y)C-J

Explanation

  • (*SPACE adds (* at point (before the number);
  • C-SPACEC-EM-Y Select and copy the number;
  • SPACE adds a space character at point (after the number);
  • C-YC-Y pastes two times the number at point;
  • ) adds ) at the end;
  • C-J interprets the line as a LISP expression and prints its result.

Exemple

Cursor represented by a pipe (|)

  • |174
  • (*SPACE (* |174
  • C-SPACEC-EM-Y (* 174|
  • SPACE (* 174 |
  • C-YC-Y (* 174 174174|
  • ) (* 174 174174)|
  • C-J

Result

(* 174 174174)
30306276|
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Hi, and welcome to PPCG! Nice first post! \$\endgroup\$
    – Riker
    Jul 7, 2016 at 14:54
14
\$\begingroup\$

Python 2.7, 21 bytes:

lambda f:int(`f`*2)*f

Well, this has to be the shortest Python answer I have ever written in the shortest amount of time ever. It's an anonymous lambda function that can be executed by naming it anything you want and then calling it like a normal function wrapped in print(). For instance, if your input is 12, and the function was named H, this this would be called like print(H(12)).

Try It Online! (Ideone)

Note that this only works for values up and equal to 9223372036854775807 since any higher value and repr() puts a L at the end of the integer. Therefore, for values greater than 9223372036854775807, this 24 byte version would be the one that works:

lambda f:int(str(f)*2)*f

Try This Online! (Ideone)

\$\endgroup\$
9
  • \$\begingroup\$ I still find Phytons String operations magical... \$\endgroup\$
    – Seims
    Jul 7, 2016 at 10:34
  • \$\begingroup\$ @Seims in what way? \$\endgroup\$
    – busukxuan
    Jul 7, 2016 at 11:36
  • \$\begingroup\$ String multiplication and addition. Haven't seen that often. \$\endgroup\$
    – Seims
    Jul 7, 2016 at 11:37
  • \$\begingroup\$ @Seims I guess you mostly deal with static languages then? \$\endgroup\$
    – busukxuan
    Jul 7, 2016 at 11:42
  • 1
    \$\begingroup\$ It's sad that a third of this answer is Python's verbose lambda syntax \$\endgroup\$
    – Cyoce
    Feb 22, 2017 at 23:59
12
\$\begingroup\$

C#, 19 23 bytes

n=>int.Parse(""+n+n)*n;

Without strings, 47 bytes

n=>{int i=1;while(i<=n)i*=10;return(i+1)*n*n;};
\$\endgroup\$
3
  • 4
    \$\begingroup\$ This is a snippet, not a full program or function. It would be valid with e.g. (n)=>{int.Parse(""+n+n)*n}2 \$\endgroup\$
    – cat
    Jul 7, 2016 at 13:12
  • \$\begingroup\$ @cat better? do I need the trailing ;? \$\endgroup\$
    – weston
    Jul 7, 2016 at 13:22
  • \$\begingroup\$ I don't know. See also Defaults for Code Golf and Tips for golfing in C# \$\endgroup\$
    – cat
    Jul 7, 2016 at 16:12
11
\$\begingroup\$

Jelly, 4 Bytes

;DḌ×

Try it online

Explanation

;DḌ×    Main link. argument : N

 D      Decimal; Yield the digits of N
;       Concatenate N and its digits
  Ḍ     Convert to integer; We get O
   ×    Multiply O and N
\$\endgroup\$
3
  • 1
    \$\begingroup\$ It's a really happy winking face with a goatee! ;DDx \$\endgroup\$
    – cat
    Jul 7, 2016 at 16:11
  • \$\begingroup\$ In which encoding does take only 1 byte? Usually we use UTF-8, in which it takes 3 (and the × takes 2, but it is 1 byte in e.g. ISO8859-1). \$\endgroup\$
    – o11c
    Jul 9, 2016 at 1:49
  • \$\begingroup\$ @o11c Jelly uses its own custom code page where these characters are one byte each. \$\endgroup\$ Jul 12, 2016 at 17:34
10
\$\begingroup\$

J, 7 bytes

*,~&.":

Explanation

*,~&.":  Input: n
     ":  Format n as a string
 ,~&.    Reflect and join the string to make "nn"
         and parse the string to get a number
*        Multiply that number by n
\$\endgroup\$
4
  • \$\begingroup\$ +1. I couldn't even think that Under is working properly with string concatenation. What a great discovery for me! Thank you. \$\endgroup\$
    – Dan Oak
    Jul 15, 2016 at 6:04
  • \$\begingroup\$ Sadly, I think this should be in parenthesis, since it's a hook that is not working if typed directly * ,~ &.": n, and can't be used in formation of other verbs either. \$\endgroup\$
    – Dan Oak
    Jul 15, 2016 at 6:06
  • 1
    \$\begingroup\$ @dahnoak Here at PPCG, we only have to specify what is needed for a function, and so the above is all that is necessary to create a function in J. Then to invoke it using some input as an argument, it will either be in parentheses or stored in a variable. \$\endgroup\$
    – miles
    Jul 15, 2016 at 6:11
  • \$\begingroup\$ Ah, I got this, ty. \$\endgroup\$
    – Dan Oak
    Jul 15, 2016 at 9:50
10
\$\begingroup\$

C, 70 68 54 53 52 44

f(n){return(pow(10,(int)log10(n)+1)*n+n)*n;}

Previous version (48 bytes, no math functions), saved 16 bytes thanks to @LeakyNun, 1 byte thanks to @FryAmTheEggman, 4 bytes thanks to @TobySpeight:

f(n,d,i){for(i=d=n;d;d/=10)i*=10;return(i+n)*n;}

Call f() with one argument, the number, and it returns the corresponding uninteresting number.

Test program

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    while (*++argv) {
        int n = atoi(*argv);
        printf("%d -> %d\n", n, f(n));
    }
    return 0;
}

Test results:

$ ./84712 1 2 3 4 10 174
1 -> 11
2 -> 44
3 -> 99
4 -> 176
10 -> 10100
174 -> 30306276

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ f(n){int b=1;while(a)b*=10,a/=10;return(n+n*b)*n;} \$\endgroup\$
    – Leaky Nun
    Jul 7, 2016 at 13:08
  • \$\begingroup\$ This shouldn't work properly without including math.h, but you get away with it in GCC, where log10() and pow() are built-in, and the compiler merely warns about "incompatible implicit declaration of built-in function" rather than assuming (as it should) that they both return int. \$\endgroup\$ Jul 7, 2016 at 14:16
  • \$\begingroup\$ @Leaky - you didn't put anything into a... \$\endgroup\$ Jul 7, 2016 at 14:16
  • 1
    \$\begingroup\$ It's nice to see another answer that stays entirely within the arithmetic world (not doing string concatenation). :-) \$\endgroup\$ Jul 7, 2016 at 14:31
  • 2
    \$\begingroup\$ @Toby - String concatenation in C is incompatible with golfing. ;-) \$\endgroup\$
    – owacoder
    Jul 7, 2016 at 14:32
9
\$\begingroup\$

Dyalog APL, 7 bytes

⊢×#⍎⍕,⍕

string representation

⍕, prepend string representation

#⍎ make into number (in root namespace)

⊢× multiply by original number

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Those wrecked TIE fighters are funny! \$\endgroup\$
    – Luis Mendo
    Jul 7, 2016 at 11:13
  • 2
    \$\begingroup\$ @LuisMendo dyalog.com/blog/2015/12/apl-puns \$\endgroup\$
    – Adám
    Jul 7, 2016 at 12:09
  • 1
    \$\begingroup\$ The fork awakens :-D \$\endgroup\$
    – Luis Mendo
    Jul 7, 2016 at 12:28
  • \$\begingroup\$ I'm pretty sure those aren't bytes in any encoding, since they're not letterlike or very common. \$\endgroup\$
    – o11c
    Jul 9, 2016 at 1:51
  • \$\begingroup\$ @o11c Did you check out the preemptive link for the word "bytes", viz. meta.codegolf.stackexchange.com/a/9429/43319. \$\endgroup\$
    – Adám
    Jul 10, 2016 at 1:19
9
\$\begingroup\$

Retina, 27 20 bytes

^
$_$*: $_
:
$_$*:
:

Gets a bit slow for large inputs, because before the last the stage the result is represented in unary.

Try it online! (The first line enables a linefeed-separated test suite.)

Explanation

I'll use 2 as an example input (because the unary representations get a bit unwieldy for larger inputs).

Stage 1: Substitution

^
$_$*: $_

By matching the beginning of the string with ^ we simply prepend some stuff. $_ refers to the input string itself and $*: means we insert that many colons. So we get:

:: 22

Stage 2: Substitution

:
$_$*:

Now we match every : and again replace it with $_$*:. Of course, this time $_ doesn't evaluate to an integer (but to :: 22 in our example), but $* just looks for the first decimal in the string, so this evaluates to the input concatenated to itself (O in the challenge specification). We'll end up with N*O colons, followed by O:

:::::::::::::::::::::::::::::::::::::::::::: 22

Stage 3: Match

:

All that's left is counting the :s to convert from unary back to decimal, which is exactly what this stage does.

\$\endgroup\$
3
  • \$\begingroup\$ Ooh, so close to being as long as Java. Outgolfed it by just 2 bytes. +1 \$\endgroup\$
    – R. Kap
    Jul 7, 2016 at 11:21
  • \$\begingroup\$ @R.Kap Actually, Java 8 outgolfed it after removing 3 bytes! o.Ô \$\endgroup\$ Jul 7, 2016 at 12:36
  • 4
    \$\begingroup\$ Sorry, Java.... \$\endgroup\$ Jul 7, 2016 at 13:16
9
\$\begingroup\$

CJam, 8 bytes

ri_`_+i*

Try it online!

r     e# Read input
i     e# Convert to integer
_     e# Duplicate
`     e# Convert to string
_     e# Duplicate
+     e# Concatenate
i     e# Convert to integer
*     e# Multiply. Implicitly display
\$\endgroup\$
3
  • 2
    \$\begingroup\$ I was about to suggest not converting to in then immediately back to string but the naive approach (i.e. having never used CJam before) is r__+i\i*, which is the same length. \$\endgroup\$
    – anon
    Jul 7, 2016 at 11:34
  • \$\begingroup\$ @QPaysTaxes Ah nice. I noticed the same thing as you: why first convert it to int and then back to string again. I also never used CJam and didn't really look close enough at all the possible operators, so I was unable to find a solution on first glance. Thanks for sharing your approach without converting it back to string, even though it's the same byte-length. \$\endgroup\$ Jul 7, 2016 at 11:40
  • \$\begingroup\$ If there was a way to apply an operation to the whole stack in two bytes, it would be a byte shorter (something like r__+si*, where s is "apply this operation over the stack"), but I don't see anything like that \$\endgroup\$
    – anon
    Jul 7, 2016 at 11:43
8
\$\begingroup\$

Jelly, 8 6 bytes

ŒṘẋ2v×

Try it online!

Explanation

ŒṘẋ2v× - Main link. Left argument: the number to convert

     × - Multiply
    v  - an evaluation of the left argument
ŒṘ     - converted to string
  ẋ    - multiplied by
   2   - two and the left argument
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I don't think you need either of those ³s. \$\endgroup\$ Jul 7, 2016 at 11:41
8
\$\begingroup\$

Awk, 13 bytes

$0=($0$0)*$0

Set the line to 2 of itself multiplied by itself

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I don't know about all AWK's but $0*=$0$0 works with GAWK, which is the same idea just a couple bytes shorter. \$\endgroup\$
    – cnamejj
    Apr 9, 2022 at 19:56
7
\$\begingroup\$

Brachylog, 7 bytes

:?c:?*.

Explanation

:?c      Concatenate Input to itself
   :?*.  Output is that concatenation times Input
\$\endgroup\$
7
\$\begingroup\$

Python, 42 bytes

Pure arithmetic approach, without strings!

f=lambda n,m=1:m<=n and f(n,m*10)or-~m*n*n

Ideone it!

\$\endgroup\$
7
\$\begingroup\$

Matlab / Octave, 20 bytes

@(x)eval([x x 42 x])

This is an anonymous function that takes the input as a string.

Example use:

>> f = @(x)eval([x x 42 x])
f = 
    @(x)eval([x,x,42,x])
>> f('12')
ans =
       14544

Or try it online with ideone.

Explanation

The code builds a string by concatenating the input string twice, then the character * (that has ASCII code 42), then the string again. The concatenated string is then evaluated.

\$\endgroup\$
4
  • \$\begingroup\$ What does 42 mean? \$\endgroup\$
    – Leaky Nun
    Jul 7, 2016 at 14:45
  • 4
    \$\begingroup\$ @LeakyNun It's the Answer to the Ultimate Question of Life, the Universe, and Everything". Also, it happens to be the ASCII code for * \$\endgroup\$
    – Luis Mendo
    Jul 7, 2016 at 14:51
  • \$\begingroup\$ Ah. I was searching for something like the 42th function. \$\endgroup\$
    – Leaky Nun
    Jul 7, 2016 at 14:54
  • \$\begingroup\$ The code simply builds a string by concatenating the input string twice, then *, then the string again. The concatenated string is then evaluated. I'll edit that into the answer \$\endgroup\$
    – Luis Mendo
    Jul 7, 2016 at 14:58
7
\$\begingroup\$

Lua, 20 Bytes

Takes in a command-line argument, and outputs via STDOUT

a=...print((a..a)*a)

And ungolfed as @LeakyNun asked in the comment :)

a=...       -- alias for the first argument
print(
     (a..a) -- concatenate a with itself, equivalent to a:rep(2)
     *a)    -- multiply the resulting number by a
\$\endgroup\$
1
  • \$\begingroup\$ That can be a nice demonstration of type coercion... if you add the explanation in. \$\endgroup\$
    – Leaky Nun
    Jul 18, 2016 at 8:22
6
\$\begingroup\$

MATL, 6 bytes

tVthU*

Try it online!

tV     % Input number implicitly. Duplicate and convert to string
th     % Duplicate and concatenate the two equal strings
U      % Convert to number
*      % Multiply
\$\endgroup\$
6
\$\begingroup\$

zsh, 13 bytes

<<<$[$1$1*$1]

Takes input as a command line argument, outputs to STDOUT.

This only works in zsh, but here's 15 bytes in Bash using echo instead of <<<:

echo $[$1$1*$1]
\$\endgroup\$
6
\$\begingroup\$

Perl, 11 bytes

$_*=$_ x2

+ the p and l flags.

(run with perl -ple '$_*=$_ x2')

-2 bytes thanks to pipe.

\$\endgroup\$
4
  • \$\begingroup\$ Save two bytes: $_*=$_ x2 \$\endgroup\$
    – pipe
    Jul 7, 2016 at 16:56
  • \$\begingroup\$ I don't think you need -l \$\endgroup\$ Jul 7, 2016 at 17:56
  • \$\begingroup\$ @BradGilbertb2gills Yes I need it because without it, $_ x2 will produce ...\n...\n which when converted as a number by perl ends at the first \n \$\endgroup\$
    – Dada
    Jul 7, 2016 at 18:04
  • \$\begingroup\$ I was testing it with both Perl 5 and 6, and didn't notice that I forgot to remove the 6. \$\endgroup\$ Jul 7, 2016 at 18:08
6
\$\begingroup\$

Excel VBA, 35 Bytes

Sub called with number, msgbox returns answer

Sub B(a)
MsgBox (a & a) * a
End Sub

Alternative Excel VBA, 42 Bytes

Number given in formula, returns answer.

Function B(a)
B = (a & a) * a
End Function
\$\endgroup\$
7
  • \$\begingroup\$ Think about a MsgBox and a Sub. It will save you 13 Byte, if I count correctly \$\endgroup\$
    – GER_Moki
    Jul 7, 2016 at 18:38
  • \$\begingroup\$ I would need some form of input box to get the value, no? \$\endgroup\$
    – tjb1
    Jul 7, 2016 at 18:39
  • \$\begingroup\$ Try Sub B(a) MsgBox (a & a) * a End Sub \$\endgroup\$
    – GER_Moki
    Jul 7, 2016 at 18:42
  • \$\begingroup\$ That requires another sub to pass the value, I'm not sure that's allowed in golf. \$\endgroup\$
    – tjb1
    Jul 7, 2016 at 18:44
  • \$\begingroup\$ The Function must be called too ;) \$\endgroup\$
    – GER_Moki
    Jul 7, 2016 at 18:47
5
\$\begingroup\$

Pyke, 5 4 bytes

`+b*

Try it here!

`    -    str(input)
 +   -   ^+input  (convert to string implicitly)
  b  -  int(^)
   * - ^*input

Also 5 bytes with string inputs

+bRb*
+]mbB
\$\endgroup\$
5
\$\begingroup\$

PHP, 25 24 bytes

Short opening tags are useful for surprisingly few golfing challenges, luckily this is one of them. Unfortunately operator precedence is the opposite of the order you need to do them in so lots of brackets are needed.

<?=($a=$argv[1])*"$a$a";

edit: I realised that seeing as how I'm using brackets anyway I can effectively skip the concatenation operator by changing the written order of the operations around.

\$\endgroup\$
5
\$\begingroup\$

dc, 11 10 bytes

ddZAr^1+**

I knew that eventually I would find a use for the Z command!

Operation is fairly simple - count the digits, take 10 raised to that power and add one. This gives a multiplier that concatenates the number with itself. Then just multiply.

I/O uses the stack, as usual for dc.

Full program

This is what I used for the tests:

#!/usr/bin/dc
?
ddZAr^1+**
p

The two extra commands give us pipeline I/O.

Tests

$ for i in 1 2 3 10 174; do printf '%d -> ' $i; ./84712.dc <<<$i; done
1 -> 11
2 -> 44
3 -> 99
10 -> 10100
174 -> 30306276

Thanks are due to Sir Biden XVII (1 byte).

\$\endgroup\$
1
  • \$\begingroup\$ You can substitute A for 10 to save a byte. Well done! \$\endgroup\$
    – Joe
    Jul 7, 2016 at 20:38
4
\$\begingroup\$

Mumps, 11 bytes

R I W I_I*I

This is one of those rare golf challenges where the idiosyncrasies of Mumps can come in very handy. First, all variables are strings, and all math equations are strictly evaluated left-to-right (as in: not PEMDAS), so 1+2*4=12 in Mumps instead of =9 the way PEMDAS would. So, (barely) ungolfed:

R I ;     Read from stdin to variable I
W I_I*I ; Write out I concatenated with I, then multiplied by I.

Word of caution - because the flavour of Mumps that I'm using (InterSystems Ensemble) does not echo the carriage return for stdin, the input and output number will appear concatenated. To rectify that / increase readability, you'd need to add two bytes and add a manual CR/LF, thusly:

R I W !,I_I*I

However, as I didn't see that requirement in the rules of the challenge, I'm pretty sure that I'm good with the shorter code. If I'm mistaken, please feel free to LART me and I'll modify my answer. :-)

\$\endgroup\$
4
\$\begingroup\$

PowerShell, 25, 18 bytes

Thank you TessellatingHeckler for reminding me how much PS loves the pipeline.

New 18 bytes:

process{$_*"$_$_"}

Old 25 bytes:

param($a);[int]"$a$a"*$a

Explanation:

# new
process{$_*"$_$_"}
process{         } # runs code block once for each passed item
        $_*        # multiple the first parameter
           "$_$_"  # concatenate as a string for ease
                   # in this case, the order does the typecasting for us
# old
param($a);[int]"$a$a"*$a
param($a)                 # assigns the first passed parameter to variable $a
         ;                # line terminator
          [int]           # type cast string "$a$a" to int32
               "$a$a"     # convert $a$a to string for easy concatenation
                     *$a  # multiply by $a

Testing (save as boring.ps1):

# new
12 | .\boring.ps1
14544
174 | .\boring.ps1
30306276

# old
.\boring.ps1 12
14544
.\boring.ps1 174
30306276

Definitely not the winning answer, but fun regardless!

\$\endgroup\$
6
  • \$\begingroup\$ If you put the variables the other way around, int * string will implicitly cast the string to an int, and you can save 5 bytes of casting. process{$_*"$_$_"} is 18 bytes, and takes input from "stdin" (i.e. the pipeline), e.g. 174|script.ps1 \$\endgroup\$ Jul 7, 2016 at 19:51
  • \$\begingroup\$ Hmmm... Interesting point. I can also switch them using my same structure and achieve the same result: param($a);$a*"$a$a" \$\endgroup\$ Jul 8, 2016 at 14:59
  • \$\begingroup\$ Err, I take that back, mine is one byte longer! \$\endgroup\$ Jul 8, 2016 at 15:00
  • \$\begingroup\$ @TessellatingHeckler Careful with that answer, since PowerShell's REPL environment doesn't qualify for the program or function default. Something like param($n)$n*"$n$n" (what Darth had, without the ;) is the same length and isn't a REPL. \$\endgroup\$ Jul 12, 2016 at 19:18
  • \$\begingroup\$ @TimmyD why doesn't a test.ps1 file which reads from the pipeline count? Does a bash shell script reading from stdin not count either? \$\endgroup\$ Jul 12, 2016 at 19:21
4
\$\begingroup\$

Batch, 27 20 18 bytes

@cmd/cset/a%1%1*%1

Edit: Saved 7 bytes thanks to @TessellatingHeckler. Saved a further 2 bytes thanks to @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ.

\$\endgroup\$
6
  • \$\begingroup\$ set /a at the prompt outputs the result of the assignment. -> @cmd/c set/a n=%1%1*%1 for 22 bytes. \$\endgroup\$ Jul 7, 2016 at 19:55
  • \$\begingroup\$ @TessellatingHeckler Why bother assigning if you're outputting? \$\endgroup\$
    – Neil
    Jul 7, 2016 at 20:28
  • \$\begingroup\$ @TessellatingHeckler Huh, I already did this trick myself six weeks ago, and I've forgotten it already.. \$\endgroup\$
    – Neil
    Jul 8, 2016 at 12:35
  • \$\begingroup\$ @cmd/cset/a%1%1*%1 for 18. \$\endgroup\$ Jul 15, 2016 at 10:51
  • 1
    \$\begingroup\$ @Neil Nope, but I tested it (on Windows 10!) The cmd/c part is needed because the batch file executing tool is not cmd itself. \$\endgroup\$ Jul 15, 2016 at 15:38

Your Answer

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

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