# Quine permutation group [duplicate]

Write a program or function which, when run, outputs another program or function which is valid in the same language as the original program, and which itself will output another program or function that has the same properties. You must at one point end up back to your initial program or function.

## Example

Suppose you write program A. When run, it outputs program B. When you run B, it outputs program C. When you run C, it outputs program A, thus completing the circle. A, B and C are all valid in the same language and are all different by at least one character.

## Scoring

Your score is calculated as <Number of bytes of the initial program>/<number of iterations to get back to the initial program>

For instance, with the programs A, B and C in the previous example, the number of iterations is 3 (you have to run 3 times to get back to A) and the number of bytes is the number of bytes of A.

This incentivize both golfing your initial code, and having an initial program that leads to a longer chain.

The smallest score wins

• This incentivizes long programs with long cycles too much. – lirtosiast Jul 4 '15 at 0:00
• How about bytes/sqrt(log(cyclelength)), or even bytes/log(log(cyclelength))? – lirtosiast Jul 4 '15 at 0:07
• @ThomasKwa: Yup, we need a new scoring algorithm. – Dennis Jul 4 '15 at 0:13
• Could you explain the title? I can't see the connection with permutation groups. – Peter Taylor Jul 4 '15 at 7:03
• So why not use the standard term: ouroboros? Does that not sound cool enough? – Peter Taylor Jul 4 '15 at 9:41

# CJam, score → 0

{)1e9%"X$~"}0X$~


Try it online in the CJam interpreter.

The actual score of this program is obviously positive, but 1e9 can be replaced with 1e99, 1e999, 1e9999, 1e99999, etc.

The numerator of the score grows much slower than the denominator.

### How it works

{          }     e# Define a block:
)               e#   Increment the integer on the stack.
1e9%           e#   Take the incremented integer modulo 1,000,000,000.
"X$~" e# Push that string. 0 e# Push 0. X$~ e# Copy the block and execute the copy.

e# The stack now holds the block, the modified integer and
e# the string "X$~", all of which CJam prints before exiting.  Executing the code prints {)1e9%"X$~"}1X$~ which, when executed, prints {)1e9%"X$~"}2X$~, etc. After reaching {)1e9%"X$~"}999999999X$~, one more execution yields the original code. • Well that escalated quickly... – Fatalize Jul 4 '15 at 0:06 • Now do you see how easy it is to make a long cycle? – lirtosiast Jul 4 '15 at 0:09 • @Fatalize If I make something that approaches$0$faster, do I win. Also, Dennis, how fast does yours approach$0$, asymptotically? – PyRulez Jan 24 '16 at 4:22 • @PyRulez By simply replacing the exponent in 1e9 with a larger number, the score of a program of length L is L / 10^(L - 15) for L > 15. There are unlimited ways to reduce the score faster though. – Dennis Jan 24 '16 at 4:30 # Javascript ES6, 68/1.8e16 (basically 0) $=_=>$=${$};$(0).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(0)


Based on my Bling Quine framework:

$=_=>$=${$};$()  Here's the output sequence starting from $(0):

$=_=>$=${$};$(1).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(0)$=_=>$=${$};$(1).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(1)
$=_=>$=${$};$(2).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(1)$=_=>$=${$};$(2).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(2)
$=_=>$=${$};$(3).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(2)$=_=>$=${$};$(3).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(3)
$=_=>$=${$};$(4).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(3) ...$=_=>$=${$};$(8999999999999999).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(8999999999999999)
$=_=>$=${$};$(0).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(8999999999999999)$=_=>$=${$};$(0).replace(eval(/(${_})\\)/),x=>++_%9e15+));$(0)


# Underload, score of 0 in the limit

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


Adding another :* to the sequence of :* will add two bytes to the program, and double the period of the quine. Thus, the score can be made arbitrarily small by adding more copies.

Try it online!

This basically uses the Underload universal quine constructor (a(:^)*fS):^, which is capable of producing a program whose output is any function f of the original source code. In this case, the function in question produces a string that outputs its argument in the most naive way (by escaping it and adding a print statement; escape is a, add a print statement is (S)*); and we run the function in a loop (via ^) a power of 2 times. (2 to the power of n can be written in Underload by using n repeats of :*.) The result is output like

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


which produces, when run,

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


and so on until the original program is reached.