12
\$\begingroup\$

For this challenge, you must create a programme which takes an integer x and outputs its source x many times.

Rules

  • This is codegolf, the aim is to golf your programme to have the least amount of bytes

  • If you submit a function, the function must take x as a parameter and either return or print the entirety of your code x many times to STDOUT. The function body must also not be empty

  • If you submit a lambda, it is not required for you to assign it to a variable

  • Standard loophole restrictions apply.

  • Empty programmes are forbidden

  • Your programme only has to work when x is a whole integer larger than or equal to 0

  • Your programme may also not directly read any part of its source code

\$\endgroup\$
  • \$\begingroup\$ Nice first Question \$\endgroup\$ – Bald Bantha May 18 '17 at 21:39
  • \$\begingroup\$ Can x be 0, or can we restrict it to positive integers? \$\endgroup\$ – mbomb007 May 18 '17 at 21:58
  • \$\begingroup\$ Is reading the source of our function permitted? \$\endgroup\$ – Shaggy May 18 '17 at 22:00
  • \$\begingroup\$ @mbomb007, I've made an edit to clarify the rules, x maybe any whole integer larger than or equal to 0 \$\endgroup\$ – Dignissimus - Spammy May 18 '17 at 22:03
  • \$\begingroup\$ @Shaggy, I thought that was a standard loophole, it seems as if it was not. I've made an edit to the question to clearly state that a programme may not read any part of the part of its source code \$\endgroup\$ – Dignissimus - Spammy May 18 '17 at 22:09

27 Answers 27

8
\$\begingroup\$

Python 2, 50 bytes

Note the trailing comma and newline.

s='s=%r;print s%%s*input(),\n';print s%s*input(),

Try it online!

\$\endgroup\$
  • \$\begingroup\$ @mbomb007 Assume there is a newline at the end of the code. Problem solved. \$\endgroup\$ – MD XF May 18 '17 at 22:47
  • \$\begingroup\$ The OP said that x can be zero, in which case nothing should be printed, so this answer is wrong. \$\endgroup\$ – mbomb007 May 19 '17 at 13:42
  • \$\begingroup\$ @NickA No, it prints a newline, which is wrong. \$\endgroup\$ – mbomb007 May 19 '17 at 14:01
  • \$\begingroup\$ 0 doesn't seem to print a newline to me, as tested by using sys.stdout.write("2") after it. \$\endgroup\$ – Ørjan Johansen May 20 '17 at 2:21
5
\$\begingroup\$

Japt, 14 bytes

"iQ ²pU"iQ ²pU

Try it online!

Explanation

"iQ ²pU"iQ ²pU
"iQ ²pU"       # Take this string
        iQ     # Prepend a quote
           ²   # Double the string (= source code)
            pU # Duplicate input times
\$\endgroup\$
4
\$\begingroup\$

RProgN 2, 8 bytes

{`{.*¶}{

The RProgN2 Loop quine works particularly well for this!

{     } # A function literal
       {# Without a matching }, this loops back to the second instruction, which essentially runs the function. This is a bug, but it works to make the "Looping Quine".
 `{.    # Append the literal {, stringifying the function.
    *   # Multiply the stringified function by the input.
     ¶  # Terminate the program.

Try it online!

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

Mathematica, 40 33 bytes

Thanks to lanlock4 for saving 7 bytes!

StringRepeat[ToString[#0], #1] & 

Pure function taking a nonnegative integer argument. ToString[#0] is the standard Mathematica way to access the current pure function's definition; StringRepeat[..., #1] concatenates (input) copies of that string together. For example,

StringRepeat[ToString[#0], #1] & [2]

yields:

StringRepeat[ToString[#0], #1] & StringRepeat[ToString[#0], #1] & 
\$\endgroup\$
  • \$\begingroup\$ Could you post a usage example? Running, e.g., StringJoin @@ Table[ToString[#0], #1] & [2] gives me an error. \$\endgroup\$ – Julian Wolf May 18 '17 at 22:44
  • \$\begingroup\$ Really? That exact call works for me. I'm using Mathematica 11.... \$\endgroup\$ – Greg Martin May 18 '17 at 23:12
  • \$\begingroup\$ Hmm, I'm still using 10 (v10.0.1.0)—that could be it. For reference, here's the error that I get: Table::itform: Argument 2 at position 2 does not have the correct form for an iterator. \$\endgroup\$ – Julian Wolf May 18 '17 at 23:19
  • \$\begingroup\$ Aha—I discover that Table[x,5] will return {x,x,x,x,x} in Mathematica 10.2 and beyond, but in Mathematica 10.1 it gives such an error (it expects Table[x,{5}]). \$\endgroup\$ – Greg Martin May 19 '17 at 1:24
  • \$\begingroup\$ I think you can replace StringJoin @@ Table with StringRepeat. \$\endgroup\$ – Not a tree May 19 '17 at 5:23
3
\$\begingroup\$

V, 11 bytes

ÀñAÀÑ~"qpx

Try it online!

This is an extremely trivial modification of the standard extensible V quine. We simply use À to run it arg1 times.

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

dc, 31 bytes

[[1-rd91Pn93P[dx]Pdx]s.rd0<.]dx

Try it online!

Explanation:

[[1-rd91Pn93P[dx]Pdx]s.rd0<.]dx
[     91Pn93P[dx]P          ]dx  # Same old regular quine
 [1-rd            dx]s.rd0<.     # Loop until the counter is zero
\$\endgroup\$
3
\$\begingroup\$

Python 2, 70 bytes

This solution works if x=0. There is a single trailing newline.

s='s=%r;exec"print%%r;"%%(s%%s)*input()';exec"print%r;"%(s%s)*input()


Try it online


Python 2, 60 bytes (invalid)

This assumes that x>=1, but the OP clarified that x can be zero. There is a single trailing newline.

s='s=%r;print(s%%s*input())[:-1]\n';print(s%s*input())[:-1]


Try it online

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

Underload, 12 bytes

(a(:^)*~^):^

Try it online!

Function submission, because Underload has no other way to take input. (The TIO link shows the number 4 given as input, and adds code to print the resulting output).

This is just a universal quine constructor (a(:^)*):^, plus ~^ ("make a number of copies equal to the argument").

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

Japt, 14 bytes

*E îQi"*E îQi"

Try it online!

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

Jelly, 10 bytes

“;⁾vṾẋɠ”vṾ

Try it online!

How it works

“;⁾vṾẋɠ”vṾ  Main link. No arguments.

“;⁾vṾẋɠ”    Set the argument and the return value to the string ';⁾vṾẋɠ'.
         Ṿ  Uneval; yield '“;⁾vṾẋɠ”'.
        v   Eval ';⁾vṾẋɠ' with argument '“;⁾vṾẋɠ”'.
  ⁾vṾ           Yield 'vṾ'.
 ;              Concatenate '“;⁾vṾẋɠ”' and 'vṾ', yielding the source code.
      ɠ         Read an integer from STDIN.
     ẋ          Repeat the source code that many times.
\$\endgroup\$
  • 4
    \$\begingroup\$ This looks delicious, but my mama always told me not to put strange looking things into my mouth. \$\endgroup\$ – Mateen Ulhaq May 19 '17 at 1:36
1
\$\begingroup\$

GNU Make, 49 bytes

$(foreach ,$(shell seq $1),$(strip $(value $0))) 

Make will join the copies by a single space, so I have to include the additional space character at the end and remove it with a strip in between in order to follow the requirement faithfully.

\$\endgroup\$
  • \$\begingroup\$ That looks like it reads the source code, which is both by default and explicitly forbidden. \$\endgroup\$ – Ørjan Johansen May 19 '17 at 0:55
  • \$\begingroup\$ @ØrjanJohansen No, it doesn't read the source, it reads a variable value. It's exactly the same principle as in other answers, take for example JS or Python. \$\endgroup\$ – eush77 May 19 '17 at 0:59
  • \$\begingroup\$ @ØrjanJohansen Oh, I see. $0 in Make functions is not the same thing as in shells. It's the name of the variable the function is called as. See gnu.org/savannah-checkouts/gnu/make/manual/html_node/… \$\endgroup\$ – eush77 May 19 '17 at 1:03
  • \$\begingroup\$ Sigh such a gray area. BTW, the Python ones do not do this. \$\endgroup\$ – Ørjan Johansen May 19 '17 at 1:12
  • \$\begingroup\$ @ØrjanJohansen I'd say that this answer is much closer to Python's than it is to JS's. All call is doing is substituting $0 and $1 with actual parameters — it's simple string interpolation, just like Python's %. \$\endgroup\$ – eush77 May 19 '17 at 1:20
1
\$\begingroup\$

Pyth, 17 bytes

j*]jN*2]"j*]jN*2]

Try it online!

Trivial extension to the pretty-well-known jN*2]"jN*2] quine, but can probably be golfed down

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

Betaload, 203 bytes

Newlines added for clarity:

(a(:^)*(!()):#(}:(:)~^a((::*:**:*)*)~*(~*)*~(*)~^a*(*)*{)>(0)(!()){:^}(1)(){:^}(2)(:*){:^}(
3)(::**){:^}(4)(:*:*){:^}(5)(::*:**){:^}(6)(:*::**){:^}(7)(::*::***){:^}(8)(:*:*:*){:^}(9)(
::**::**){:^}R^^S):^

I gave myself the restriction that it must read from STDIN rather than from the top of the stack like an Underload answer typically would. I also used proper, decimal input, which makes up most of the code.

Explanation:

I wrap the program up in a quine-wrapper: (a(:^)* and ):^. This means all code inside the quine wrapper will have the program's source code at the bottom of the stack.

In order to convert digits into a normal Church numeral, I use the technique of replacing each digit with the code to multiply by 10 and add that digit:

0 -> (::*:**:*)*
1 -> (::*:**:*)*(:)~*(*)*
2 -> (::*:**:*)*(::)~*(**)*
3 -> (::*:**:*)*(:::)~*(***)*

There's a lot of repetition here, so let's package it up into a subprogram that will take a Church numeral from the top of the stack and use it to construct the "digit string:"

:(:)~^a((::*:**:*)*)~*(~*)*~(*)~^a*(*)*

I put this into a new environment so that it can be accessed quickly:

#(}:(:)~^a((::*:**:*)*)~*(~*)*~(*)~^a*(*)*{)>

Now I can create the replacement code for R. R uses the top elements of the stack to form a lookup table to replace a string from STDIN with Betaload code. It works like this:

()
(0)(code for 0)
(1)(code for 1)
(2)(code for 2)
...
R

However, we can use the subprogram we just made to generate the code segments:

(0)(!()){:^}
(1)(){:^}
(2)(:*){:^}
(3)(::**){:^}
...

When R is run, it will transform the input into a series of subprograms that build up a Church numeral. When this subprogram is executed, it creates that Church numeral on the next element on the stack (0, which was placed down earlier). This means that, after R^, the top value on the stack will be the Church numeral. We then ^ one more time to apply the Church numeral to the final element in the stack (the program's source code) to get the answer.

Fun fact: I've had the MD for this submission for several months. I'd kept it after misunderstanding a question (that I can't seem to find anymore). I had to dig it up from my Recycle Bin to post it here.

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

05AB1E, 23 bytes

"34çìDJ¹.D»"34çìDJ¹.D»

Try it online!

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

Python 2, 41 bytes

_="input('_=%r;exec _'%_*input())";exec _

Try it online!

Does a kinda hacky output using input instead of print, since print has a weird bug involving printing a newline when it isn't supposed to.... Exits with an EOF error.

Explanation:

_="input('_=%r;exec _'%_*input())"; # Set _ to a string
exec _                              # Execute that string
input(                        )     # "print"
      '_=%r;exec _'%_               # The source code with _ formatted in
                     *input()       # Actual input many times
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 27 25 bytes

"34çs«DJ¹FD,"34çs«DJ¹FD,

Try it online!

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

Pyth, 13 bytes

*jN*2]"*jN*2]

Test suite

The standard Pyth quine plus two * for the repetition.

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

Perl, 48 bytes

print"$_\47"x(2*pop)for'print"$_\47"x(2*pop)for'

\47 is the octal escape for a single quote ('). It is interpreted inside double quotes ("), but not inside single quotes.

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

Javascript ES6, 27 37 bytes

_=>alert(`${f.name}=${f}`.repeat(_))

Edit

+10 bytes if f= should be also displayed


f=

_=>alert(`${f.name}=${f}`.repeat(_))

f(2);

\$\endgroup\$
  • \$\begingroup\$ @Downvoter why ? \$\endgroup\$ – Weedoze May 19 '17 at 12:40
  • \$\begingroup\$ @Kaiido I don't know.. Should I keep the f= ? \$\endgroup\$ – Weedoze May 19 '17 at 12:44
  • \$\begingroup\$ Your code is reading itself by implicitly calling toString on the function. \$\endgroup\$ – aebabis May 19 '17 at 19:27
  • \$\begingroup\$ @acbabis Calling f.toString() or f is the same but it won't display the function name \$\endgroup\$ – Weedoze May 22 '17 at 6:35
  • \$\begingroup\$ I believe what @acbabis meant is that it violates the last point of the challenge "Your programme may also not directly read any part of its source code" - using f that way is referring to its own source. \$\endgroup\$ – skyline3000 May 23 '17 at 14:32
0
\$\begingroup\$

CJam, 20 12 bytes

8 bytes saved thanks to Martin Ender

{"_~"+ri*}_~

Try it online!

Exaplanation

{       e# Begin a block literal:
 "_~"+  e#  Add whatever's on the stack to the beginning of the array ['_', '~'].
 ri*    e#  Repeat the resulting array a number of times equal to the input.
}       e# Close the block. Push it on the stack.
_~      e# Copy it and run it.
\$\endgroup\$
0
\$\begingroup\$

PHP, 194 bytes

<?php $a="PD9waHAgJGE9IiMiOyRpPSRhcmd2WzFdO3doaWxlKCRpLS0pZWNobyBzdHJfcmVwbGFjZShjaHIoMzUpLCRhLGJhc2U2NF9kZWNvZGUoJGEpKTs=";$i=$argv[1];while($i--)echo str_replace(chr(35),$a,base64_decode($a));

Try it online!

Not golfy at all, as b64 quines tend to be.

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

Go, 257 254 bytes

This pains me.

package main;import(."fmt";."strings";."strconv";."os");func main(){s:="package main;import(.\"fmt\";.\"strings\";.\"strconv\";.\"os\");func main(){s:=%q;n,_:=Atoi(Args[1]);Print(Repeat(Sprintf(s,s),n))}";n,_:=Atoi(Args[1]);Print(Repeat(Sprintf(s,s),n))}

Try it online!

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

Microscript II, 22 bytes:

"v{lqp}sN*h"v{lqp}sN*h

Microscript II, 20 bytes (but technically invalid as it accesses the source code of a code block):

{s""+`"~sN*"s`+}~sN*
\$\endgroup\$
0
\$\begingroup\$

C, 144 116 bytes

i;main(a){for(i=getchar()-48;i--;)printf(a="i;main(a){for(i=getchar()-48;i--;)printf(a=%c%s%c,34,a,34);}",34,a,34);}
\$\endgroup\$
0
\$\begingroup\$

Python 3, 69 Bytes

s='s=%r\nx=int(input())\nprint(s%%s*x)\n'
x=int(input())
print(s%s*x)
\$\endgroup\$
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 115 bytes

n=>{var s="n=>{{var s={0}{1}{0};for(int i=0;i++<n;)Write(s,(char)34,s);}}";for(int i=0;i++<n;)Write(s,(char)34,s);}

Try it online!

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

05AB1E, 17 bytes

0"D34çý×?"D34çý×?

Modification of the default 0"D34çý"D34çý by adding ×?.

Try it online.

Explanation:

0                   # Push 0 to the stack
                    #  STACK: [0]
 "D34çý×?"          # Push the string 'D34çý×?' to the stack
                    #  STACK: [0, 'D34çý×?']
          D         # Duplicate this string
                    #  STACK: [0, 'D34çý×?', 'D34çý×?']
           34ç      # Push '"' to the stack
                    #  STACK: [0, 'D34çý×?', 'D34çý×?', '"']
              ý     # Join the stack by this '"' delimiter
                    #  STACK: ['0"D34çý×?"D34çý×?']
               ×    # Repeat the string the (implicit) input amount of times
                    #  input = 2 → STACK: ['0"D34çý×?"D34çý×?0"D34çý×?"D34çý×?']
                ?   # Output to STDOUT without trailing newline
\$\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.