-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 '18 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 '18 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\$ – Kevin Cruijssen Apr 30 '18 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 '18 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 '18 at 13:38

53 Answers 53

1
2
1
\$\begingroup\$

IBM/Lotus Notes Formula, 16 bytes

@Power(10;i+1)-1

Simple port of my Python answer. One of the nice things about formula is that if you pass it a list it will apply the same function to each list member.

enter image description here

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

Befunge-98 (FBBI), 13 12 bytes

-1 byte thanks to Jo King

&#9':_@#\-1,

Try it online!

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

Symbolic Python, 40 37 bytes

__=-~(_==_)
_=(__+__**-~__)**-~_+~_+_

Try it online!


Ungolfed

# implicit input stored in _
__  = -~(_==_)             # __ = -~True = 1 + True = 1 + 1 = 2
_=(__+__**-~__)**-~_+~_+_  # _  = (2 + 2 ** -~2) ** -~_ + ~_ + _ 
                                = (2 + 2**3) ** (_ + 1) - -~_ + _
                                = 10 ** (_ + 1) - (_ + 1) + _
                                = 10 ** (_ + 1) - 1
# implicit output of _

other 37 byte solutions

__=_==_
_=(-~__*-~-~-~-~__)**-~_+~_+_

Try it online!

_=(-~(_==_)*-~-~-~-~(_==_))**-~_+~_+_

Try it online!


Symbolic Python, 29 bytes

multiplies the string '9' by _ + 1

_=`-~-~(_==_)*-~-~(_==_)`*-~_

Try it online!

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

Tcl, 26 bytes

puts [expr 10*10**$argv-1]

Try it online!

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

Common Lisp, 30 bytes

(lambda(n)(1-(expt 10(1+ n))))

Try it online!

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

Charcoal, 5 4 bytes

×9⊕N

Try it online!

Explanation

×9     Repeat "9"
  ⊕N ++(next number as input) times
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 18 bytes

Just throwing out a recursive solution to the problem:

f=n=>n?9+f(n-1):''

Evaluates the expression ''+9+9+9+9... (depending on n).

Because the first operand is a string, all the + operators are treated as concatenation instead of addition.

In my Chrome, works up to f(11424) before hitting a stack overflow.

f=n=>n?9+f(n-1):''

for(i = 0; i < 100; i += 10) {
  console.log(f(i));
}

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

PHP, 25 bytes

Try it online

Code

(Passing $arguments to the script)

<?=str_repeat(9,$argv+1);

Or a recursive function, 42 Bytes

Try it online!

function f($u){echo($u<0)?"":"9".f($u-1);}
\$\endgroup\$
0
\$\begingroup\$

Phooey, 11 bytes

&.+1[$i9-1]

Try it online!

Explanation

&.+1[$i9-1]
&.              take numeric input
  +1            add 1
    [   -1]     until that is zero:
     $i9        output 9s
\$\endgroup\$
0
\$\begingroup\$

Attache, 9 bytes

{N!-~_&9}

Try it online!

Alternatives

10 bytes: {"9"*t-~_}

13 bytes: (pointfree) 9&Resize@1&`+

Explanation

{N!-~_&9}     
{       }     anonymous lambda taking input `_`
   -~_        input + 1
      &9      9s repeated that much (array of 9s)
 N!           cast to integer
\$\endgroup\$
0
\$\begingroup\$

Red, 24 bytes

func[n][loop n[prin"9"]]

Outputs a string

Try it online!

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

Python 2, 20 18 bytes

lambda i:10**-~i-1

Try it online!

Don't think it needs an explanation.

-2 with thanks to @ovs

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

Java 8, 27 25 bytes

n->n+=Math.pow(10,n+1)+~n

Try it online.

This is basically a shorter version of n->(long)Math.pow(10,n+1)-1, because n+=...-n saves a byte in comparison to (long).... (And combining -n-1 to +~n saves a second byte in this case.)

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

Excel, 12 bytes

=10^(A1+1)-1

Or alternatively, to better handle larger values (>10) for 13 bytes

=REPT(9,A1+1)
\$\endgroup\$
0
\$\begingroup\$

Ruby, 11 bytes

->n{?9*-~n}

Try it online!

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

Swift 4, 47 34 32 bytes

-13 -15 bytes thanks to @Mr. Xcoder

{(0...$0).map{_ in"9"}.joined()}

Try it online!

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

Japt -P, 3 bytes

ò@9

Try it


Explanation

ò       :Range [0,input]
 @      :Pass each through a function
  9     :  Return 9
        :Implicitly join and output
\$\endgroup\$
0
\$\begingroup\$

Cubix, 15 bytes

Iu9)^>Os(?@.>sW

Try it online!

Expands to the following cube:

    I u
    9 )
^ > O s ( ? @ .
> s W . . . . .
    . .
    . .
\$\endgroup\$
0
\$\begingroup\$

Triangularity, 31 bytes

...)...
..IE@..
.)10^).
@_+....

Try it online!

Also 31 bytes

...)...
..IE)..
.@_rM..
"9"}""J

Try it online!

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

GolfScript, 6 bytes

~)'9'*

Try it online!

Explanation:

~)'9'* Full program, implicit input
       Stack: '5'
~      Evaluate
       Stack: 5
 )     Increment
       Stack: 6
  '9'  Push '9'
       Stack: 6 '9'
     * Repeat
       Stack: '999999'
       Implicit output

GolfScript, 7 bytes

~)10\?(

Try this one!

Explanation:

~)10\?( Full program, implicit input
        Stack: '5'
~)      Evaluate and increment (as above)
        Stack: 6
  10\?  Raise 10 to that power
        Stack: 1000000
      ( Decrement
        Stack: 999999
        Implicit output
\$\endgroup\$
0
\$\begingroup\$

Stax, 4 bytes

^|Av

Run and debug it

Packed would not be any shorter.

Explanation:

^|Av Full program, implicit input
^    Increment
 |A  Raise ten to that power
   v Decrement
     Implicit output

Stax, 4 bytes

^'9*

Run and debug it

^'9* Full program, implicit input
^    Increment
 '9* Repeat "9"
     Implicit output
\$\endgroup\$
0
\$\begingroup\$

C (gcc) (-lm), 22 bytes

f(n){n=pow(10,-~n)-1;}

Try it online!

Saved 4 bytes thanks to Kevin Cruijssen.

\$\endgroup\$
2
  • \$\begingroup\$ Why the int? Just f(n){n=pow(10,-~n)-1;} also works, doesn't it? \$\endgroup\$ – Kevin Cruijssen May 1 '18 at 7:54
  • \$\begingroup\$ Suggest exp10(...) instead of pow(10,...) \$\endgroup\$ – ceilingcat Apr 19 at 3:13
0
\$\begingroup\$

Prolog (SWI), 34 23 20 bytes

X+Y:-Y is 10*10^X-1.

Try it online!

Call as <input>+<result variable>.

\$\endgroup\$
1
2

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.