-3
\$\begingroup\$

Introduction

The challenge itself was something I came across and had to try and figure out for a personal of project of mine. I ended up branching out and asking family members if they could provide an equation to meet the requirements.

Note: I have (with the help of others) found a solution for this but now I'm just trying to see how others would approach this.

Challenge

Take the following scenario where the left hand side number is N (the input):

N    Output
0 -> 9
1 -> 99
2 -> 999
3 -> 9999
4 -> 99,999
5 -> 999,999
...
15 -> 9,999,999,999,999,999

So, essentially, for an input number N, your program must provide the highest integer with N + 1 decimal places.

Note: Commas are not required to separate the numbers, I placed them above purely for readability.

Criteria of Success

The criteria of success is based on the following:

  • Shortest in terms of code size
\$\endgroup\$
9
  • 1
    \$\begingroup\$ @Okx no, it has to be a single number. So 3 => 9999 and cannot be [9,9,9,9]. \$\endgroup\$
    – Script47
    Apr 29, 2018 at 19:10
  • 15
    \$\begingroup\$ Top of Hot Network Questions with a score of one. I'm thinking the idea about preventing HNQ when number of answers > score is a decent one. \$\endgroup\$
    – CAD97
    Apr 29, 2018 at 21:01
  • 3
    \$\begingroup\$ Someone should write an answer in 99, although I'm not sure how to do multiply/power in that esolang. ;) \$\endgroup\$ Apr 30, 2018 at 7:12
  • 3
    \$\begingroup\$ You're getting a lot of downvotes because of the overly complicated explanation for what is, essentially, either print n+1 9s or print 10^n-1 \$\endgroup\$
    – Jo King
    Apr 30, 2018 at 12:41
  • 3
    \$\begingroup\$ @Script47 It seems arbitrary to print N + 1 nines instead of just just N nines. \$\endgroup\$
    – Adám
    Apr 30, 2018 at 13:38

53 Answers 53

5
\$\begingroup\$

05AB1E, 3 bytes

Code:

>ยฐ<

Uses the 05AB1E encoding. Try it online!

Explanation:

>      # Increment the implicit input
 ยฐ     # Compute 10 ** (input + 1)
  <    # Decrement the result
\$\endgroup\$
4
\$\begingroup\$

Neim, 3 bytes

>9๐•ฃ

Explanation:

>      Increment input
 9๐•ฃ    Repeat 9

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Holy moly, 3 bytes? That's insanity. \$\endgroup\$
    – Script47
    Apr 29, 2018 at 19:17
  • 4
    \$\begingroup\$ @Script Nothing surprising. Javascript/Python takes 3 builtins, this also takes 3 builtins. \$\endgroup\$
    – DELETE_ME
    Apr 30, 2018 at 1:50
4
\$\begingroup\$

Proton, 11 bytes

(1+)+("9"*)

Try it online!

This is a function.

Explanation

(1+)+("9"*)  Function
 1+          Anonymous function; add the input to 1
      "9"*   Anonymous function; multiply "9" by the input
    +        Function Composition; add the input to 1 then multiply "9" by this number
(  ) (    )  Brackets for order of operations

:o proton beats python with its function composition for once :D

\$\endgroup\$
2
  • \$\begingroup\$ Alternatives with same byte-count: ("9"*)+(+9) or ("9"*)+(9+). +(+n) prepends, +(n+) appends to the string ("9"*) which is the same as in @HyperNeutrino's answer above. \$\endgroup\$ Apr 30, 2018 at 10:05
  • \$\begingroup\$ @KevinCruijssen Oh cool, thanks! I forgot that "9"+9 worked in Proton \$\endgroup\$
    – hyper-neutrino
    Apr 30, 2018 at 15:17
4
\$\begingroup\$

J, 7 bytes

Anonymous tacit prefix function

9^&.>:]

Try it online!

9^โ€ฆ]โ€ƒnine raised to the power of the argument

โ€ƒ&.โ€ฆโ€ƒwhile both nine and the argument are under the influence of

โ€ƒโ€ƒ>:โ€ƒincrement

I.e. increment nine and the argument, then power, then un-increment, i.e. decrement. Effectively ((9+1)^(n+1))-1.

\$\endgroup\$
4
  • \$\begingroup\$ Also 7 bytes for output as a string: '9'$~>: \$\endgroup\$ Apr 30, 2018 at 6:36
  • 1
    \$\begingroup\$ @GalenIvanov Sure, but this solution is way cooler :-) \$\endgroup\$
    – Adám
    Apr 30, 2018 at 8:40
  • 1
    \$\begingroup\$ Of course, your solution is incomparably cooler. it's always a pleasure to use &. :) \$\endgroup\$ Apr 30, 2018 at 9:30
  • 1
    \$\begingroup\$ @GalenIvanov Yes, I hope to get Under into APL next year, โข. \$\endgroup\$
    – Adám
    Apr 30, 2018 at 11:27
4
\$\begingroup\$

APL+WIN, 9 5 bytes

Thanks to ngn for -3 bytes and Adรกm for -1 byte

Prompts for integer input:

1โŽ•/โ•9
\$\endgroup\$
3
  • \$\begingroup\$ Save a byte with '9' โ†’ โ•9. \$\endgroup\$
    – Adám
    Apr 29, 2018 at 23:18
  • \$\begingroup\$ and another three with (1+โŽ•)โด -> 1โŽ•/ \$\endgroup\$
    – ngn
    Apr 29, 2018 at 23:35
  • \$\begingroup\$ @ngn Very clever! \$\endgroup\$
    – Adám
    Apr 30, 2018 at 8:28
4
\$\begingroup\$

99, 90 bytes

 999
9999 9 9
99999 99 9 9999 9
999 999 9999 9
			




99999
999 999 9
 999999 999
 9 9999

Try it online!

Possibly not optimal, but it was fun to write.

EDIT: looks like I was right, as Jo King outgolfed me by not incrementing the input and being smarter about the gotos.

 999			assign input to tri-nine
9999 9 9		assign 0 to quad-nine
99999 99 9 9999 9	assign 81 to quint-nine for printing
999 999 9999 9		increment tri-nine
			




99999			print 81/nine (the numeral nine)
999 999 9		decrement tri-nine
 999999 999		if tri-nine is zero exit program (goto outside program)
 9 9999			else goto line nine
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Well done. Too bad the byte-count isn't 99 as well. ;p \$\endgroup\$ Apr 30, 2018 at 13:11
4
\$\begingroup\$

99, 69 bytes

9999 9 9
99999 99 9 9999 9
 999






99999
 99 999
999 999 9
 9 9999

Try it online!

Appropriate (but it's a pity that this didn't end up at 99 bytes)

Explanation

9999 9 9           9999 = 9-9
99999 99 9 9999 9  99999 = 99-9+(9-9)-9 = 9*9
 999               999 = input*9



Filler to get up to line 9


99999      Print (9*9)/9 as a number
 99 999    Jump to line 99 if 999 is 9-9
999 999 9  999 = 999 - 9
 9 9999    Jump unconditionally to line 9
\$\endgroup\$
1
  • 2
    \$\begingroup\$ the number 9 now just looks weird to me. \$\endgroup\$
    – Giuseppe
    Apr 30, 2018 at 13:16
3
\$\begingroup\$

Actually, 3 bytes

uโ•คD

Explanation:

u    Increment input
 โ•ค   10 ** x
  D  Decrement

Try it online!

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

MATL, 6 bytes

Q10w^q

Try it online!

Explanation:

Q       % Grab input and increment by 1
 10     % Push 10
   w    % Swap stack
    ^   % Raise 10 to the power of input+1
     q  % Decrement by 1
\$\endgroup\$
3
\$\begingroup\$

Pyth, 4

*\9h

Online test.

Explanation

    Q  # Implicit input
   h   # Increment
*\9    # Repeat string "9" n+1 times
\$\endgroup\$
5
  • \$\begingroup\$ Nice, I got t^Th, also 4 bytes. \$\endgroup\$
    – hakr14
    Apr 30, 2018 at 2:24
  • \$\begingroup\$ I got *p\9 with length 4. pyth.herokuapp.com/… \$\endgroup\$ Apr 30, 2018 at 14:13
  • \$\begingroup\$ @NeverHopeless I see it works, but how? e.g. with input 4, I think this translates to print "9", 4 times; yet 5 "9"s are a printed? \$\endgroup\$ Apr 30, 2018 at 15:50
  • 1
    \$\begingroup\$ @DigitalTrauma, by enabling debugging I found that there are two print functions 'Pprint' and 'imp_print' in this statement imp_print(times(Pprint("9"),Q)) and it is written here: pyth.herokuapp.com/rev-doc.txt that p <any> Print A, with no trailing newline. **Return A**. So my understanding is that Pprint prints Q times and 'p' token return that character which is printed by 'imp_print' that is why it appears Q+1 times. \$\endgroup\$ May 2, 2018 at 5:45
  • \$\begingroup\$ @NeverHopeless Makes sene - thanks! \$\endgroup\$ May 2, 2018 at 17:03
3
\$\begingroup\$

dc, 8

A?1+^1-p

Try it online!

Explanation

A         # Push 10
 ?        # Push input
  1+      # Increment input
    ^     # Raise 10 to the (input + 1)th power
     1-   # Decrement
       p  # Print
\$\endgroup\$
3
\$\begingroup\$

Haskell, 14 13 bytes

f n=10*10^n-1

Or for string output 15 bytes, f n='9'<$[0..n]

Thanks to @EsolangingFruit for a byte.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ f n=10*10^n-1 saves a byte. \$\endgroup\$ Apr 30, 2018 at 4:13
3
\$\begingroup\$

Zsh, 19 bytes

Zsh does a better job than Bash (needs eval):

printf 9%.s {0..$1}

Try it online!

Alternative, 20 bytes

This works for both Zsh and Bash (due to Digital Trauma):

echo $[10**($1+1)-1]

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Shorter for bash: echo $[10**($1+1)-1] \$\endgroup\$ Apr 30, 2018 at 6:31
  • \$\begingroup\$ @DigitalTrauma: Oh nice, works for Zsh as well (edited it in)! \$\endgroup\$ Apr 30, 2018 at 8:19
2
\$\begingroup\$

Python 3, 16 bytes

lambda a:"9"*-~a

Try it online!

Just a simple anonymous function.

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

JavaScript (Node.js), 18 bytes

x=>"9".repeat(x+1)

Try it online!

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Or alternatively n=>10**-~n-1 \$\endgroup\$
    – Arnauld
    Apr 29, 2018 at 19:42
  • \$\begingroup\$ Though I never specified, it would be ideal to get it a int rather than a string. Nevertheless, good answer. \$\endgroup\$
    – Script47
    Apr 29, 2018 at 21:30
  • \$\begingroup\$ @Arnauld it seems that with with your code when n = 15 the output is 10000000000000000. \$\endgroup\$
    – Script47
    Apr 29, 2018 at 21:32
  • \$\begingroup\$ @Script47 JS can't support numbers greater than 9007199254740991. So, yes, it works up to n=14. \$\endgroup\$
    – Arnauld
    Apr 29, 2018 at 21:36
  • 4
    \$\begingroup\$ Alternative to @Arnauld's version: n=>"10e"+n-1 \$\endgroup\$ Apr 30, 2018 at 2:37
2
\$\begingroup\$

Jelly, 4 bytes

โ€˜โต*โ€™

Try it online!

โ€˜โต*โ€™ - Main link. Argument: n (integer)
โ€˜    - n+1
 โต*  - 10 ** (n+1)
   โ€™ - (10 ** (n+1)) - 1
\$\endgroup\$
2
\$\begingroup\$

Husk, 4 bytes

R'9โ†’

Try it online!

R'9โ†’  -- example input: 3
   โ†’  -- increment: 4
R'9   -- replicate the character '9': ['9','9','9','9']
      -- implicitly print "9999"
\$\endgroup\$
2
\$\begingroup\$

Canvas, 3 bytes

โ•ต9ร—

Try it here!

โ•ต    increment the input
 9   push "9"
  ร—  repeat the "9" input+1 times
\$\endgroup\$
2
\$\begingroup\$

Brain-Flak, 48 bytes

({}()){({}<(((((()()()){}()){}){}){}())>[()])}{}

Try it online!

This uses the -A to enable ASCII output. Bonus round: A version without -A.

Brain-Flak, 50 bytes

({}(())){({}<((({})(({}){}){}){})>[()])}{}({}[()])

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 46 bytes \$\endgroup\$
    – Jo King
    Apr 30, 2018 at 5:14
2
\$\begingroup\$

Momema, 21 bytes

a00+1*0-8 9a=+*0-+1_A

Try it online! Requires the -i interpreter flag.

Explanation

A brief rundown of Momema's features:

  • Its only data type is the unbounded integer. It has a double-ended tape (indexed by unbounded integers) where every number is initialized to zero.
  • Strings of digits are integer literals which evaluate to themselves. Leading zeroes are parsed as their own integers.
  • +ab evaluates to the sum of a and b.
  • -a evaluates to the negation of a.
  • *a evaluates to the value of cell a on the tape.
  • =a evaluates to 0 if a evaluates to 0, and = otherwise.
  • At the top level, ab stores b at cell a on the tape. Attempting to write a number to -8 writes its decimal representation to STDOUT instead.
  • At the top level, [w]a (where [w] is any string of lowercase letters) acts like "relative jump forward by a jump instructions that share the label [w]."
  • The Momema reference implementation accepts a flag -i, which activates interactive mode. In interactive mode, _[W] (where [W] is any string of uppercase letters) is called a hole and evaluates to a number read from STDIN. However, it caches the number with its label, so if a hole with the same label is ever evaluated again it will be reused.

Knowing this, here's how you expand this program:

a   0                                                                                       # do {
0   (+ 1 (* 0))                                                                             #   n = n + 1
-8  9                                                                                       #   print 9
a   (= (+ (* 0) (- (+ 1 _A))))                                                              # while (n != input)

(This still parses, by the way; the Momema parser treats parentheses the same way as whitespace.)


a   0

Jumps forward by 0 a instructions (i.e. does nothing). This is only here to serve as a jump target later.

0   (+ 1 (* 0))

Increments the value of cell 0 (initially 0) and stores it back in cell 0.

-8  9

-8 is memory-mapped for numeric I/O, so this outputs 9 to STDOUT.

a   (= (+ (* 0) (- (+ 1 _A))))

Compute tape[0] - (input + 1). If it is zero (i.e. both sides were equal), then = maps the result to 0, making it a no-op. Control runs off of the end of the program. However, if it is nonzero, this is a 1. Since there are no a instructions after here, it wraps around to the beginning of the program.

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

brainfuck, 24 23 bytes

thanks to Jo King for -1 byte

-[++>+[+<]>]>+<,+[->.<]

Try it online! Takes input as ASCII

\$\endgroup\$
2
  • \$\begingroup\$ Most people wouldn't begrudge you removing that leading > \$\endgroup\$
    – Jo King
    Apr 30, 2018 at 7:50
  • \$\begingroup\$ @JoKing The next time I should have a closer look while copying from the esolang page :). Thank you \$\endgroup\$
    – ovs
    Apr 30, 2018 at 7:54
2
\$\begingroup\$

Whitespace, 79 bytes

[S S S T    S T S N
_Push_10][S N
S _Duplicate_10][S N
S _Duplicate_10][S N
S _Duplicate_10][T  N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][N
S S N
_Create_Label_LOOP][S S S T N
_Push_1][T  S S T   _Subtract][S N
S _Duplicate][N
T   T   S N
_If_negative_Jump_to_Label_EXIT][S N
T   _Swap][S T  S S T   S N
_Copy_2st][T    S S N
_Multiply][S N
T   _Swap][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_EXIT][T   S S S _Add][T   N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Integer i = STDIN as integer
Integer j = 10
Start LOOP:
  i = i - 1
  If i is negative (-1): Go to function PRINT_AND_EXIT
  j = j * 10
  Go to next iteration of LOOP

function PRINT_AND_EXIT:
  j = j + i (Since i=-1 at this point, this is basically j = j - 1)
  Print j as integer
  Exit with error

Example run (n=4):

Command   Explanation                   Stack              HEAP    STDIN   STDOUT  STDERR

SSSTSTSN  Push 10                       [10]
SNS       Duplicate top (10)            [10,10]
SNS       Duplicate top (10)            [10,10,10]
SNS       Duplicate top (10)            [10,10,10,10]
TNTT      Read STDIN as integer         [10,10,10]         {10:4}  4
TTT       Retrieve                      [10,10,4]          {10:4}
NSSN      Create Label_LOOP             [10,10,4]          {10:4}
 SSSTN     Push 1                       [10,10,4,1]        {10:4}
 TSST      Subtract (4-1)               [10,10,3]          {10:4}
 SNS       Duplicate top (3)            [10,10,3,3]        {10:4}
 NTTSN     If neg.: Jump to Label_EXIT  [10,10,3]          {10:4}
 SNT       Swap top two                 [10,3,10]          {10:4}
 STSSTSN   Copy 2nd                     [10,3,10,10]       {10:4}
 TSSN      Multiply (10*10)             [10,3,100]         {10:4}
 SNT       Swap top two                 [10,100,3]         {10:4}
 NSNN      Jump to Label_LOOP           [10,100,3]         {10:4}

 SSSTN     Push 1                       [10,100,3,1]       {10:4}
 TSST      Subtract (3-1)               [10,100,2]         {10:4}
 SNS       Duplicate top (2)            [10,100,2,2]       {10:4}
 NTTSN     If neg.: Jump to Label_EXIT  [10,100,2]         {10:4}
 SNT       Swap top two                 [10,2,100]         {10:4}
 STSSTSN   Copy 2nd                     [10,2,100,10]      {10:4}
 TSSN      Multiply (100*10)            [10,2,1000]        {10:4}
 SNT       Swap top two                 [10,1000,2]        {10:4}
 NSNN      Jump to Label_LOOP           [10,1000,2]        {10:4}

 SSSTN     Push 1                       [10,1000,2,1]      {10:4}
 TSST      Subtract (2-1)               [10,1000,1]        {10:4}
 SNS       Duplicate top (1)            [10,1000,1,1]      {10:4}
 NTTSN     If neg.: Jump to Label_EXIT  [10,1000,1]        {10:4}
 SNT       Swap top two                 [10,1,1000]        {10:4}
 STSSTSN   Copy 2nd                     [10,1,1000,10]     {10:4}
 TSSN      Multiply (1000*10)           [10,1,10000]       {10:4}
 SNT       Swap top two                 [10,10000,1]       {10:4}
 NSNN      Jump to Label_LOOP           [10,10000,1]       {10:4}

 SSSTN     Push 1                       [10,10000,1,1]     {10:4}
 TSST      Subtract (1-1)               [10,10000,0]       {10:4}
 SNS       Duplicate top (0)            [10,10000,0,0]     {10:4}
 NTTSN     If neg.: Jump to Label_EXIT  [10,10000,0]       {10:4}
 SNT       Swap top two                 [10,0,10000]       {10:4}
 STSSTSN   Copy 2nd                     [10,0,10000,10]    {10:4}
 TSSN      Multiply (10000*10)          [10,0,100000]      {10:4}
 SNT       Swap top two                 [10,100000,0]      {10:4}
 NSNN      Jump to Label_LOOP           [10,100000,0]      {10:4}

 SSSTN     Push 1                       [10,100000,0,1]    {10:4}
 TSST      Subtract (0-1)               [10,100000,-1]     {10:4}
 SNS       Duplicate top (-1)           [10,100000,-1,-1]  {10:4}
 NTTSN     If neg.: Jump to Label_EXIT  [10,100000,-1]     {10:4}
NSSSN      Create Label_EXIT            [10,100000,-1]     {10:4}
TSSS       Add (10000+-1)               [10,99999]         {10:4}
TNST       Print as integer             [10]               {10:4}          99999
                                                                                   error

Stops program with error: No exit defined.

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

Japt, 5 4 bytes

ยฐUรฎ9

Try it online!

Shaved off that one byte thanks to Shaggy's clever eyes. You can see the edit history for a number of 5-byte solutions.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 4 bytes: °Uî9 \$\endgroup\$
    – Shaggy
    Apr 29, 2018 at 23:35
  • \$\begingroup\$ @Shaggy Nice! You should post it as an answer. \$\endgroup\$
    – Etheryte
    Apr 30, 2018 at 4:50
  • \$\begingroup\$ It's just a variation on your first solution so work away. Also, I have a 3 byte solution to post after lunch. \$\endgroup\$
    – Shaggy
    Apr 30, 2018 at 12:13
1
\$\begingroup\$

AWK, 26 bytes

{for(;a++<$1;){printf"9"}}

Try it online!

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

Julia, 14 bytes

f(n)="9"^(n+1)

That's if the result can be a string.

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

SNOBOL4 (CSNOBOL4), 30 bytes

	OUTPUT =DUPL(9,INPUT + 1)
END

Try it online!

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

Gol><>, 5 bytes

PR`9H

Try it online!

This is a Gol><> function which takes the stack content as input.

Example full program & How it works

1AG9G
PR`9H

1AG    Register row 1 as function G
   9G  Call G with stack [9]

P      Increment
 R`9   Push char '9' that many times
    H  Print the stack content as chars and halt
\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 7 bytes

10^(Ans+1)-1

Fairly straightforward...

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Yeah, no idea why this got flagged. \$\endgroup\$
    – Nissa
    Apr 30, 2018 at 1:21
  • \$\begingroup\$ @Ste Blame SE automatic bots. Why must answers here have explanation? \$\endgroup\$
    – DELETE_ME
    Apr 30, 2018 at 1:57
1
\$\begingroup\$

CJam, 6 bytes

qi)'9*

Try it online!

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

Perl 5, 7 bytes

$_=9x$_

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You don't need the quotes there. Try it online! \$\endgroup\$
    – Xcali
    Apr 30, 2018 at 1:03
  • \$\begingroup\$ Good spot, thx. \$\endgroup\$
    – steve
    Apr 30, 2018 at 5:14

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.