How many times will you have to golf a quine?

12

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

Dignissimus - Spammy

Posted 2017-05-18T21:16:20.627

Reputation: 449

Nice first Question – Bald Bantha – 2017-05-18T21:39:56.000

Can x be 0, or can we restrict it to positive integers? – mbomb007 – 2017-05-18T21:58:19.583

Is reading the source of our function permitted? – Shaggy – 2017-05-18T22:00:21.630

@mbomb007, I've made an edit to clarify the rules, x maybe any whole integer larger than or equal to 0 – Dignissimus - Spammy – 2017-05-18T22:03:19.980

@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 – Dignissimus - Spammy – 2017-05-18T22:09:17.417

I think the consensus for quines is that reading of source code is not permitted by default. I always hold out hope, though, because, without reading the source, creating quines make my brain itchy! – Shaggy – 2017-05-18T22:13:11.957

I disagree with the reading of sourcecode, as it "Defeats the purpose" of a quine. It's easy to cat $0, it's difficult to programmatically create yourself. – ATaco – 2017-05-19T01:13:25.903

@Spammy23: It's not a standard loophole, but it is a default rule for [tag:quine] challenges, so it was disallowed all along. That said, given that this question is solvable via universal quine constructors, I'm not sure how much it adds to existing quine challenges. – None – 2017-05-19T01:44:17.953

Related, but not a duplicate because the problem is significantly changed by not having to handle negative inputs. – None – 2017-05-19T04:58:45.683

"Your programme may also not directly read any part of its source code". So I guess using a Rexx function called Sourceline(x) which returns line x in the source is a no-no. :) – theblitz – 2017-05-19T08:29:47.557

I'm surprised this isn't a duplicate. – aebabis – 2017-05-19T19:37:42.457

@Spammy23 Does that mean no string literals? :P – SuperJedi224 – 2017-05-22T13:03:11.143

Answers

8

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!

totallyhuman

Posted 2017-05-18T21:16:20.627

Reputation: 15 378

@mbomb007 Assume there is a newline at the end of the code. Problem solved. – MD XF – 2017-05-18T22:47:14.697

The OP said that x can be zero, in which case nothing should be printed, so this answer is wrong. – mbomb007 – 2017-05-19T13:42:53.620

@NickA No, it prints a newline, which is wrong. – mbomb007 – 2017-05-19T14:01:25.757

0 doesn't seem to print a newline to me, as tested by using sys.stdout.write("2") after it. – Ørjan Johansen – 2017-05-20T02:21:56.710

5

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

Luke

Posted 2017-05-18T21:16:20.627

Reputation: 4 675

4

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] & 

Greg Martin

Posted 2017-05-18T21:16:20.627

Reputation: 13 940

Could you post a usage example? Running, e.g., StringJoin @@ Table[ToString[#0], #1] & [2] gives me an error. – Julian Wolf – 2017-05-18T22:44:43.813

Really? That exact call works for me. I'm using Mathematica 11.... – Greg Martin – 2017-05-18T23:12:53.637

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. – Julian Wolf – 2017-05-18T23:19:10.283

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}]). – Greg Martin – 2017-05-19T01:24:10.760

I think you can replace StringJoin @@ Table with StringRepeat. – Not a tree – 2017-05-19T05:23:10.047

I can once I know about the command! Thanks :) – Greg Martin – 2017-05-19T07:37:47.320

4

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!

ATaco

Posted 2017-05-18T21:16:20.627

Reputation: 7 898

3

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.

James

Posted 2017-05-18T21:16:20.627

Reputation: 54 537

3

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

mbomb007

Posted 2017-05-18T21:16:20.627

Reputation: 21 944

3

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

eush77

Posted 2017-05-18T21:16:20.627

Reputation: 1 280

2

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").

user62131

Posted 2017-05-18T21:16:20.627

Reputation:

2

Japt, 14 bytes

*E îQi"*E îQi"

Try it online!

Oliver

Posted 2017-05-18T21:16:20.627

Reputation: 7 160

1

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.

Dennis

Posted 2017-05-18T21:16:20.627

Reputation: 196 637

4This looks delicious, but my mama always told me not to put strange looking things into my mouth. – Mateen Ulhaq – 2017-05-19T01:36:43.933

1

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.

eush77

Posted 2017-05-18T21:16:20.627

Reputation: 1 280

That looks like it reads the source code, which is both by default and explicitly forbidden. – Ørjan Johansen – 2017-05-19T00:55:51.867

@Ø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. – eush77 – 2017-05-19T00:59:02.687

@Ø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 https://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/Call-Function.html#Call-Function

– eush77 – 2017-05-19T01:03:56.473

Sigh such a gray area. BTW, the Python ones do not do this. – Ørjan Johansen – 2017-05-19T01:12:58.710

@Ø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 %. – eush77 – 2017-05-19T01:20:56.413

1

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

clap

Posted 2017-05-18T21:16:20.627

Reputation: 834

1

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.

Esolanging Fruit

Posted 2017-05-18T21:16:20.627

Reputation: 13 542

1

05AB1E, 23 bytes

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

Try it online!

kalsowerus

Posted 2017-05-18T21:16:20.627

Reputation: 1 894

1

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

Jo King

Posted 2017-05-18T21:16:20.627

Reputation: 38 234

0

05AB1E, 27 25 bytes

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

Try it online!

Riley

Posted 2017-05-18T21:16:20.627

Reputation: 11 345

0

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.

Business Cat

Posted 2017-05-18T21:16:20.627

Reputation: 8 927

0

Pyth, 13 bytes

*jN*2]"*jN*2]

Test suite

The standard Pyth quine plus two * for the repetition.

isaacg

Posted 2017-05-18T21:16:20.627

Reputation: 39 268

0

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.

Grimmy

Posted 2017-05-18T21:16:20.627

Reputation: 12 521

0

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);

Weedoze

Posted 2017-05-18T21:16:20.627

Reputation: 931

@Downvoter why ? – Weedoze – 2017-05-19T12:40:08.677

@Kaiido I don't know.. Should I keep the f= ? – Weedoze – 2017-05-19T12:44:21.923

Your code is reading itself by implicitly calling toString on the function. – aebabis – 2017-05-19T19:27:28.737

@acbabis Calling f.toString() or f is the same but it won't display the function name – Weedoze – 2017-05-22T06:35:23.483

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. – skyline3000 – 2017-05-23T14:32:56.577

0

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.

Skidsdev

Posted 2017-05-18T21:16:20.627

Reputation: 9 656

0

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!

totallyhuman

Posted 2017-05-18T21:16:20.627

Reputation: 15 378

0

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*

SuperJedi224

Posted 2017-05-18T21:16:20.627

Reputation: 11 342

0

Python 3, 69 Bytes

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

MrGeek

Posted 2017-05-18T21:16:20.627

Reputation: 231

0

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);}

MD XF

Posted 2017-05-18T21:16:20.627

Reputation: 11 605

0

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

Kevin Cruijssen

Posted 2017-05-18T21:16:20.627

Reputation: 67 575

0

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!

Embodiment of Ignorance

Posted 2017-05-18T21:16:20.627

Reputation: 7 014