Let's define a 2-quine as a program that prints its source code two times. For example, if the program
ABCprintedABCABC, 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
ABCprintedABCABCABC, 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 code-golf, so the program with the fewest bytes wins.
-
1\$\begingroup\$ Related. Also related. \$\endgroup\$Dennis– Dennis2016年11月11日 07:23:18 +00:00Commented Nov 11, 2016 at 7:23
-
1\$\begingroup\$ You should explicitly forbid programming languages where a 0-byte program is a quine. Otherwise I would say: "QBasic - 0 bytes!" \$\endgroup\$Martin Rosenau– Martin Rosenau2016年11月11日 08:32:27 +00:00Commented Nov 11, 2016 at 8:32
-
3\$\begingroup\$ @MartinRosenau Those are forbidden by default. \$\endgroup\$Martin Ender– Martin Ender2016年11月11日 08:56:20 +00:00Commented Nov 11, 2016 at 8:56
-
2\$\begingroup\$ I challenge someone to do this in a purely functional language, because I can't figure it out... \$\endgroup\$user42682– user426822016年11月11日 11:05:48 +00:00Commented Nov 11, 2016 at 11:05
-
1\$\begingroup\$ @CamilStaps Just did it in Underload, but that's a bit of a cheat. \$\endgroup\$Esolanging Fruit– Esolanging Fruit2017年01月31日 04:21:16 +00:00Commented Jan 31, 2017 at 4:21
4 Answers 4
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
-
\$\begingroup\$ 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. \$\endgroup\$DLosc– DLosc2016年11月12日 07:04:58 +00:00Commented Nov 12, 2016 at 7:04
-
\$\begingroup\$ @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. \$\endgroup\$Martin Rosenau– Martin Rosenau2016年11月12日 09:47:34 +00:00Commented Nov 12, 2016 at 9:47
-
\$\begingroup\$ Ah, that makes sense. \$\endgroup\$DLosc– DLosc2016年11月12日 09:49:41 +00:00Commented Nov 12, 2016 at 9:49
-
\$\begingroup\$ Why CR+LF and not LF? \$\endgroup\$Pavel– Pavel2017年01月31日 05:28:03 +00:00Commented Jan 31, 2017 at 5:28
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.
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.
Python 3.8 (pre-release), 117 bytes
for x in 1,0:
exec(s:="if x%2:a=1;d={}\nx or~input('for x in 1,0:\\n exec(s:=%r)#'%s*~-a*d[a-1]);a+=1;d[x]=a;x*=a")#
How it works :
for x in 1,0:allows me to iterate through the code twice- then I execute some code
#at the end comment thefor x in 1,0:if the "base" is repeated multiple times
the code executed :
if x%2: # execute if x=1 (happen only once at the start,
# after that x will always be a multiple of 2)
a=1
d={}
# if x==0: print the quine the right number of time then crash
x or~input('for x in 1,0:\\n exec(s:=%r)#'%s*~-a*d[a-1])
a+=1 # a increments itelf by one for each line of the code
d[x]=a # store (a-1)! and a
x*=a # x = a!
If the actual code is a n quine, it has (n-1)! times the "base" and should print it n! times:
- Because
ais incremented by 1 each line, after the first iteration of theforloop, a is equal to(n-1)!+1. - I then search
a-1intod(wich store relation beetween(k-1)!andk) to findn. - I multiply
nbya-1to getn! - I print this number times the "base"