Skip to main content
Commonmark migration
Source Link

###dc, 21

dc, 21

?dsn[lnpr1-d0!=F]dsFx

Try it online!

Ungolfed:

# Read a number from standard input (?), save it to register n (sn) and leave a copy on the stack (d)
?dsn

# Function F: print register n as many times as the top of the stack shows
[            # start of macro
  lnp        # print register n
  r          # swap the counter to the top
  1-         # decrement counter
  d          # duplicate counter for the comparison
  0 !=F      # if counter is not zero run F again
]dsFx        # end of macro, save it to F (sF) and execute (d x)

Run on the command line like this:

echo 5 | dc dcscript

Output:

5
5
5
5
5

This shorter script works for n>1 (15 bytes):

? [d d z 1- <F]dsFx f

This uses the depth of the stack to determine when to stop.

###dc, 21

?dsn[lnpr1-d0!=F]dsFx

Try it online!

Ungolfed:

# Read a number from standard input (?), save it to register n (sn) and leave a copy on the stack (d)
?dsn

# Function F: print register n as many times as the top of the stack shows
[            # start of macro
  lnp        # print register n
  r          # swap the counter to the top
  1-         # decrement counter
  d          # duplicate counter for the comparison
  0 !=F      # if counter is not zero run F again
]dsFx        # end of macro, save it to F (sF) and execute (d x)

Run on the command line like this:

echo 5 | dc dcscript

Output:

5
5
5
5
5

This shorter script works for n>1 (15 bytes):

? [d d z 1- <F]dsFx f

This uses the depth of the stack to determine when to stop.

dc, 21

?dsn[lnpr1-d0!=F]dsFx

Try it online!

Ungolfed:

# Read a number from standard input (?), save it to register n (sn) and leave a copy on the stack (d)
?dsn

# Function F: print register n as many times as the top of the stack shows
[            # start of macro
  lnp        # print register n
  r          # swap the counter to the top
  1-         # decrement counter
  d          # duplicate counter for the comparison
  0 !=F      # if counter is not zero run F again
]dsFx        # end of macro, save it to F (sF) and execute (d x)

Run on the command line like this:

echo 5 | dc dcscript

Output:

5
5
5
5
5

This shorter script works for n>1 (15 bytes):

? [d d z 1- <F]dsFx f

This uses the depth of the stack to determine when to stop.

Added a shorter alternative that does not work for n=1
Source Link
Thor
  • 2.9k
  • 2
  • 20
  • 23

###dc, 21

?dsn[lnpr1-d0!=F]dsFx

Try it online!

Ungolfed:

# Read a number from standard input (?), save it to register n (sn) and leave a copy on the stack (d)
?dsn

# Function F: print register n as many times as the top of the stack shows
[            # start of macro
  lnp        # print register n
  r          # swap the counter to the top
  1-         # decrement counter
  d          # duplicate counter for the comparison
  0 !=F      # if counter is not zero run F again
]dsFx        # end of macro, save it to F (sF) and execute (d x)

Run on the command line like this:

echo 5 | dc dcscript

Output:

5
5
5
5
5

This shorter script works for n>1 (15 bytes):

? [d d z 1- <F]dsFx f

This uses the depth of the stack to determine when to stop.

###dc, 21

?dsn[lnpr1-d0!=F]dsFx

Try it online!

Ungolfed:

# Read a number from standard input (?), save it to register n (sn) and leave a copy on the stack (d)
?dsn

# Function F: print register n as many times as the top of the stack shows
[            # start of macro
  lnp        # print register n
  r          # swap the counter to the top
  1-         # decrement counter
  d          # duplicate counter for the comparison
  0 !=F      # if counter is not zero run F again
]dsFx        # end of macro, save it to F (sF) and execute (d x)

Run on the command line like this:

echo 5 | dc dcscript

Output:

5
5
5
5
5

###dc, 21

?dsn[lnpr1-d0!=F]dsFx

Try it online!

Ungolfed:

# Read a number from standard input (?), save it to register n (sn) and leave a copy on the stack (d)
?dsn

# Function F: print register n as many times as the top of the stack shows
[            # start of macro
  lnp        # print register n
  r          # swap the counter to the top
  1-         # decrement counter
  d          # duplicate counter for the comparison
  0 !=F      # if counter is not zero run F again
]dsFx        # end of macro, save it to F (sF) and execute (d x)

Run on the command line like this:

echo 5 | dc dcscript

Output:

5
5
5
5
5

This shorter script works for n>1 (15 bytes):

? [d d z 1- <F]dsFx f

This uses the depth of the stack to determine when to stop.

Source Link
Thor
  • 2.9k
  • 2
  • 20
  • 23

###dc, 21

?dsn[lnpr1-d0!=F]dsFx

Try it online!

Ungolfed:

# Read a number from standard input (?), save it to register n (sn) and leave a copy on the stack (d)
?dsn

# Function F: print register n as many times as the top of the stack shows
[            # start of macro
  lnp        # print register n
  r          # swap the counter to the top
  1-         # decrement counter
  d          # duplicate counter for the comparison
  0 !=F      # if counter is not zero run F again
]dsFx        # end of macro, save it to F (sF) and execute (d x)

Run on the command line like this:

echo 5 | dc dcscript

Output:

5
5
5
5
5