Write a continuous quine (quine-variant)

7

1

Let's define a 2-quine as a program that prints its source code two times. For example, if the program ABC printed ABCABC, it would be a 2-quine.

Let's define a 3-quine as a program that prints its source code three times. For example, if the program ABC printed ABCABCABC, it would be a 3-quine.

Let's define a n-quine as a program that prints its source code n times.

Your task is to write a 2-quine, that prints a 3-quine, that prints a 4-quine, that prints a 5-quine, etc...

For example, here is an example of a valid submission (which is fictional of course):

AB

which prints

ABAB

which prints

ABABABABABAB

which prints

ABABABABABABABABABABABABABABABABABABABABABABABAB

etc.

Remember, this is , so the program with the fewest bytes wins.

Oliver Ni

Posted 2016-11-11T06:38:52.797

Reputation: 9 650

1Related. Also related. – Dennis – 2016-11-11T07:23:18.400

1You should explicitly forbid programming languages where a 0-byte program is a quine. Otherwise I would say: "QBasic - 0 bytes!" – Martin Rosenau – 2016-11-11T08:32:27.893

3

@MartinRosenau Those are forbidden by default.

– Martin Ender – 2016-11-11T08:56:20.027

2I challenge someone to do this in a purely functional language, because I can't figure it out... – None – 2016-11-11T11:05:48.700

Related. – Oliver Ni – 2016-11-11T14:48:46.020

1@CamilStaps Just did it in Underload, but that's a bit of a cheat. – Esolanging Fruit – 2017-01-31T04:21:16.230

@CamilStaps I figured out how to do it in C but I can't because online IDEs for C lock the program's file's attributes up and I can't read them. Anyways, what you do is make sure your program is just 1 line of code and ends with // this way when it repeats itself the repeats are all commented out, that way you don't get errors for defining your main(){} function more than once. Your quine also contains a for loop for printing itself out and the number of times that loops is based on your file size which can be read from the pre-processor __FILE__. So the bigger the file, the more loops – Albert Renshaw – 2017-01-31T08:55:35.923

1

@AlbertRenshaw that's great, but C is not a purely functional language.

– None – 2017-01-31T08:57:07.773

@CamilStaps My apologies, misunderstood the definition of purely functional; thanks for the link! – Albert Renshaw – 2017-01-31T08:58:02.393

@AlbertRenshaw no problem. And of course, you can add your implementation to the answers here. That it does not work in an online IDE is not a problem as far as I know, especially since most people have a C compiler on their installation already :) – None – 2017-01-31T08:59:18.383

Answers

3

QBasic (224 bytes incl. trailing CR+LF)

(Tested with QBasic/MS-DOS on a virtual machine as well as with FreeBASIC with "-lang qb" setting)

a$="a$=:l=l+1:if l>y then f=f+1:y=y*f+y+f:while x<=y:x=x+1:print mid$(a$,1,3)+chr$(34)+a$+chr$(34)+mid$(a$,4):wend":l=l+1:if l>y then f=f+1:y=y*f+y+f:while x<=y:x=x+1:print mid$(a$,1,3)+chr$(34)+a$+chr$(34)+mid$(a$,4):wend

Martin Rosenau

Posted 2016-11-11T06:38:52.797

Reputation: 1 921

Nice to see QBasic as the first answer to a question! Check out my QBasic quine for some ideas that might be able to shorten this a good bit. Also, I don't think you need to count the trailing CR+LF.

– DLosc – 2016-11-12T07:04:58.963

@DLosc The 2-quine generated must contain CR+LF between the lines. For this reason the CR+LF is definitely a part of the program and I have to count it. – Martin Rosenau – 2016-11-12T09:47:34.750

Ah, that makes sense. – DLosc – 2016-11-12T09:49:41.830

Why CR+LF and not LF? – Pavel – 2017-01-31T05:28:03.933

1

Javascript ES6 (REPL), 52 bytes

var a=a||'';$=_=>a+=`$=${$};$();`.repeat(a?1:2);$();

Initial working implementation, may be golfable.

y.onclick=_=>z.innerText=eval("var a=a||'';$=_=>a+=`$=${$};$();`.repeat(a?1:2);$();".repeat(x.value))
<input id=x><button id=y>Submit!</button><pre id=z></pre>

Explanation

Based on my usual quine implementation ($=_=>`$=${$};$()`;$()).

var a||'' coerces a to an empty string if it isn't defined; otherwise, a is left as is. a will store our final result.

Each function call, a will be checked to see if it is empty/falsy. The first function call will append 2 copies of the quine because a is empty; subsequent calls will only append 1 copy because a is no longer empty.

The last function call will output the final value of a after finishing.

Mama Fun Roll

Posted 2016-11-11T06:38:52.797

Reputation: 7 234

1

Ruby, 154 bytes

s=DATA.read.lines;s+=s[0,2];n=s.size/4;k=1;n/=k+=1while n>0;puts s*k
__END__
s=DATA.read.lines;s+=s[0,2];n=s.size/4;k=1;n/=k+=1while n>0;puts s*k
__END__

The trailing newline matters.

This program first duplicates itself; the output triplicates itself; that output quadruplicates itself, etc.

Thus the n-th program is a repetition of these 4 lines, n! times.

s=DATA.read.lines;s+=s[0,2]; gets us the current source code as an array of lines. We take its length, divide it by 4, compute the inverse factorial, add one, then print s that many times.

Lynn

Posted 2016-11-11T06:38:52.797

Reputation: 55 648