Jonathan. Frech’s WebBlog

Cyclic Quine (#177)

Jonathan Frech,

A classic quine is a pro­gram which outputs its own source code.
At first, such a pro­gram’s existence seems weird if not im­pos­si­ble, as it has to be so self-referential that it knows about itself everything, including how to know about itself. How­ev­er, writ­ing quines is possible, even though not trivial.

A cyclic quine therefore is a pro­gram which outputs source code which differs from its own source code, yet outputs the original source code when run (the cycle length could be great­er than one). So when run­ning source codes 𝛹 and 𝛷, they output source codes 𝛷 and 𝛹.

Therefore, when one saves the first pro­gram as q0.py and the second as q1.py, one can create both source codes from one another (the following bash commands⁠¹ will not change the files’ contents).

$ python q0.py > q1.py
$ python q1.py > q0.py
$ python q0.py | python > q0.py
$ python q0.py | python | python | python | python > q1.py

[1][2020-08-05] Any shell will suffice; one nowadays pos­si­bly needs to specify python2.