# Quit Whining; Start Quining

Goal:

Write a function or program that, given an integer n, will output a program in the same language that is a quine with a period of n.

Description of Output Program:

The output program needs to be a program that, when run, ... will output a program... that will output a program... etc, until the nth program is the original (first) output program. So this is, in a way, a modular quine. That's how I think of it, anyway.

The resulting programs should all be distinct.

Example:

Input: 2

Program: modulus_quine(n)

Output: PQP → ...

Scoring:

This is , so shortest code (for the generating program) wins.

Brownie points (-0 bytes, +1 thumbs up?) are given to anyone whose generating program IS their modular quine. This is where your program takes the input and outputs the first modular quine in the sequence, which outputs the next, etc. The resulting output programs must not prompt the user for input (until it gets back to the generator.)

• @vihan1086 Nope. This was discussed in the sandbox. Taking an input and generating the quine is completely different. The program you're golfing isn't required to be a quine, but is rather the generator program/function. – mbomb007 Jul 29 '15 at 20:39
• The answer to the previous question doesn't take much modification (I managed it with a Levenshtein distance of 7) to get an answer to this one. – Peter Taylor Jul 29 '15 at 20:57
• @PeterTaylor Good for you. Not everyone will find it so easy. Maybe try a different language. – mbomb007 Jul 29 '15 at 21:49
• Should the generator and the quine programs be in the same language? – randomra Jul 29 '15 at 21:59
• @randomra Yes. Clarification added. – mbomb007 Jul 30 '15 at 14:08

# GolfScript, 16 bytes

{%}+")1$~":x+0x  Try it online in Web GolfScript. ### Example run $ echo -n 3 | golfscript quining.gs
{3 % ")1$~"}0)1$~
$echo -n 3 | golfscript quining.gs | golfscript {3 % ")1$~"}1)1$~$ echo -n 3 | golfscript quining.gs | golfscript | golfscript
{3 % ")1$~"}2)1$~
$echo -n 3 | golfscript quining.gs | golfscript | golfscript | golfscript {3 % ")1$~"}0)1$~  ### Generator  # (implicit) Read from STDIN. {%}+ # Concatenate the read input with the block. # For example, "3"{%}+ -> {3 %}. ")1$~":x     # Push that string and save it in the the variable x'.
+   # Inspect the string and concatenate it with the block.
# For example, {3 %}'")1$~"'+ -> {3 % ")1$~"}
0x # Push a 0 and the string stored in x'.


### Modular quine

For example, the modular quine {3 % ")1$~"}0)1$~ works as follows.

{          }      # Define a block.
3 %              # Take the integer on the stack modulo 3.
")1$~" # Push that string. 0) # Push 0 and increment. Pushes 1. 1$~ # Copy the block from the bottom of the stack and execute it.


The original copy of the block, the modified integer and the string the block pushed are printed, resulting in {3 % ")1$~"}1)1$~.

All printed programs have a trailing linefeed, which GolfScript prints automatically.

• {%}+")1$~":n+0 would be one byte shorter, but the original output program would lack the trailing newline present in all subsequent output programs. – Dennis Jul 30 '15 at 14:39 # Ruby, 100 bytes + brownie points Generating program/zero-state: ;puts <<2.sub(/2*/){$&[/./]?$':?2*gets.to_i}*2,2 ;puts <<2.sub(/2*/){$&[/./]?$':?2*gets.to_i}*2,2 2  Output for input of 3: 222;puts <<2.sub(/2*/){$&[/./]?$':?2*gets.to_i}*2,2 222;puts <<2.sub(/2*/){$&[/./]?\$':?2*gets.to_i}*2,2
2


This program, when run, will not ask for input but output itself with the initial 222 on the first 2 rows replaced with 22. That will output a version where each row begins with 2, which will output the original program.

"2" lives a varied life here as (in order of appearance) a repeated digit in a no-op numeric literal, a HEREDOC reference, a regex literal, a character literal, a numeric argument to String#*, a numeric argument to puts, a character in a HEREDOC string, and finally a HEREDOC delimiter.

• This is close, but not quite correct, since an input of 1 does not cause your program to print itself. Your program appends one too many 2s to the front of each of the first two lines for each n. – mbomb007 Jul 30 '15 at 21:15

# Python 3.8 (pre-release), 59 bytes

b=0;exec(s:=f"print('b=%d;exec(s:=%r)'%(-~b%{input()},s))")


Try it online!

• You're right, my bad! – Jitse Jan 27 at 14:53

# Underload, 29 bytes

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


Underload has no method to take input from the user, so I wrote a function rather than a complete program. The input is taken from the stack, the output is placed onto the stack (and can be printed with S).

Here's how to call it for an input of 1, ():

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


and it produces the following quine:

(a(:^)*a(S)*^):^


which runs like this:

(a(:^)*a(S)*^):^
(a(:^)*a(S)*^)(a(:^)*a(S)*^)^
(a(:^)*a(S)*^)a(:^)*a(S)*^
((a(:^)*a(S)*^))(:^)*a(S)*^
((a(:^)*a(S)*^):^)a(S)*^
(((a(:^)*a(S)*^):^))(S)*^
(((a(:^)*a(S)*^):^)S)^
((a(:^)*a(S)*^):^)S
program exits, with output (a(:^)*a(S)*^):^


As you can see, the modular quine of order 1 (i.e. just a normal quine) works by first generating a copy of itself, then generating a function to print that output, then running that function.

For higher orders, we just repeat the "generating a function to print that output" step (which is written in Underload as a(S)*). Here's how we call it for order 4 ((:::***) is 4):

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


Here's the program produced:

(a(:^)*a(S)*a(S)*a(S)*a(S)*^):^


which, when run, produces the following programs in turn:

((((a(:^)*a(S)*a(S)*a(S)*a(S)*^):^)S)S)S
(((a(:^)*a(S)*a(S)*a(S)*a(S)*^):^)S)S
((a(:^)*a(S)*a(S)*a(S)*a(S)*^):^)S
(a(:^)*a(S)*a(S)*a(S)*a(S)*^):^


The same technique probably works in other languages, although maybe not so concisely.

As a side note, Underload only has eight commands, so a hypothetical golfing version that represented each using 3 bits would use 29×3÷8 = 10⅞ bytes. That would make it the shortest solution here. Although it only has nine commands, one (()`) uses two different characters, so the previous calculation isn't quite correct.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.2001662403345108, "perplexity": 2414.8298175128803}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": false}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-10/segments/1581875145260.40/warc/CC-MAIN-20200220162309-20200220192309-00382.warc.gz"}