<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Darshan]]></title><description><![CDATA[Darshan]]></description><link>https://blog.darshanas.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 31 May 2026 06:30:15 GMT</lastBuildDate><atom:link href="https://blog.darshanas.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[(7 of 7) Quine-spirations: To infinity and beyond]]></title><description><![CDATA[Stephen Hawking begins his book, A Brief History of Time, with this story:

A well-known scientist (some say it was Bertrand Russell) once gave a public lecture on astronomy. He described how the earth orbits around the sun and how the sun, in turn, ...]]></description><link>https://blog.darshanas.dev/quinespirations</link><guid isPermaLink="true">https://blog.darshanas.dev/quinespirations</guid><category><![CDATA[dna]]></category><category><![CDATA[self-rep]]></category><category><![CDATA[quine]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Darshan A S]]></dc:creator><pubDate>Fri, 16 Aug 2024 12:30:54 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1720258601673/8817ff94-892c-4524-85d3-841076db69c9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Stephen Hawking begins his book, <a target="_blank" href="https://www.goodreads.com/book/show/3869.A_Brief_History_of_Time"><em>A Brief History of Time</em></a>, with this story:</p>
<blockquote>
<p>A well-known scientist (some say it was Bertrand Russell) once gave a public lecture on astronomy. He described how the earth orbits around the sun and how the sun, in turn, orbits around the center of a vast collection of stars called our galaxy. At the end of the lecture, a little old lady at the back of the room got up and said: "What you have told us is rubbish. The world is really a flat plate supported on the back of a giant tortoise." The scientist gave a superior smile before replying, "What is the tortoise standing on?" "You're very clever, young man, very clever," said the old lady. "But it's turtles all the way down!"</p>
</blockquote>
<p>We are pretty sure that the Universe isn't turtles all the way down, but we certainly can create <a target="_blank" href="https://darshan.hashnode.dev/quine">Quines - Self replicating computer programs</a>, going all the way down to infinity.</p>
<h2 id="heading-zip-quine">Zip Quine</h2>
<p>Below is a zip file, that when extracted, unpacks into the same zip file, which when extracted, unpacks to the same zip file, and on and on ad infinitum. There's also an Image taking a free ride along the infinite recursion, injecting itself as an Intron.</p>
<p><a target="_blank" href="https://alf.nu/s/droste.zip">Download the ZIP quine</a>, and try extracting it for yourself!</p>
<p>How is this possible, you might ask? Well, there are several lossless compression standards, and one basic algorithm you might've come across is <a target="_blank" href="https://en.wikipedia.org/wiki/Run-length_encoding">Run-Length encoding (RLE)</a>, wherein a prefix integer tells how many times to repeat the next alphabet. Ex: The compressed data <code>2a6b1c9d</code>, gets decompressed into <code>aabbbbbbcddddddddd</code></p>
<p>Similarly, one of the sophisticated standards is L-Zip. Given an input data sequence, It spits out a compressed data sequence, and a series of commands to decompress. Running the commands as an <em>action</em> on the compressed data will decompress into the original sequence. There are only 2 possible commands:</p>
<ul>
<li><p><strong>print M</strong>: prints the next <strong>M</strong> lines of <em>input</em> as is.</p>
</li>
<li><p><strong>repeat M N</strong>: seeks back <strong>N</strong> lines from the end of the <em>output</em> and prints the next <strong>M</strong> lines of <em>output</em> as is.</p>
</li>
</ul>
<p>One can carefully design an input sequence, that is also made of the same two commands, such that it decompresses to itself.</p>
<p>Try solving the interactive <a target="_blank" href="https://wgreenberg.github.io/quine.zip/">Zip Quine puzzle</a>. It's fun!</p>
<p>Below are some <a target="_blank" href="https://darshan.hashnode.dev/quinespirations">Inspirational-Quines</a>; or more generally, programs with a self-referential nature in them.</p>
<h2 id="heading-fluid-simulation-quine">Fluid simulation Quine</h2>
<p>This is an HTML, CSS, and JavaScript Quine, that not only prints itself but also simulates fluid flow animation on its own source code!</p>
<p><a target="_blank" href="https://github.com/xem/miniFluid/">GitHub</a></p>
<p><a target="_blank" href="http://www.p01.org/fluid_simulation_quine/">Website Source</a></p>
<p>HTML / CSS / JavaScript: <a target="_blank" href="http://www.p01.org/fluid_simulation_quine/quine.html">Watch it online!</a></p>
<pre><code class="lang-JavaScript">&lt;body id=<span class="hljs-string">"b"</span> style=<span class="hljs-string">"font-size:6vw;width:29ch"</span>&gt;&lt;tt&gt;&lt;b&gt;1kb Fluid
Simulation Quine&lt;/b&gt;&lt;p&gt;Click to restart / &lt;a href="https://github.com/xem/
miniFluid/"&gt;github&lt;/a&gt;&lt;/p&gt;&lt;canvas width="512" height="192" id="c"
style="width:99%;background:#9cf"&gt;&lt;script&gt;M=o=&gt;//&lt;3
a.map(p         =&gt;{   p.t=.1   ;p.s   =0;   o||(       p.D=p.w)
;a.map(  q=&gt;(g=Math   .hypot   (e=p   .x-   q.x,  f=    p.y-q.y
)/2-1)&lt;       0?o?(   g/=p.D   ,p.t   +=(   f*(z  =3-   p.D-q.D
)+p.v-q   .v)*g,p.s   +=(e*z   +p.u   -q.   u)*g  ):    p.D+=g*
g:1)});   ;;;;;;;;;       ;G=        b.outerHTML  .    slice/*#
#*/(0,-22);Q=o=&gt;{_=a=[];for(C of G)_++,C!=" "&amp;&amp;a.push({x:_%/*.#
#*/64+4,y:5+_/64|0,C,w:"#"==C,u:0,v:0})};setInterval(`R--&gt;0/*;#
#*/&amp;&amp;Q();M(c.width^=M());for(p of a)with(p)c.getContext(/*Vida#
#*/"2d").fillText(C,7*x,8*y),w||(y+=v+=t/4,x+=u+=s/8)`,R=19)/*#
#*/;onclick=o=&gt;R=1;/*                                        ##
##                                                 ########### 
 ###################################################*/&lt;/script&gt;
</code></pre>
<p>What's more, is that the code can be formatted to fit 64 characters wide, and 16 lines tall, making it <code>64 * 16 = 1024</code> bytes. The entire source code of this fluid simulation Quine is exactly 1 KB!</p>
<p>The big empty spaces in the middle of the code spell out a certain word. Did you notice?</p>
<h2 id="heading-palindromic-quine">Palindromic Quine</h2>
<p>This is a JavaScript Quine that is also a palindrome! Try it in the console.</p>
<p>JavaScript:</p>
<pre><code class="lang-JavaScript">(q=<span class="hljs-function"><span class="hljs-params">u</span>=&gt;</span>(i=<span class="hljs-string">`(q=<span class="hljs-subst">${q}</span>,q())`</span>,i+<span class="hljs-string">' // '</span>+[...i].reverse().join<span class="hljs-string">``</span>),q()) <span class="hljs-comment">// ))(q,)``nioj.)(esrever.]i...[+' // '+i,`))(q,}q{$=q(`=i(&gt;=u=q(</span>
</code></pre>
<h2 id="heading-palindromic-polyquine">Palindromic PolyQuine</h2>
<p>This is a C# and Java Polyglot-Quine, that is also a palindrome!</p>
<p><a target="_blank" href="https://github.com/KvanTTT/Freaky-Sources?tab=readme-ov-file#palindrome-polyglot-quine">GitHub</a></p>
<p>C#: <a target="_blank" href="https://tio.run/##rVVdb6pAEH3nVwDeWD5WINJazZZrTRPfmjT1oQ8qihTtJgiGXdu0ht/unVm0ibe0TZMmcHbAmXPGs@Ma81acF8l@71qW67qTred5A8T2UOJA2XKWrdTRKxfJmlYZ7UOGEqcR5@pdka@KaL3bbBcpi1UuIgHLc84eawlvI5ad8qzhjWKMRAFK4@mbuatClQf63LL68@vGLDxpoz9rhN9p97EKtSAZBcJaAe2PRkTAKcuEyhDH0yjIkhdVhjvfJ71z4ndJB/CC9ODqkI5P/E5Jj3QLmX982mlNTSNaM0NoIlwjNBBmAHO4@xrhJV3mhcECj7KrHmW2bYpAOLLp@2STRnECfRdVFBqaZhvxU1SY0ZhNyQLApMIOzqyzA033sk3ZX2RrtZDJFpIKEulNnvE8TZyHggkkdZBoIAxm0spOJ98KZwPti2VoCJOWpav/sin6RNeJPskQJhLkXByj9vA9wncu3gCfmFQ7VQfTTgfr4J9i6PqP/atV@eDnqd5X1ioHay23LKkpDGUpMlZsHLHNnXUi@OuImswQgyJ6ip1h2/O2k4FE100EKx6cJM15lt/QKRsr1SeHLK/Kgp4DYbZajHrBX0bbl92AGUW@pPAtA1tQEwoXBCAyUcOwdd1QkjhKN0nxn558eV@r4qCIbTPau0KhSqHk1YbJnauqcJeH71FVPTluf4YAM7GbjldgghipL0kWLI5PtJTTRDo9cgGXT847pAvYI74PJeCbzI9kSJmKyAMB8pW14VfWNmb9D36Gs8Z1/8f@aZoRHvxD0so0SfWZSfjLlyfADE@GBsJ183hQZAhwevyyKXDCBVytOHbm25HOCDMWraFvWG5l048sf1ZjBicohyVdbDe7dVSs8uJO5TxK4xCSaWUn0vEtVs37ljXXawUUFDgdLNSqnarvtJVTnpM2agnx72y//wc">Try it online!</a></p>
<p>Java: <a target="_blank" href="https://tio.run/##rVVdT@pAEH3vr2jLDfZjaRuqCFkrEhPeTIw8@AAUllpwvaUl3a1GSX87d2cLJlyrxsSkPTstM@cMZ4fliTyT1tPD393OtSzXdSeF53kDwPZQ4kApGE1X6uiV8XiNq4z2PkOJEsKYeptnq5yst5tikdBIZZxwsTxn9KGW8IbQ9JhnLd4oxojnQmk8fTO3VaiyQJ9bVn9@1ZiFR230Z43wO@0@VIGWSAaBsFZA@6MhHjBMU65SwPGUBGn8ospw6/uod4r8LuoIPEM9cXVQx0d@p8QHuoXMPzxttaamIa2ZAjQBrgAaADMBc3H3NcRKvMxygwYephc9TG3b5AF3ZNN38SYhUSz6zqsoNDTNNqJHkptkTKdoIcDE3A5OrJM9Tfe8jeklsLVawGRzSSUS8XWWsiyJnfucciB1gGjADWriyk4nK7izEe3zZWhwE5elq/@yKfpE15E@SQEmEuRcHKL28D2Cdy7cAj4xqXaq9qYdD9beP8XQ9R/7V6vywc9jva@sVfbWWm5ZYpMbypKnNN84vMicdczZ6wib1OCDnDxGzrDtecVkINF1Y07zeydOMpZm13hKx0r1yT7Lq7JEzwE3Wy2KveCS4vZ5N6BGni2x@JaBzbEpChdIADFBw7B13VDiiCSbOP9PT768q1VxQMS2Ke5dgFClULJqw@TOVVWwy8P3qKqeHLY/BRAzsZ2OV8IEPlJf4jRYHJ5wKacJdXroTFw@Ou2grsAe8n1RInyT@USGmKqALOBCvrI2/Mraxqz/wc9w1rjq/9g/TTPCvX9AWpkmqT4zCX758gSYwcnQALhqHg6KFECcHr9sijjhAqZWHFvz7UBnhCkla9G3WG5k0w80e1YjKk5QJpZkUWy2a5KvsvxWZYwkUSiScWUn0LECquZ9y5rrtQIKCBwPFmjVTtV32soxz1EbtYTwd7bb/QM">Try it online!</a></p>
<pre><code class="lang-Java"><span class="hljs-comment">/**///\u000A\u002F\u002A
using System;//\u002A\u002F
class Program{public static void//\u000A\u002F\u002A
Main//\u002A\u002Fmain
(String[]z){String s="`**?`@#_^using System;?_#^class Program{public static void?@#_^Main?_#main^(String[]z){String s=!$!,t=s;int i;int[]a=new int[]{33,94,38,64,35,95,96,63,36};String[]b=new String[]{!&amp;!!,!&amp;n!,!&amp;&amp;!,!&amp;@!,!&amp;#!,!&amp;_!,!`!,!?!,s};for(i=0;i&lt;9;i++)t=t.?@#_^Replace?_#replace^(!!+(char)a[i],b[i]);t+='*';for(i=872;i&gt;=0;i--)t=t+t?@#_^[i];Console.Write?_#.charAt(i);System.out.printf^(t);}}/",t=s;int i;int[]a=new int[]{33,94,38,64,35,95,96,63,36};String[]b=new String[]{"\"","\n","\\","\\u000A","\\u002F","\\u002A","/","//",s};for(i=0;i&lt;9;i++)t=t.//\u000A\u002F\u002A
Replace//\u002A\u002Freplace
(""+(char)a[i],b[i]);t+='*';for(i=872;i&gt;=0;i--)t=t+t//\u000A\u002F\u002A
[i];Console.Write//\u002A\u002F.charAt(i);System.out.printf
(t);}}/*/</span>}};)t(
ftnirp.tuo.metsyS;)i(tArahc.F200u\A200u\<span class="hljs-comment">//etirW.elosnoC;]i[</span>
A200u\F200u\A000u\<span class="hljs-comment">//t+t=t)--i;0=&gt;i;278=i(rof;'*'=+t;)]i[b,]i[a)rahc(+""(</span>
ecalperF200u\A200u\<span class="hljs-comment">//ecalpeR</span>
A200u\F200u\A000u\<span class="hljs-comment">//.t=t)++i;9&lt;i;0=i(rof;}s,"//","/","A200u\\","F200u\\","A000u\\","\\","n\",""\"{][gnirtS wen=b][gnirtS;}63,36,69,59,53,46,83,49,33{][tni wen=a][tni;i tni;s=t,"/}};)t(^ftnirp.tuo.metsyS;)i(tArahc.#_?etirW.elosnoC;]i[^_#@?t+t=t)--i;0=&gt;i;278=i(rof;'*'=+t;)]i[b,]i[a)rahc(+!!(^ecalper#_?ecalpeR^_#@?.t=t)++i;9&lt;i;0=i(rof;}s,!?!,!`!,!_&amp;!,!#&amp;!,!@&amp;!,!&amp;&amp;!,!n&amp;!,!!&amp;!{][gnirtS wen=b][gnirtS;}63,36,69,59,53,46,83,49,33{][tni wen=a][tni;i tni;s=t,!$!=s gnirtS{)z][gnirtS(^niam#_?niaM^_#@?diov citats cilbup{margorP ssalc^#_?;metsyS gnisu^_#@`?**`"=s gnirtS{)z][gnirtS(</span>
niamF200u\A200u\<span class="hljs-comment">//niaM</span>
A200u\F200u\A000u\<span class="hljs-comment">//diov citats cilbup{margorP ssalc</span>
F200u\A200u\<span class="hljs-comment">//;metsyS gnisu</span>
A200u\F200u\A000u\<span class="hljs-comment">///**/</span>
</code></pre>
<h2 id="heading-upside-down-quine">Upside-down Quine</h2>
<p>Here's a JavaScript Quine, that not only prints itself backwards character by character but also flips each character upside-down!</p>
<p>JavaScript: (Try it in console)</p>
<pre><code class="lang-JavaScript">(o=(Z=<span class="hljs-string">"()[]&lt;&gt;⌄^Ɩ1nueǝrɹvʌmɯaɐjɾiᴉfɟpd\"⹂˙."</span>)=&gt;[...<span class="hljs-string">"(o="</span>+o+<span class="hljs-string">")()"</span>].reverse().map(<span class="hljs-function"><span class="hljs-params">s</span>=&gt;</span>Z[Z.indexOf(s)^<span class="hljs-number">1</span>]||s).join(<span class="hljs-string">""</span>))()
</code></pre>
<p>prints</p>
<pre><code class="lang-JavaScript">()((⹂⹂)uᴉoɾ˙(s||[Ɩ⌄(s)ɟOxǝpuᴉ˙Z]Z&lt;=s)dɐɯ˙()ǝsɹǝʌǝɹ˙[⹂()(⹂+o+⹂=o)⹂˙˙˙]&lt;=(⹂˙.<span class="hljs-string">"⹂\\pdfɟiᴉjɾaɐmɯvʌrɹeǝnuƖ1⌄^&lt;&gt;[]()⹂=Z)=o)</span>
</code></pre>
<h2 id="heading-snake-game-quine">Snake game Quine</h2>
<p>A quine that plays snake over its own source code.</p>
<p><a target="_blank" href="https://github.com/taylorconor/quinesnake">GitHub</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1723756728511/2fe2d61a-bdfe-4895-a588-ea8fd21fca90.gif" alt class="image--center mx-auto" /></p>
<p>Too much food for one snake? Share it with others from the Multiplayer variant of the Snake-Quine game below. Each red dot is food, and the other 3 colors are players!</p>
<p><a target="_blank" href="https://github.com/hanachin/quine-snake">GitHub</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1723756778717/9d8a22de-9d00-4fed-bccb-b5ec1e3c58cf.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-c-to-html-quine">C to HTML Quine</h2>
<p>This is a C program, that prints the source code of an HTML/JS website, that displays the original C program:</p>
<p><a target="_blank" href="http://michaelwehar.com/quines/c_prints_javascript.html">C to HTML Quine</a></p>
<h2 id="heading-rubick-cube-quine">Rubick cube Quine:</h2>
<p>This is a JavaScript Quine with a fully functional Rubik Cube at the center!</p>
<p>It does have colors! ANSI escape codes are used to display the colour when output into the terminal.</p>
<pre><code class="lang-Javascript"><span class="hljs-built_in">eval</span>((c=<span class="hljs-string">`[b,d,p]=($=String.fromCh              arCode)(96,92,32);M=process.argv[S
='slice'](2).join(W='').split                       (/(\\ww?\\d?'?)/).filter(d=&gt;d
.trim());P=[6,3,0,7,4,1,8,                             5,2];E=/./.constructor;C=$
(85,70,82,66,76,68);s=c.                                 replace(E('[^'+C+']','g'
),W);I=C+'xyz';u=_=&gt;{s                                     =P.map(d=&gt;s[d]).join(W
)+s[S](12,21)+s[S](9,                                       12)+s[S](21)};y=_=&gt;{s
=[P.map(d=&gt;s[d]),[0,                                         12,24].flatMap(_=&gt;[[
q=12+_,21+_],[9+_,q                   UUU                     ]]).map(d=&gt;s[S](...
d)),[...P].reverse                    UUU                      ().map(d=&gt;s[d+45])
].flat().join(W)};                    UUU                      x=_=&gt;{s=[...'ÀÁÂÌÍ
ÎØÙÚ¿Ë×áâãÛÏÃ¼»º¾Ê                 LLLFFFRRRBBB                ÖäåæÜÐÄ¹¸·½ÉÕçèéÝÑ
Å¶µ´àßÞÔÓÒÈÇÆ'].ma                 LLLFFFRRRBBB                p(d=&gt;s[d.charCodeA
t()-180]).join(W)}                 LLLFFFRRRBBB                ;z=[x,y,x,x,x];X=[
x,x,x];m=[[u],[x,u                    DDD                      ,X],[y,y,y,X,u,x,y
],[X,u,x],[z,u,z,z                    DDD                      ,z],[x,x,u,x,x],[x
],[y],z];M.map(d=&gt;                    DDD                      {A=_=&gt;m[I.indexOf(
d[0])].flat().map(d                                           =&gt;d());A();d[1]=="'
"?(A(),A()):d[1]&amp;&amp;A(                                         )});i=0;console.log(
'eval((c='+b+c.replac                                       e(/\\x1b\\[\\d+m/g,''
).replace(E('['+C+']',                                     'g'),_=&gt;'\\x1b['+[47,4
2,41,44,45,43][I.indexOf                                 (s[i])]+'m'+s[i++]+'\\x1
b[0m').replaceAll(d,d+d)+b                             +').replace('+p.repeat(16)
+'/'+d+'x1b'+d+'['+d+'d+m|'+d                       +'s+|['+C+']*/g,""))');/3x3x3
*simulator*by*kirjava/`</span>).replace(                <span class="hljs-regexp">/\x1b\[\d+m|\s+|[UFRBLD]*/g</span>,<span class="hljs-string">""</span>))
</code></pre>
<p>When executed normally, it functions as a regular Quine, and prints itself. When executed with a moves as arguments, it produces a new Quine with the moves applied! <code>node qube.js "RUR'URU2R'"</code></p>
<p>Since this is a Quine, you can continuously pipe the program into itself and apply moves on the way. <code>node qube.js "R U" | node - "R' U'" | node - "R' F'"</code></p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.youtube.com/watch?v=OPM49mhgmk0">https://www.youtube.com/watch?v=OPM49mhgmk0</a></div>
<p> </p>
<h2 id="heading-hash-quine">Hash Quine</h2>
<p>This is a PNG Image, which contains it's own MD5 Hash value in the image. Hash Value: <code>1337e2ef42b9bee8de06a4d223a51337</code></p>
<p><a target="_blank" href="https://emn178.github.io/online-tools/md5_checksum.html">Try MD5 Hashing Online</a> and check if it spits out the same value.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1723756930419/49a3e156-14f2-4faa-9838-d7a991ef8866.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-globe-quine">Globe Quine</h2>
<p>A Ruby Quine with an ASCII image of a globe in its source code that rotates by 45 degrees after each run, eventually coming around after eight runs.</p>
<p><a target="_blank" href="https://github.com/knoxknox/qlobe?tab=readme-ov-file">GitHub</a></p>
<pre><code class="lang-Ruby">v=<span class="hljs-number">0000</span>;eval$s=%q~d=%!^Lcf&lt;LK8,                  <span class="hljs-number">_</span>@7gj*LJ=c5nM)Tp1g<span class="hljs-number">0</span>%Xv.,S[&lt;&gt;YoP
<span class="hljs-number">4</span>ZojjV)O&gt;qIH1/n[<span class="hljs-params">|2yE[&gt;:ieC       "%.#%  :::##"       97N-A&amp;Kj_K_&gt;&lt;wS5rtWk@*a+Y5
yH?b[F^e7C/56j|</span>pmRe+<span class="hljs-symbol">:</span>)B     <span class="hljs-string">"##%      ::##########"</span>     O98(Zh)<span class="hljs-string">'Iof*nm.,$C5Nyt=
PPu01Avw^&lt;IiQ=5$'</span>D-y?    <span class="hljs-string">"##:         ###############"</span>    g6<span class="hljs-string">`YT+qLw9k^ch|K'),tc
6ygIL8xI#LNz3v}T=4W    "#            #.   .####:#######"    lL27FZ0ij)7TQCI)P7u
}RT5-iJbbG5P-DHB&lt;.   "              ##### # :############"   R,YvZ_rnv6ky-G+4U'
$*are@b4U351Q-ug5   "              #######################"   00x8RR%`</span>Om7VDp4M5
PFixrPvl&amp;&lt;p[]<span class="hljs-number">1</span>IJ   <span class="hljs-string">"              ############:####  %#####"</span>   EGgDt8Lm<span class="hljs-comment">#;bc4zS^</span>
y]<span class="hljs-number">0</span><span class="hljs-string">`_PstfUxOC(q   "              .#############:##%   .##  ."   /,}.YOIFj(k&amp;q_V
zcaAi?]^lCVYp!;  " %%            .################.     #.   "  ;s="v=%04o;ev"%
(;v=(v-($*+[45,  ":####:          :##############%       :   "  ])[n=0].to_i;)%
360)+"al$s=%q<span class="hljs-subst">#{  <span class="hljs-string">"%######.              #########            "</span>  ;;<span class="hljs-string">"%c"</span>%<span class="hljs-number">126</span>+$s&lt;&lt;
<span class="hljs-number">126</span>}</span>";d.gsub!(/  "##########.           #######%             "  |\s|".*"/,"");;
require"zlib"||  "###########           :######.             "  ;d=d.unpack"C*"
d.map{|c|n=(n||  ":#########:           .######: .           "  )*90+(c-2)%91};
e=["%x"%n].pack   " :#######%           :###### #:          "   &amp;&amp;"H*";e=Zlib::
Inflate.inflate(   "  ######%           .####% ::          "   &amp;&amp;e).unpack("b*"
)[0];22.times{|y|   "  ####%             %###             "   ;w=(Math.sqrt(1-(
(y*2.0-21)/22)**(;   " .###:             .#%             "   ;2))*23).floor;(w*
2-1).times{|x|u=(e+    " %##                           "    )[y*z=360,z]*2;u=u[
90*x/w+v+90,90/w];s[(    " #.                        "    ;y*80)+120-w+x]=(""&lt;&lt;
32&lt;&lt;".:%#")[4*u.count((     " .                   "     ;"0"))/u.size]}};;puts\
s+";_ The Qlobe<span class="hljs-subst">#{<span class="hljs-string">" "</span>*<span class="hljs-number">18</span>+ (       <span class="hljs-string">"#  :#######"</span>       ;<span class="hljs-string">"Copyright(C).Yusuke End\
oh, 2010"</span>)}</span>";exit~;_ The Qlobe                  Copyright(C).Yusuke Endoh, 2010</span>
</code></pre>
<h2 id="heading-quine-tweet">Quine Tweet</h2>
<p><a target="_blank" href="https://twitter.com/quinetweet/status/1309951041321013248">Quine Tweet</a> - A tweet that tweets its own link. This was done way before, the edit feature was introduced.</p>
<p>Here's a write-up about how it was done: <a target="_blank" href="https://oisinmoran.com/quinetweet">Write up</a></p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://twitter.com/quinetweet/status/1309951041321013248">https://twitter.com/quinetweet/status/1309951041321013248</a></div>
<p> </p>
<h2 id="heading-quine-blog-series">Quine blog series</h2>
<p>Last but not least, is the existence of this very own <a target="_blank" href="https://darshan.hashnode.dev/series/quines">blog series</a>! Each blog in this 7-part series has a self-referential easter egg relating to the topic in question. How many did you find?</p>
<p><a target="_blank" href="https://darshan.hashnode.dev/series/quines"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1723757044575/1fe9c4bc-4d0a-43ea-8bc8-a49d5c3dafea.jpeg" alt class="image--center mx-auto" /></a></p>
<hr />
<p>Although we've explored self-reference in Computing here, you can find self-reference embedded in Art, Literature, Math, Biology and much more. In fact, there is a case to be made that, self reference might be a crucial ingredient for intelligence and consciousness.</p>
<p>That is to say, a Quine, in its essence, embodies a fundamental truth: the capacity for self-reflection and self-replication is not just a quirk of computer science, but a cornerstone of existence itself. From the spiralling double helix of DNA to the introspective musings of the human mind, the universe seems to whisper a persistent message: "<strong>Know thyself!</strong>"</p>
<p>It beckons us to turn our gaze inward, to explore the depths of our own being, to understand the code that shapes our thoughts, actions, and destinies.</p>
<blockquote>
<p>"Until you make the unconscious conscious, it will direct your life and you will call it fate."</p>
<p>- Carl Jung</p>
</blockquote>
<hr />
<p>Sources and references:</p>
<ul>
<li><p><a target="_blank" href="https://github.com/kirjavascript/quine-howto">How to write a lot of Quines</a>, GitHub repo.</p>
</li>
<li><p><a target="_blank" href="http://www.p01.org/fluid_simulation_quine/">Fluid Simulation Quine</a>, website.</p>
</li>
<li><p><a target="_blank" href="https://wgreenberg.github.io/quine.zip/">LZ77 Write your own Zip Quine</a>, online puzzle by Will Greenberg.</p>
</li>
<li><p><a target="_blank" href="https://research.swtch.com/zip">Zip Files All The Way Down</a>, blog by Russ Cox.</p>
</li>
<li><p><a target="_blank" href="https://www.goodreads.com/book/show/3869.A_Brief_History_of_Time">A Brief History of Time</a>, book by Stephen Hawking.</p>
</li>
<li><p><a target="_blank" href="https://darshan.hashnode.dev/quinespirations">Quine-spirations. To infinity and beyond</a>, self-referencing blog with inspiring Quines.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[(6 of 7) PolyQuine: The rosetta stone of computing]]></title><description><![CDATA[Welcome back to our Quine saga! We've delved into self-replication, hidden messages, and even self-healing code. Now, prepare to be dazzled by another mind-bending concept: PolyglotQuine, or PolyQuine for short.
Before PolyglotQuine, let's understand...]]></description><link>https://blog.darshanas.dev/polyquine</link><guid isPermaLink="true">https://blog.darshanas.dev/polyquine</guid><category><![CDATA[dna]]></category><category><![CDATA[quine]]></category><category><![CDATA[self-rep]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Darshan A S]]></dc:creator><pubDate>Sat, 10 Aug 2024 20:08:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1720258421774/f39ec18a-034c-408d-ac29-1e1ffe2d48c8.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Welcome back to our <a target="_blank" href="https://darshan.hashnode.dev/quine">Quine</a> saga! We've delved into self-replication, hidden messages, and even self-healing code. Now, prepare to be dazzled by another mind-bending concept: <strong>PolyglotQuine</strong>, or <a target="_blank" href="https://darshan.hashnode.dev/polyquine"><strong>PolyQuine</strong></a> for short.</p>
<p>Before <strong>PolyglotQuine</strong>, let's understand what a <strong>Polyglot</strong> itself is. In the human world, a polyglot is someone who speaks, reads, and/or writes in multiple languages. Multi-lingual in other words.</p>
<h2 id="heading-the-rosetta-stone">The Rosetta stone</h2>
<p>Speaking of multi-lingual, on 15 July 1799, French soldiers under Napoleon, were strengthening the defenses of Fort Julien, a couple of miles north-east of the Egyptian port city of Rosetta (modern-day Rashid). One of the soldiers spotted a slab with inscriptions on one side. He saw at once that it might be important and informed his superiors. A month later, Napoleon himself inspected what had already begun to be called <em>la Pierre de Rosette</em>, the <a target="_blank" href="https://en.wikipedia.org/wiki/Rosetta_Stone">Rosetta Stone</a>.</p>
<p>The stone has three distinctive sections. It was found that the top text is ancient Egyptian <a target="_blank" href="https://en.wikipedia.org/wiki/Egyptian_hieroglyphs">hieroglyphic</a>, the middle text to be Egyptian <a target="_blank" href="https://en.wikipedia.org/wiki/Demotic_(Egyptian)">Demotic</a> script, while the bottom is in <a target="_blank" href="https://en.wikipedia.org/wiki/Ancient_Greek">Ancient Greek</a> all saying the same thing! The decree has only minor differences across the three versions, making the Rosetta Stone key to deciphering the long-lost Egyptian scripts.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1723319981444/93627301-e6b1-4722-8336-76e0e45c1d2c.jpeg" alt class="image--center mx-auto" /></p>
<p>The stone currently, is on display at <a target="_blank" href="https://artsandculture.google.com/story/the-rosetta-stone-the-british-museum/OQVRt6m8dEG4rw?hl=en">The British Museum</a> after the British stole the stone. Also, check out the <a target="_blank" href="https://sketchfab.com/3d-models/the-rosetta-stone-1e03509704a3490e99a173e53b93e282">3D Model of the Stone</a> online.</p>
<p>In the coding world, a polyglot is a digital equivalent of the Rosetta Stone, a program that speaks not one, but multiple programming languages fluently.</p>
<p>Essentially, a Polyglot of order n is a program such that:</p>
<ul>
<li><p>The same source code is a valid syntax in multiple languages.</p>
</li>
<li><p>Executing the same script in any of those languages yields exactly the same outputs.</p>
</li>
</ul>
<blockquote>
<p>Polyglots are a hack whereas Quines, and everything up until now in this blog series, were a general phenomenon of the fixed-point theorem.</p>
<p>Los políglotas son un truco, mientras que los Quines, y todo lo discutido hasta ahora en esta serie de blogs, son un fenómeno general del teorema del punto fijo.</p>
<p>पॉलीग्लॉट्स एक तरकीब हैं, जबकि क्वाइन और इस ब्लॉग श्रृंखला में अब तक की गई सभी बातें, स्थिर-बिंदु प्रमेय की एक सामान्य घटना हैं।</p>
</blockquote>
<h2 id="heading-trivial-polyglot">Trivial polyglot</h2>
<p>The below syntax for printing is so common that this is a valid code in Python, Ruby, and Perl! The same file can be fed to any of the three compilers/interpreters, and each would execute as a valid program.</p>
<pre><code class="lang-Python">print(<span class="hljs-string">"Hello World!"</span>)
</code></pre>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/SQL_Injection">SQL Injection</a> is also a trivial exploit of the polyglot phenomenon, where a server naively expects user-controlled input to conform to a certain constraint, but the user supplies syntax which is interpreted as SQL code.</p>
<h2 id="heading-2nd-order-polyglot">2nd-order polyglot</h2>
<p>This is a 2nd order polyglot in C and Bash, that prints <code>Hello, world!</code></p>
<p><a target="_blank" href="https://www.nyx.net/~gthompso/poly/applin.txt">Website source</a></p>
<p>Highlighted in C: <a target="_blank" href="https://tio.run/##XY5NDoIwEIX3nOLJQn5SbNAFCyJrD@AFaluhsRRSQEyMXB2LsHIWk8w3770ZnpSczzNnPc7pCTSmKKiQT2oGrXEs9mkOyasG/kVq3RCMjdVi5zv6Ur0XezFwrVSH1jalZfWyf3QYjJAWnBPcs4yAGQF6U4Z21WHx0Bw1UyaM8Pbwq9GqXoae@yOZEtdXTGMShE6@TuTvh40upihHO/S8YjackmmNcOzjEjZVFETbKWmEi5znLw">Try it online!</a></p>
<pre><code class="lang-C">cat =<span class="hljs-number">13</span> <span class="hljs-comment">/*/ &gt;/dev/null 2&gt;&amp;1; echo "Hello, world!"; exit
*
*  This program works under cc, f77, and /bin/sh.
*
*/</span>; main() {
      write(
cat-~-cat
     <span class="hljs-comment">/*,'(
*/</span>
     ,<span class="hljs-string">"Hello, world!"</span>
     ,
cat); <span class="hljs-built_in">putchar</span>(~-~-~-cat); } <span class="hljs-comment">/*
     ,)')
      end
*/</span>
</code></pre>
<p>Highlighted in Bash: <a target="_blank" href="https://tio.run/##XY5BDoIwEEX3nOLLQoEUG3XBgsjaA3iB0lbbWAopICZGro5FWDmLSebN/3@mZK2aJs46nA8n0ISioEI@qe2NwbHYHnJIrmqEF2lMTTDUzohN6OlLd0ESJMBV6RaNq@@OVfP@0aK3QjpwTnDLMgJmBWipLW3VfvbQHBXTNorxDvCrwelORoH/Ix1T3xdME7KLvHyZyN8PK51NcY6m77hiLhrTcYnw7OMTVlW8i9dT0gofOU1f">Try it online!</a></p>
<pre><code class="lang-bash">cat =13 /*/ &gt;/dev/null 2&gt;&amp;1; <span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello, world!"</span>; <span class="hljs-built_in">exit</span>
*
*  This program works under cc, f77, and /bin/sh.
*
*/; <span class="hljs-function"><span class="hljs-title">main</span></span>() {
      write(
cat-~-cat
     /*,<span class="hljs-string">'(
*/
     ,"Hello, world!"
     ,
cat); putchar(~-~-~-cat); } /*
     ,)'</span>)
      end
*/
</code></pre>
<h2 id="heading-3rd-order-polyglot">3rd-order polyglot</h2>
<p>Two commonly used techniques for constructing a polyglot program are to make use of languages that use different characters for comments and to redefine various tokens as others in different languages. These are demonstrated in this public domain polyglot written in ANSI C, PHP, and bash, where all three of them print <code>Hello, world!</code></p>
<p>Bash: <a target="_blank" href="https://tio.run/##bVFLTsMwFNz7FEPaRRIBRkis3M@WQ3STxi/YkrEj1w6RgLtwFi4WTD9KUnXleTPz5tnP@@qghgG8XKy2rWoZqFYO2SsZ4@7x4byRd5ngHM8bcEkdt9EYTPEOguHKINCvq0Qv@/WLuBZPHbpBni/7orjRHBRZBk8heounW/GNZlhIarQl0O/PdjOWe5Q8VdrWJkrC6hCkdo9q4nivtM0LaBtOsHNaFqPc@qQ05yMf@eAjYeJroq2DdummF3ROZvhkl5T5Jnc2@0/h5XwhX3jz1OKhO84ouZg9/nscWaePYscpiSv5MPwB">Try it online!</a></p>
<pre><code class="lang-Bash"> /*<span class="hljs-comment">#&lt;?php</span>
 <span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello, world!"</span>;// 2&gt; /dev/null &gt; /dev/null \ ;
 // 2&gt; /dev/null; x=a;
 <span class="hljs-variable">$x</span>=5; // 2&gt; /dev/null \ ;
 <span class="hljs-keyword">if</span> ((<span class="hljs-variable">$x</span>))
 // 2&gt; /dev/null; <span class="hljs-keyword">then</span>
 <span class="hljs-built_in">return</span> 0;
 // 2&gt; /dev/null; <span class="hljs-keyword">fi</span>
 <span class="hljs-comment">#define e ?&gt;</span>
 <span class="hljs-comment">#define b */</span>
 <span class="hljs-comment">#include &lt;stdio.h&gt;</span>
 <span class="hljs-comment">#define main() int main(void)</span>
 <span class="hljs-comment">#define printf printf(</span>
 <span class="hljs-comment">#define true )</span>
 <span class="hljs-comment">#define function</span>
 <span class="hljs-keyword">function</span> <span class="hljs-function"><span class="hljs-title">main</span></span>()
 {
 <span class="hljs-built_in">printf</span> <span class="hljs-string">"Hello, world!\n"</span><span class="hljs-literal">true</span>/* 2&gt; /dev/null | grep -v <span class="hljs-literal">true</span>*/;
 <span class="hljs-built_in">return</span> 0;
 }
 <span class="hljs-comment">#define c /*</span>
 main
 <span class="hljs-comment">#*/</span>
</code></pre>
<p>PHP: <a target="_blank" href="https://tio.run/##bVFLTsMwFNz7FEPaRWIBRkis3M@WQ3RT4hdiydiWsUMk4C6chYsF04@SVF153sy8efazb/0wQPDFautbz0B161A8kzHuFh8uGHVTSCHwuIFQ1AmbjMEU7yAZLgwS/Xqf6WW/fpKX4rFDNyjLZV9VV5pjS5YhUEzB4uFafKMZFooabQn0@7PdjOULuMiVtrVJirB6j0q7@3bieNtrW1bQNh5h57SqRtmHrDSnoxz5GBJh4muSraN2@aZndEpm@GTnlPkmd7b4TxF8vpAvvAbyuOsOM7iQs8d/jyPr/FHsMCVzXAzDHw">Try it online!</a></p>
<pre><code class="lang-PHP"> <span class="hljs-comment">/*#&lt;?php
 echo "Hello, world!";// 2&gt; /dev/null &gt; /dev/null \ ;
 // 2&gt; /dev/null; x=a;
 $x=5; // 2&gt; /dev/null \ ;
 if (($x))
 // 2&gt; /dev/null; then
 return 0;
 // 2&gt; /dev/null; fi
 #define e ?&gt;
 #define b */</span>
 <span class="hljs-comment">#include &lt;stdio.h&gt;</span>
 <span class="hljs-comment">#define main() int main(void)</span>
 <span class="hljs-comment">#define printf printf(</span>
 <span class="hljs-comment">#define true )</span>
 <span class="hljs-comment">#define function</span>
 <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">main</span>(<span class="hljs-params"></span>)
 </span>{
 printf <span class="hljs-string">"Hello, world!\n"</span><span class="hljs-literal">true</span><span class="hljs-comment">/* 2&gt; /dev/null | grep -v true*/</span>;
 <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
 }
 <span class="hljs-comment">#define c /*</span>
 main
 <span class="hljs-comment">#*/</span>
</code></pre>
<p>C: <a target="_blank" href="https://tio.run/##bVFLbsMgFNxziqmThY2SUlXqyvlse4hsUniOkShYFFxLae/Ss/RiLs1HtqOsmDczbx485PIgZd9D8Nlq29QNA8naIXslY9wCn84b9ZCVQuB5A6GoFTYagzHeoWS4MZTo1vtEz7v1S3krnjt0hTyfd0VxpznUZBk8hegtnu7FV5phpqjSlkC/P9vNUL6Bi1RpK01UhNVHUNo91iPH@17bvIC24Qxbp1UxyI1PSnU58oEPPhJGvipaGbRLN72iSzLDkV1Tppvc2ew/RfDpQr5w8NRg2Z5mcFFOHv89jJTpo9hpSuK46Ps/">Try it online!</a></p>
<pre><code class="lang-C"> <span class="hljs-comment">/*#&lt;?php
 echo "Hello, world!";// 2&gt; /dev/null &gt; /dev/null \ ;
 // 2&gt; /dev/null; x=a;
 $x=5; // 2&gt; /dev/null \ ;
 if (($x))
 // 2&gt; /dev/null; then
 return 0;
 // 2&gt; /dev/null; fi
 #define e ?&gt;
 #define b */</span>
 <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
 <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> main() int main(void)</span>
 <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> printf printf(</span>
 <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> true )</span>
 <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> function</span>
 <span class="hljs-function">function <span class="hljs-title">main</span><span class="hljs-params">()</span>
 </span>{
 <span class="hljs-built_in">printf</span> <span class="hljs-string">"Hello, world!\n"</span><span class="hljs-literal">true</span><span class="hljs-comment">/* 2&gt; /dev/null | grep -v true*/</span>;
 <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
 }
 <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> c <span class="hljs-comment">/*
 main
 #*/</span></span>
</code></pre>
<h2 id="heading-6th-order-polyglot">6th-order polyglot</h2>
<p>Polyglot in 6 languages: written in Perl, C, Unix shell, <a target="_blank" href="https://en.wikipedia.org/wiki/Brainfuck">Brainfuck</a>, <a target="_blank" href="https://en.wikipedia.org/wiki/Whitespace_(programming_language)">Whitespace</a> and <a target="_blank" href="https://en.wikipedia.org/wiki/Befunge">Befunge</a>, where they all print <code>hitforum</code>.</p>
<p><a target="_blank" href="https://www.retas.de/thomas/computer/programs/useless/misc/polyglot/c.html">Website source</a></p>
<ul>
<li><p>Perl5: <a target="_blank" href="https://tio.run/##pY/JDoIwFEX3/YqbQuKA1pUbaRoWuvUH0DSoJTSRIQiExPDtiMWhiUtPu7j3TX0tVHld972Di4p1ptCixmoOQwPiYPOLGHFdN6JpXeZxpRPqhGLjLGRwBFFNdMVEnZMcNNFVnJd1Sn3V6mri3@oTTOaOotRZBRrIQ0a78UUQKXf7rZTC@2DJ0NJ2fOjzOF8exdLAPPZKMm7sGyZGBxvyLGKcM8xXIGmks@nsvV2M6ecHw5YzH923z1x70L@BlyPD6fsH">Try it online!</a></p>
</li>
<li><p>C: <a target="_blank" href="https://tio.run/##pY/JDoIwFEX3/YqbQiKDlL00jQvd@gNqGsQiTQQMgjExfjticWji0tMu7n1TX7PokGV972Cvcl0pXNEhDmC4gDiY/SJGXNdNadk1dd7qgjprMXOmcr4FUZf0iInKihq00G1eN11JE3XV7SQ5dzuYzA2nRlct6FxuKnofXwSRcrlaSCnCD5ZcW9qOD30h59FWRAYWsleScWPfMDE62JBnEeOcIYhBylRXnv/eLof3@cGwpZ/g/u0z1x70b@DlyHD6/gE">Try it online!</a></p>
</li>
<li><p>Bash: <a target="_blank" href="https://tio.run/##pY9LDoIwFEXnXcVNIUFA61yahoFO3YCSBrWEJgKGjzExrh2xKDZx6GkH975fXw9pk/e9g5PKdKlwQ4dlAMMVxMHqFzHium5Ki66uslbn1NmJlTOXcQKirukZnjrmFWiu26yqu4JG6qZbL2q6A0zmjkutyxY0lvuSPsYXQaTcbNdSinDCkjtL2/GhL@R8kYiFgYXsnWTc2A9MjA425FXEOGcIliBFqsuZ/9kuw2z6wbClH@Hx7TPXHvRv4O3IcPr@CQ">Try it online!</a></p>
</li>
<li><p>Brainfuck: <a target="_blank" href="https://tio.run/##pZBLCsIwFEXnWcUlLfipxrkNoQOdugGVUDWhQdtKbUQQ115rqjXg0JMM3n3/ZFelptB2f2yaAAelTaFwg8VsDMcVJMD8F9ERhmFKc1uVujYZDdZiHkxksgVR1/SEgdpnJWhmal1WNqexupl6EF/sDi5yx7kyRQ2ayE1BH91EECmXq4WUIurxzLVn@/62LuJ8uhVTB4vYO8i4kx@Y6BR8yCuJcc4wnoHk7ZcMR5/tNIb9C9otRzEe3zp3/Ub/Ot6KtKdpng">Try it online!</a></p>
</li>
<li><p>Whitespace: (Not working for now)</p>
</li>
<li><p>Befunge: <a target="_blank" href="https://tio.run/##pY/PjsIgEMbvPMUX2sQ/3eLVtYR40KsvoIZUhS3JlppuMW6Mz167VF0Sj/6Gw/fNMMywU9rZL5V@TtPjr5dtG@GgtLEKZzhMxvCcQCLMXhE9cRzntHR1pRtT0GgtZtGHnG9B1Cn/xkDtiwq0MI2ualfSTJ1NM8h@3A6@csGxNrYBncuNpdd@IoiUy9VCSpE8CeQ60GG@60s4T7ci9bCE3YuMe/uAid4hhPxdYpwzjCcgZW7scPTYTmP4/EG35SjD9b/Pn/ChdxN3R7po2xs">Try it online!</a></p>
</li>
</ul>
<pre><code class="lang-c"><span class="hljs-meta"># <span class="hljs-meta-keyword">define</span> x u <span class="hljs-comment">/*      v 
# :::::::::::::::::::&gt;&gt;&gt;&gt;&gt;&gt;&gt;$$$a"muroftih"#[&gt;:#,_@] 
eval 'echo "hitforum";exit';sub echo { print "@_\n"}       
__END__&gt;++++++++++&gt;++++++++++[&gt;+++++++++++&gt;++++++++++  
+&lt;&lt;-]&gt;------.+.&gt;++++++.&lt;---.+++++++++.&gt;--.+++             
.&lt;--.&lt;&lt;. */</span> </span>
main() { <span class="hljs-built_in">printf</span> (<span class="hljs-string">"hitforum\n"</span>); }
</code></pre>
<h2 id="heading-7th-order-polyglot">7th-order polyglot</h2>
<p>A polyglot written in 7 esoteric programming languages: <a target="_blank" href="http://esolangs.org/wiki/Malbolge">Malbolge</a>, <a target="_blank" href="http://esolangs.org/wiki/Brainfuck">Brainfuck</a>, <a target="_blank" href="http://esolangs.org/wiki/Whitespace">Whitespace</a>, <a target="_blank" href="http://esolangs.org/wiki/Spoon">Spoon</a>, <a target="_blank" href="http://esolangs.org/wiki/Asdf">asdf</a>, <a target="_blank" href="http://esolangs.org/wiki/123">123</a> and <a target="_blank" href="http://esolangs.org/wiki/EXCON">EXCON</a>.</p>
<p><a target="_blank" href="https://github.com/kenrube/Esopoly?tab=readme-ov-file">GitHub</a></p>
<p>The code has an ASCII art that spells out <em>X-P ESOTERIC POLYGLOT</em>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1723320220823/37c8d9c9-5183-4d81-8463-15103274bf8d.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-450th-order-polyglot">450th-order polyglot</h2>
<p>There is an <a target="_blank" href="https://codegolf.stackexchange.com/questions/102370/add-a-language-to-a-polyglot/266144?answertab=modifieddesc#tab-top">ongoing thread</a> in CodeGolf to chain as many languages as possible while still keeping the entire program a Polyglot. Mind-blowingly enough, the count has now reached to 450+</p>
<p><a target="_blank" href="https://codegolf.stackexchange.com/questions/102370/add-a-language-to-a-polyglot/266144?answertab=modifieddesc#tab-top">450+ Polyglot</a></p>
<h2 id="heading-pdf-mp3-polyglot">PDF + MP3 polyglot</h2>
<p>Here's a file that is a Polyglot of two completely different file types. Saving the file as <em>file.pdf</em> opens as a valid PDF document, and saving the same file as <em>file.mp3</em> plays a valid audio. Download and try renaming the file yourself!</p>
<p><a target="_blank" href="https://www.lab6.com/1">Download the PDF + MP3 Polyglot</a></p>
<hr />
<h1 id="heading-polyquine">PolyQuine</h1>
<p>A Polyglot program is not required to have a self-reproducing quality. But if a polyglot program also becomes a valid Quine in one or more of its possible ways to execute. Then it would be termed as a <strong>PolyglotQuine</strong> or <strong>PolyQuine</strong> for short.</p>
<p>However, let's clear up a common misconception:</p>
<blockquote>
<p>All PolyQuines are <a target="_blank" href="https://darshan.hashnode.dev/multiquine">MultiQuines</a>, but all MultiQuines are not <a target="_blank" href="https://darshan.hashnode.dev/polyquine">PolyQuines</a>.</p>
</blockquote>
<p>While both involve multiple languages, MultiQuines can transform into different languages based on command-line arguments, whereas PolyglotQuines remain static but are valid in multiple languages simultaneously. But, as stated earlier, PolyglotQuines don't exist for every combination of programming languages (although it is true that some people have been incredibly smart at constructing them).</p>
<h2 id="heading-2nd-order-polyquine">2nd-order PolyQuine</h2>
<p>This is a Polyglot in C and Haskell, which is also a valid Quine, in both those languages!</p>
<p><a target="_blank" href="https://github.com/lamarqua/polyquine/blob/master/test.c">GitHub</a></p>
<h2 id="heading-another-2nd-order-polyquine">Another 2nd-order PolyQuine</h2>
<p>This is a Polyglot in Racket and Perl, which is also a valid Quine, in both those languages!</p>
<p><a target="_blank" href="https://github.com/lamarqua/polyquine/blob/master/quine2.pl">GitHub</a></p>
<h2 id="heading-4th-order-palindromic-polyquine">4th-order Palindromic PolyQuine</h2>
<p>Here's a bind blowing 4th order PaliPolyQuine in Perl, Ruby, PHP, and JavaScript. PaliPolyQuine is a program that is a Quine, a Polyglot, and a Palindrome.</p>
<p><a target="_blank" href="https://codegolf.stackexchange.com/a/156562">Codegolf post</a></p>
<ul>
<li><p>Perl: <a target="_blank" href="https://tio.run/##TY/BisIwFEX38xXy5tUmUNK6mE1D6MKdCH6AaKo1aiU0tSkipfTXO2kjzJC3yLnvXnJTq0b/jCNKEWInkgxwCaxS7zZdqNdJE6ibsmqvojCVNVoxbW781JpzvlkXOVDu1wQCK0Vgp8PnnGPK4/g7jjmVgSX69FLcG4RjiPAisNsnh76/H7cRPidazbSLUDrBOeZ5RpBAhpLZWpctAaCsUS/VWEUoe5iymqR0GD6iT9LQ18B/LdCXCOkFoydGys6JYUgpAKlK82CUfEQ2SW2pa8skZu79KXCZx10kRrvjve8Pq32HwgnbmZKJnANsIMX808CXcPzXwga@hjcIx0CubVU2NaeQF@tNfjbtid@MZkobW5lC@DXMuUXavlXFYImQJaLDUEj8Gsdf">Try it online!</a></p>
</li>
<li><p>Ruby: <a target="_blank" href="https://tio.run/##TY/BjoIwFEX38xXmzUNoQgqzpWlYuDMmfoDRgloVQ1qkHeMQwq8zhZrMpG/Rc9@96W37ffwZRxQ8xI6nOeASqJIvmy3ks6wjaNpK2Qs/aWV0LWmtr6y0@lisV6cCCPPrCAIjeGCmw@acY8KS5DNJGBGBieryKZk3cMcQ45ljt0v3fX87bGJ8TPQ10zZG4QTnmOcRQwo5CmqaurIRAKGtfMrWyIjQu67UJGXD8BZ9koS@Bv5rgb5ESM4YPzCWZk4MQ0YAIlXpOyXRW6STZKu6MVRg7t6fAud53EVgvD3c@n7/teuQO2EzUzqRc4AJBJ9/GvgSjv9amMDX8AbuGKKLVVXbMALFabUujtqW7KprKmttlD5xv4Y5t8jsSyoKS4Q85R2GXODHOP4C">Try it online!</a></p>
</li>
<li><p>PHP: <a target="_blank" href="https://tio.run/##TY/BioMwFEX38xXlzbMmINFuDcFFd6XQDyhtdGzaWoKxRkoR8dedaAoz5C1y7ruX3DT3ZppQihB7kWSAa2C1enfpSr0KTaBpq7q7itLU1mjFtLnxojM/@W5b5kC5XxMIrBSBnQ9fco4pj@PvOOZUBpbo4qW4NwjHEOFFYH9MTsNwP@8jfM60WegQoXSCcyzzjCCBDCWzja46AkBZq16qtYpQ9jBVPUvpOH5En6Shr4H/WqAvEdILRk@MlF0S45hSAFJX5sEo@YhslrpKN5ZJzNz7c@CyjLtIjA7n@zCcNscehRP2CyUzOQfYQIrlp4Ev4fivhQ18DW8QjoFcu7pqG04hL7e7/Md0Bb8ZzZQ2tjal8GtYcqu0e6uawRohS0SPoZD4NU2/">Try it online!</a></p>
</li>
<li><p>JavaScript: (Try in console)</p>
</li>
</ul>
<pre><code class="lang-Perl">$_=<span class="hljs-string">'$z=0?"$&amp;".next: eval("printf=console.log;atob`JCc`");printf("%s_=%s%s%s;eval(%s_);//#//;)_%s(lave;%s%s%s=_%s",$d=$z[0]||h^L,$q=$z[1]||h^O,$_,$q,$d,$d,$q,"0"?$_.split("").reverse().join(""):~~reverse,$q,$d)'</span>;<span class="hljs-keyword">eval</span>($_);<span class="hljs-regexp">//</span><span class="hljs-comment">#//;)_$(lave;')d$,q$,esrever~~:)""(nioj.)(esrever.)""(tilps._$?"0",q$,d$,d$,q$,_$,O^h||]1[z$=q$,L^h||]0[z$=d$,"s%_=s%s%s%;eval(s%_);//#//;)_s%(lave;s%s%s%=_s%"(ftnirp;)"`cCJ`bota;gol.elosnoc=ftnirp"(lave :txen."&amp;$"?0=z$'=_$</span>
</code></pre>
<h2 id="heading-writing-your-own-polyglot">Writing your own Polyglot</h2>
<p>Let's print the same thing in both <em>C</em> and <em>Python</em> in the same script.</p>
<p>Python / C:</p>
<pre><code class="lang-Python">int main(){printf(<span class="hljs-string">"wubbalubbadubdub"</span>);}
print(<span class="hljs-string">"wubbalubbadubdub"</span>)
</code></pre>
<p>Now, the problem is, <em>Python</em> throws a syntax error on line 1, and <em>C</em> on line 2.</p>
<p>Let's try to ignore line 1 in Python, by commenting it out.</p>
<p>Python / C:</p>
<pre><code class="lang-Python"><span class="hljs-comment">#int main(){printf("wubbalubbadubdub");}</span>
print(<span class="hljs-string">"wubbalubbadubdub"</span>)
</code></pre>
<p>This works perfectly in <em>Python</em>, but the <em>C</em> compiler still doesn't know what <code>print("wubbalubbadubdub")</code> means.</p>
<p>Luckily, <em>C</em> lets you define MACROS! Let's define a MACRO called <code>print</code> such that it takes a string <code>x</code> and replaces it with the current source code of <em>C</em></p>
<p>Python: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/XzklNS0zL1WhoCgzr0SjQlMBSCnkJmbmaWhWg8XSgILWtVwQeaXy0qSkxBwQkVKaBERKmv//AwA">Try it online!</a></p>
<p>C: <a target="_blank" href="https://tio.run/##S9ZNT07@/185JTUtMy9VoaAoM69Eo0JTAUgp5CZm5mloVoPF0oCC1rVcEHml8tKkpMQcEJFSmgRESpr//wMA">Try it online!</a></p>
<pre><code class="lang-Python"><span class="hljs-comment">#define print(x) int main(){printf(x);}</span>
print(<span class="hljs-string">"wubbalubbadubdub"</span>)
</code></pre>
<p>There we go. It's a valid 2nd-order Polyglot!</p>
<blockquote>
<p>At its core, an nth-order Polyglot is a game of n clever ways to hide irrelevant code in comments across n programming languages.</p>
</blockquote>
<hr />
<p>Sources and references:</p>
<ul>
<li><p><a target="_blank" href="https://en.wikipedia.org/wiki/Polyglot_(computing)">Polyglot</a>, wiki page.</p>
</li>
<li><p><a target="_blank" href="https://esolangs.org/wiki/Polyglot">Polyglot</a>, esolang wiki page.</p>
</li>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=yeQ-6eyMQ_o">How The Rosetta Stone Unlocked Hieroglyphics</a>, youtube video by Tom Scott.</p>
</li>
<li><p><a target="_blank" href="https://sketchfab.com/3d-models/the-rosetta-stone-1e03509704a3490e99a173e53b93e282">3D Model of the Rosetta stone</a> webpage by The British Museum.</p>
</li>
<li><p><a target="_blank" href="https://codegolf.stackexchange.com/questions/102370/add-a-language-to-a-polyglot?answertab=modifieddesc#tab-top">Code Golf Polyglot</a>, Code Golf post to write the highest nth-order Polyglot.</p>
</li>
<li><p><a target="_blank" href="https://codegolf.stackexchange.com/questions/93324/write-a-palindrome-polyglot-quine">Write a Palindrome-Polyglot-Quine</a> Code Golf post to write the highest nth-order PaliPolyQuine.</p>
</li>
<li><p><a target="_blank" href="https://www.nyx.net/~gthompso/poly/polyglot.htm">The Polyglot List</a>, blog by Gary P. Thompson II.</p>
</li>
<li><p><a target="_blank" href="https://darshan.hashnode.dev/polyquine">PolyQuine</a>, self-referencing blog that has a polyglot-ish paragraph in English, Spanish, and Hindi.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[(5 of 7) Radiation hardened Quine: Whatever doesn't kill you, simply makes you stronger]]></title><description><![CDATA[Welcome back to our series on Quines! We've delved deep into the world of self-replication, exploring everything from introns to multi-language shape-shifters. Now, let's push the limits of Quine resilience and talk about a fascinating concept: Radia...]]></description><link>https://blog.darshanas.dev/rhquine</link><guid isPermaLink="true">https://blog.darshanas.dev/rhquine</guid><category><![CDATA[quine]]></category><category><![CDATA[dna]]></category><category><![CDATA[self-rep]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Darshan A S]]></dc:creator><pubDate>Sat, 03 Aug 2024 16:43:56 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1722703227004/943e9d51-698f-4cc1-9c97-75c428792bd6.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Welcome back to our series on Quines! We've delved deep into the world of self-replication, exploring everything from introns to multi-language shape-shifters. Now, let's push the limits of Quine resilience and talk about a fascinating concept: <a target="_blank" href="https://darshan.hashnode.dev/rhquine"><strong>Radiation Hardened Quine</strong></a>.</p>
<p>As we've learned, a Quine is a harmonious blend of <em>data</em> and <em>code</em>. The <em>data</em> holds the program's source code, while the <em>code</em> itself knows how to interpret this data and reproduce both itself and the data. It's a beautiful dance of self-reference, where the data becomes the code, and the code brings the data to life.</p>
<h2 id="heading-self-healing">Self healing</h2>
<p>Now what if we are given only the <em>data</em> part of the Quine? Can we recover the <em>code</em> part and vice-versa? In the analogy of cellular biology we've been using, this is the equivalent of having the DNA given and wanting to reconstruct a cell.</p>
<p>Say, if I had given you only the <em>data</em> part of the original Quine we wrote, such as below:</p>
<p>Python:</p>
<pre><code class="lang-Python">data = <span class="hljs-string">"print('data =', repr(data)); print(data)"</span>
</code></pre>
<p>You probably won't have much trouble recovering the original Quine, since the encoding used in <em>data</em> is so trivial. All you have to do is just copy-paste the value of the <code>data</code> as <em>code</em>.</p>
<p>What if the <em>data</em> is encoded, say in ASCII, much like how DNA would be encoded in base pairs? Can you find out the <em>code</em> of the below <em>data</em> section and recover the original Python Quine I wrote?</p>
<p>Python:</p>
<pre><code class="lang-Python">data = [<span class="hljs-number">112</span>, <span class="hljs-number">114</span>, <span class="hljs-number">105</span>, <span class="hljs-number">110</span>, <span class="hljs-number">116</span>, <span class="hljs-number">40</span>, <span class="hljs-number">39</span>, <span class="hljs-number">100</span>, <span class="hljs-number">97</span>, <span class="hljs-number">116</span>, <span class="hljs-number">97</span>, <span class="hljs-number">32</span>, <span class="hljs-number">61</span>, <span class="hljs-number">32</span>, <span class="hljs-number">39</span>, <span class="hljs-number">44</span>, <span class="hljs-number">32</span>, <span class="hljs-number">100</span>, <span class="hljs-number">97</span>, <span class="hljs-number">116</span>, <span class="hljs-number">97</span>, <span class="hljs-number">41</span>, <span class="hljs-number">10</span>, <span class="hljs-number">112</span>, <span class="hljs-number">114</span>, <span class="hljs-number">105</span>, <span class="hljs-number">110</span>, <span class="hljs-number">116</span>, <span class="hljs-number">40</span>, <span class="hljs-number">98</span>, <span class="hljs-number">121</span>, <span class="hljs-number">116</span>, <span class="hljs-number">101</span>, <span class="hljs-number">115</span>, <span class="hljs-number">40</span>, <span class="hljs-number">100</span>, <span class="hljs-number">97</span>, <span class="hljs-number">116</span>, <span class="hljs-number">97</span>, <span class="hljs-number">41</span>, <span class="hljs-number">46</span>, <span class="hljs-number">100</span>, <span class="hljs-number">101</span>, <span class="hljs-number">99</span>, <span class="hljs-number">111</span>, <span class="hljs-number">100</span>, <span class="hljs-number">101</span>, <span class="hljs-number">40</span>, <span class="hljs-number">34</span>, <span class="hljs-number">97</span>, <span class="hljs-number">115</span>, <span class="hljs-number">99</span>, <span class="hljs-number">105</span>, <span class="hljs-number">105</span>, <span class="hljs-number">34</span>, <span class="hljs-number">41</span>, <span class="hljs-number">41</span>] <span class="hljs-comment"># ascii encoded</span>
</code></pre>
<p>This process is called <em>bootstrapping</em>, and it is similar to the process of bootstrapping, say, a C compiler (you start with an initial C compiler, which may be much simpler, much less feature-full, or much less efficient, than the C compiler you want to build, and you run it on the sources of the desired C compiler, giving a first binary C compiler, which you use a second time to recompile its own sources).</p>
<p>In the book <em>The C Programming Language</em>, <a target="_blank" href="https://en.wikipedia.org/wiki/Dennis_Ritchie">Dennis Ritchie</a>, <a target="_blank" href="https://en.wikipedia.org/wiki/Brian_Kernighan">Brian Kernighan</a>, states:</p>
<blockquote>
<p>The operating system, <em>the C compiler</em> itself, and essentially all UNIX application programs are written in C.</p>
</blockquote>
<p>The possibility of bootstrapping means that to some extent Quines are self-healing: if the code is damaged but still able to use the data to recover the original code, bootstrapping can be performed.</p>
<p>Essentially, a Radiation hardened Quine of order n is a program such that:</p>
<ul>
<li><p>It works as a regular Quine, and replicates itself when run normally.</p>
</li>
<li><p>When run, after deleting at-most any n characters in the source, still produces the original Quine as is.</p>
</li>
</ul>
<p>Note that bootstrapping won't be possible without the key, if say the <em>data</em> section used encryption instead of encoding.</p>
<h2 id="heading-1st-order-rh-quine">1st-order RH Quine</h2>
<p>A self-healing Quine in Ruby with an ASCII art depicting the "Radiation" symbol. Delete any one character in the source code, and check if it still prints the original when run.</p>
<p><a target="_blank" href="https://github.com/mame/radiation-hardened-quine">GitHub</a></p>
<p>Ruby: <a target="_blank" href="https://tio.run/##ZZJrd5pAEIa/76/YMhp3NeEOwlESWpvW3u@XpKcQYjFyTNQKtKQef7vd5SIa3w8DO8wMu/s@y/T6frMJ/wS3Th1aPDZip/mb8DfS/EsWaRITUiZYFapqvUsPwPMlWPGlm4donD9p58xtK3KnScQ4@heiU1gRNpTU06ERU6A0/3yirNdreP3m7bv3Hz5@8gGSSYbE8W1wEyXRPI0fAWCmYqjKhn7@8vUbz3y/kMTIXYbxKA0BkORriq4YtmoaGi5kWHbX1jp5a5lSTDaho3YVS7UQD7phy3oPb@XbVtc0dE1V6pzc65XnQmwPFpsgZHhHDiGwisV4N5ferU94k4j8OL2jTdUwdz9jKo4my95eqvqJi4qD7g/Eo2BGJImu9pKB07jqZJ0G8vxATGeLYK9jWjyJt5sdtj36Q/4pokmYNW0my3Ycvn/ePqqqpkQYtoW8sK5D66OjnIyArkEAEHy@qlpy84HJJQPqDuaL@2V0gyaJq8qK7l6kcToN3fPZr/nEhaKD1TIKkiURXN8TjjWVX0of9RW539dsxkd6Tc4uj2NKeyHOogQeP8GDp@fPng9fvHxF201CKYUWQAuVHOI8RGO8uy@Mc9BOTVvdIQ3lpB2CVoljliOGK8QkVBNWooUfqCSrBgttcSohwgcSMsaPrna1bglMxUuByEGXGFckVOa3UOH9aEpaw3brgWmOY1qyrj5wDm2vizuGmWO4dGySYG4YLgzDuWHcKdhs/gM">Try it online!</a></p>
<pre><code class="lang-Ruby">eval=eval=eval=<span class="hljs-string">'eval$s=%q(eval(%w(puts((%q(eval=ev
al=eval=^Z^##^_/<span class="hljs-subst">#{eval@eval@if@eval)+?@*<span class="hljs-number">10</span>+<span class="hljs-string">%(.size
&gt;<span class="hljs-subst">#{(s=<span class="hljs-string">%(eval$s=%q(#$s)</span><span class="hljs-comment">#)).size-1}}}#LMNOPQRS_##thx</span>
.flagitious!<span class="hljs-comment">##    )+?@*12+%(TUVW    XY/.i<span class="hljs-doctag">@rescue</span>##</span>
/_3141592653       <span class="hljs-number">589793</span>+)+?@*       <span class="hljs-number">16</span>+<span class="hljs-string">%(+271828
182845904;          _987654321          0;;eval)</span>+?
@*<span class="hljs-number">18</span>+<span class="hljs-string">%("x            =((<span class="hljs-subst">#{s.s            um}</span>-eval.
_sum)</span>%<span class="hljs-number">256</span>             ).chr;             ;eval)+?@
*<span class="hljs-number">12</span>+<span class="hljs-string">%(.s             can(//)</span>{             a=$`+x+$
^_a.unpa            ck      (^            H*^)[<span class="hljs-number">0</span>].
hex%<span class="hljs-number">999989</span>==<span class="hljs-comment">#{s.unpac        k("H*")[0].hex%999989</span>
}</span>&amp;&amp;eval(a)</span>}</span>#"##"_eval        @eval####@(C)@Copyrig
ht@2014@Yusuke@Endoh@#      ###)).tr("@_^",32.chr&lt;
&lt;10&lt;&lt;39).sub(?Z,s));e xit#AB CDEFGHIJK)*%()))#'</span><span class="hljs-comment">##'</span>
/<span class="hljs-comment">#{eval eval if eval          .size&gt;692}}#LMNOPQRS</span>
<span class="hljs-comment">##thx.flagitious!##            TUVWXY/.i rescue##/</span>
<span class="hljs-number">3141592653589793</span>+                +<span class="hljs-number">271828182845904</span>;
<span class="hljs-number">9876543210</span>;;eval                  <span class="hljs-string">"x=((42737-eval.
sum)%256).chr;;eval            .scan(//){a=$`+x+$'
a.unpack('H*')[0].hex%999989==68042&amp;&amp;eval(a)}#"</span><span class="hljs-comment">##"</span>
eval eval<span class="hljs-comment">#### (C) Copyright 2014 Yusuke Endoh ####</span>
</code></pre>
<h2 id="heading-3rd-order-rh-quine">3rd-order RH Quine:</h2>
<p>This is a Perl program, that is self healing upto loss of 3 characters.</p>
<p><a target="_blank" href="https://codegolf.stackexchange.com/a/100785">Codegolf post</a></p>
<p>Perl: (Due to issues in ASCII code copy pasting. Unable to Try Online)</p>
<pre><code class="lang-Perl"><span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+(<span class="hljs-string">q(FoPqOlengthFoBBPP181XXVVVVJJJKKKNdoWchopJFtPDevalMODx4KNFrPIPA-MN-TUV-ZPINFsPIFoPqOI.Fo.IQNevalFoINevalIFsPZyI.Fr.IT-UPINsayDFtJqJFsKPZyPT-UFWrYrKD.DEEEEQDx6NsayDNDforB1..4YforB1..4NexitQNevalFo)</span>=~<span class="hljs-keyword">y</span>=A-Z=-+;-AZz-~=r)<span class="hljs-comment">####&gt;####&gt;####&gt;####&gt;####&gt;####&gt;</span>
;
;
;
;
<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+(<span class="hljs-string">q(FoPqOlengthFoBBPP181XXVVVVJJJKKKNdoWchopJFtPDevalMODx4KNFrPIPA-MN-TUV-ZPINFsPIFoPqOI.Fo.IQNevalFoINevalIFsPZyI.Fr.IT-UPINsayDFtJqJFsKPZyPT-UFWrYrKD.DEEEEQDx6NsayDNDforB1..4YforB1..4NexitQNevalFo)</span>=~<span class="hljs-keyword">y</span>=A-Z=-+;-AZz-~=r)<span class="hljs-comment">####&gt;####&gt;####&gt;####&gt;####&gt;####&gt;</span>
;
;
;
;
<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+(<span class="hljs-string">q(FoPqOlengthFoBBPP181XXVVVVJJJKKKNdoWchopJFtPDevalMODx4KNFrPIPA-MN-TUV-ZPINFsPIFoPqOI.Fo.IQNevalFoINevalIFsPZyI.Fr.IT-UPINsayDFtJqJFsKPZyPT-UFWrYrKD.DEEEEQDx6NsayDNDforB1..4YforB1..4NexitQNevalFo)</span>=~<span class="hljs-keyword">y</span>=A-Z=-+;-AZz-~=r)<span class="hljs-comment">####&gt;####&gt;####&gt;####&gt;####&gt;####&gt;</span>
;
;
;
;
<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+&lt;<span class="hljs-keyword">eval</span>+(<span class="hljs-string">q(FoPqOlengthFoBBPP181XXVVVVJJJKKKNdoWchopJFtPDevalMODx4KNFrPIPA-MN-TUV-ZPINFsPIFoPqOI.Fo.IQNevalFoINevalIFsPZyI.Fr.IT-UPINsayDFtJqJFsKPZyPT-UFWrYrKD.DEEEEQDx6NsayDNDforB1..4YforB1..4NexitQNevalFo)</span>=~<span class="hljs-keyword">y</span>=A-Z=-+;-AZz-~=r)<span class="hljs-comment">####&gt;####&gt;####&gt;####&gt;####&gt;####&gt;</span>
;
;
;
;
</code></pre>
<h2 id="heading-14th-order-rh-quine">14th-order RH Quine</h2>
<p>A Befunge program which is so resilient, that can handle deletions upto 14 characters!</p>
<p><a target="_blank" href="https://codegolf.stackexchange.com/a/103555">Codegolf post</a></p>
<p>Befunge: <a target="_blank" href="https://tio.run/##pdKxCsIwFEbhZ6kVBDUQcal28UFcLE0EBXEpKD58BLdzhiD0I0Ohf5N7ezOkPD2uKRy68Hz/HkvJMf69XrSgTCuKdKY5YZ2r8I4CHampUlUX0s4D6e2abrQk9buhO21pTwrrIDX4IZXR0UQK609qKOpI3@rWtdRTfaBq/0QjKaydE9WnoCJL@QI">Try it online!</a></p>
<pre><code class="lang-c">f00f00f00f00f00f00f00f00f00f00f00f00f00f00f0xxxxxxxxxxxxxxx<span class="hljs-string">""</span><span class="hljs-string">""</span><span class="hljs-string">""</span><span class="hljs-string">""</span><span class="hljs-string">""</span><span class="hljs-string">""</span><span class="hljs-string">""</span><span class="hljs-string">"fffffffffffffff'''''''''''''''000000000000000\\\\\\\\\\\\\\\'''''''''''''''000000000000000\\\\\\\\\\\\\\\'''''''''''''''fffffffffffffff\\\\\\\\\\\\\\\111111111111111---------------:::::::::::::::!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!000000000000000aaaaaaaaaaaaaaa---------------bbbbbbbbbbbbbbb---------------***************jjjjjjjjjjjjjjj$$$$$$$$$$$$$$$'''''''''''''''+++++++++++++++kkkkkkkkkkkkkkk,,,,,,,,,,,,,,,333333333333333kkkkkkkkkkkkkkk$$$$$$$$$$$$$$$000000000000000{{{{{{{{{{{{{{{'''''''''''''''888888888888888uuuuuuuuuuuuuuu'''''''''''''''!!!!!!!!!!!!!!!111111111111111+++++++++++++++'''''''''''''''xxxxxxxxxxxxxxx###############;;;;;;;;;;;;;;;:::::::::::::::!!!!!!!!!!!!!!!kkkkkkkkkkkkkkk@@@@@@@@@@@@@@@dddddddddddddddkkkkkkkkkkkkkkk:::::::::::::::eeeeeeeeeeeeeeekkkkkkkkkkkkkkk,,,,,,,,,,,,,,,;;;;;;;;;;;;;;;</span>
</code></pre>
<p>In the iconic film <a target="_blank" href="http://www.imdb.com/title/tt0107290/?ref_=nv_sr_1">Jurassic Park</a>, chaos theorist Dr. Ian Malcolm utters the now-famous line, <em>Life finds a way</em>, as a response to the park's founder, John Hammond, who naively believes he can control the genetically engineered dinosaurs.</p>
<blockquote>
<p><strong>Dr. Ian Malcolm:</strong> John, the kind of control you're attempting simply is… it's not possible. If there is one thing the history of evolution has taught us it's that life will not be contained. Life breaks free, it expands to new territories and crashes through barriers, painfully, maybe even dangerously, but, uh… well, there it is.</p>
<p><strong>Henry Wu:</strong> You're implying that a group composed entirely of female animals will… breed?</p>
<p><strong>Dr. Ian Malcolm:</strong> No, I'm, I'm simply saying that <em>life, uh… finds a way</em>.</p>
</blockquote>
<p>In case, you've been living under a rock and haven't yet watched the movie; Despite the park's sophisticated security measures, the dinosaurs escape, proving Malcolm's point that life is inherently unpredictable and extremely resilient.</p>
<p>So, next time you encounter a Quine, take a moment to appreciate its resilience and the potential for self-repair that lies within its code. It's a testament to the power of self-reference and the enduring spirit of digital life.</p>
<hr />
<p>Sources and references:</p>
<ul>
<li><p><a target="_blank" href="https://codegolf.stackexchange.com/questions/57257/radiation-hardened-quine">Code Golf Radiation Hardened Quine</a>, Code Golf post to write the highest nth-order RH Quine.</p>
</li>
<li><p><a target="_blank" href="http://www.madore.org/~david/computers/quine.html">Quines (self-replicating programs)</a>, blog by David Madore.</p>
</li>
<li><p><a target="_blank" href="https://github.com/mame/radiation-hardened-quine">Radiation-hardened Quine</a>, GitHub by Yusuke Endoh.</p>
</li>
<li><p><a target="_blank" href="https://en.wikipedia.org/wiki/Bootstrapping_(compilers)">Bootstrapping (compilers)</a>, wiki page.</p>
</li>
<li><p><a target="_blank" href="https://www.bell-labs.com/usr/dmr/www/chist.html">The Development of the C Language</a>, paper by Dennis M. Ritchie.</p>
</li>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=kiVVzxoPTtg">Life finds a way - Dialogue</a>, Youtube video clip from the movie Jurrassic Park.</p>
</li>
<li><p><a target="_blank" href="https://darshan.hashnode.dev/rhquine">Radiation Hardened Quine</a>, self-referencing blog that has a 1st order radiation hardened slug in the url (../rhquine).</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[(4 of 7) MultiQuine: Weaving the web of Quines]]></title><description><![CDATA[Previously, we've seen how to construct QuineRelays by leveraging the existence of Introns. Now, prepare to have your minds blown once again as we unlock the next level of Quine wizardry: MultiQuine
We start by saying what a Bi-Quine (or more general...]]></description><link>https://blog.darshanas.dev/multiquine</link><guid isPermaLink="true">https://blog.darshanas.dev/multiquine</guid><category><![CDATA[dna]]></category><category><![CDATA[quine]]></category><category><![CDATA[self-rep]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Darshan A S]]></dc:creator><pubDate>Sat, 27 Jul 2024 11:23:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1721997171604/584577cd-5d33-4709-bec4-c2da5c0cdc48.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Previously, we've seen how to construct <a target="_blank" href="https://darshan.hashnode.dev/quinerelay">QuineRelays</a> by leveraging the existence of <a target="_blank" href="https://darshan.hashnode.dev/intron">Introns</a>. Now, prepare to have your minds blown once again as we unlock the next level of Quine wizardry: <a target="_blank" href="https://darshan.hashnode.dev/multiquine">MultiQuine</a></p>
<p>We start by saying what a Bi-Quine (or more generally a MultiQuine) is. To begin, here is what it is not: a Bi-Quine is not a program that prints a second program, which in turn prints the first again (actually, it is that, but things are a bit more subtle). That would just be a QuineRelay and it is too easy to do (we have proved the existence of such, using Introns).</p>
<p>A Bi-Quine is a very interesting kind of program: when run normally, it is a <a target="_blank" href="https://darshan.hashnode.dev/quine">Quine</a>. But if it is called with a particular command line argument, it will print a different program, its "sibling". Its sibling is also a Quine, but in a different programming language, so its sibling prints its own source code when run normally. But when run with a particular command line argument, the sibling prints the source code of the original program. So in effect, a Bi-Quine is a set of two programs each of which is able to print either of the two.</p>
<p>Essentially, MultiQuines are set of n programs, in n different languages such that:</p>
<ul>
<li><p>Each program is a valid Quine and outputs itself when executed with no input.</p>
</li>
<li><p>Each program can output the source of any other program in the set, according to the command line argument it is passed.</p>
</li>
</ul>
<p>Think Fully-Connected-Graph here.</p>
<blockquote>
<p>All MultiQuines are QuineRelays, but all QuineRelays are not MultiQuines.</p>
</blockquote>
<p>Note that cheating is not allowed: the command line arguments must not be too long — passing the full source of a program is considered cheating.</p>
<h2 id="heading-5th-order-multiquine-pentaquine">5th-order MultiQuine (PentaQuine)</h2>
<p>Here's a 5th-order MultiQuine made up of Python, Perl, F#, newLISP, and C.</p>
<p><a target="_blank" href="https://github.com/rvantonder/pentaquine">GitHub</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721997073002/3c297895-5749-46a2-a8aa-5b2e163e72bc.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-25th-order-multiquine">25th-order MultiQuine</h2>
<p>A MultiQuine consisting of 25 languages, from Ruby, Octave, CoffeScript, JavaScript, Python and 20 more.</p>
<p><a target="_blank" href="https://github.com/coolwanglu/quine-chameleon">GitHub</a></p>
<p>One of the characters in <em>Six Degrees of Separation</em>, a popular <a target="_blank" href="https://en.wikipedia.org/wiki/Six_Degrees_of_Separation_\(play\)">play</a> and <a target="_blank" href="https://en.wikipedia.org/wiki/Six_Degrees_of_Separation_\(film\)">film</a> by an American playwright <a target="_blank" href="https://en.wikipedia.org/wiki/John_Guare">John Guare</a> states:</p>
<blockquote>
<p>I read somewhere that everybody on this planet is separated by only six other people. Six degrees of separation between us and everyone else on this planet. The President of the United States, a gondolier in Venice, just fill in the names. I find it extremely comforting that we're so close. I also find it like Chinese water torture, that we're so close because you have to find the right six people to make the right connection... I am bound, you are bound, to everyone on this planet by a trail of six people.</p>
</blockquote>
<p>This phenomenon is also called <a target="_blank" href="https://en.wikipedia.org/wiki/Six_degrees_of_separation">The small world theory</a>. Facebook's data team released two papers in November 2011 which document that amongst all Facebook users at the time of research (721 million users with 69 billion friendship links) there was an average distance of 4.74 It had come down from 5.28 in 2008. And by 2016, the separation distance was down to 4.57</p>
<p>Similarly, MultiQuines are a special case of the small world theory. A world where a population of self-sustainable Quines are linked to each other by just one degree of separation.</p>
<h2 id="heading-writing-your-own-multiquine">Writing your own MultiQuine</h2>
<p>The secret behind MultiQuines lies in the clever use of, you guessed it, Introns - those non-coding regions we explored earlier. Each MultiQuine program contains <code>n</code><em>data</em> sets: one for its own code and one for each of its siblings. When executed with a specific argument, a MultiQuine uses the corresponding data set to generate the source code of the requested sibling. It then uses its own data sets to replicate the introns within the sibling's code, ensuring the cycle of transformation continues seamlessly.</p>
<p>Let's take the outline of the QuineRelay we build last time:</p>
<p>Python:</p>
<pre><code class="lang-Python">d1 = <span class="hljs-string">"code part of Python"</span>
d2 = <span class="hljs-string">"code part of JavaScript"</span>
print(<span class="hljs-string">'d1 ='</span>, repr(d1)); print(<span class="hljs-string">'d2 ='</span>, repr(d2)); print(d2)
</code></pre>
<p>JavaScript:</p>
<pre><code class="lang-JavaScript">d1 = <span class="hljs-string">"code part of Python"</span>
d2 = <span class="hljs-string">"code part of JavaScript"</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`d1 = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(d1)}</span>`</span>); <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`d2 = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(d2)}</span>`</span>); <span class="hljs-built_in">console</span>.log(d1 + <span class="hljs-string">''</span>);
</code></pre>
<p>Now, we want to read the command line argument and decide to print <code>d1</code> or <code>d2</code></p>
<p>In Python, <code>sys.argv</code> is the array that holds these. If it says "js" we output the JavaScript MultiQuine If it says "py" or empty we output the same Python MultiQuine back. Note. We are choosing to ignore any other values and just output the same Python Quine Make sure to <code>import sys</code> and also check if <code>sys.argv</code> does have an argument passed in.</p>
<p>Python:</p>
<pre><code class="lang-Python">d1 = <span class="hljs-string">"code part of Python"</span>
d2 = <span class="hljs-string">"code part of JavaScript"</span>
<span class="hljs-keyword">import</span> sys; print(<span class="hljs-string">'d1 ='</span>, repr(d1)); print(<span class="hljs-string">'d2 ='</span>, repr(d2)); print(d2 <span class="hljs-keyword">if</span> len(sys.argv) &gt; <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> sys.argv[<span class="hljs-number">1</span>] == <span class="hljs-string">'js'</span> <span class="hljs-keyword">else</span> d1)
</code></pre>
<p>In Javascript, <code>process.argv</code> is the array that holds these. Use the same conditions to modify the JavaScript MultiQuine as below:</p>
<p>JavaScript:</p>
<pre><code class="lang-JavaScript">d1 = <span class="hljs-string">"code part of Python"</span>
d2 = <span class="hljs-string">"code part of JavaScript"</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`d1 = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(d1)}</span>`</span>); <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`d2 = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(d2)}</span>`</span>); <span class="hljs-built_in">console</span>.log(process.argv.length &gt; <span class="hljs-number">2</span> &amp;&amp; process.argv[<span class="hljs-number">2</span>] == <span class="hljs-string">'py'</span> ? d1 : d2);
</code></pre>
<p>Now, all we have to do is, copy the final <em>code</em> part to each other's <code>d1</code> and <code>d2</code> variables:</p>
<p>Python: <a target="_blank" href="https://tio.run/##xZC9CsIwFEZ3n@KjiGlBCo2bUt0ddHAUQbGxP9QkJEEo4rPHW4u0orvrPR8nh@jGFUrOvM8SpAjKq1bGwTZ2AW1K6ULWAjaFEdqEWRJFPeADwHtA9/KCWsiQNPHJ5LcISyQ4yQzvyz45IE3BKssgaitA5mDUGhGclbSqFnGt8vD4yhrf17vtJraO/Hl5adqOx5Ee/Jzyn1P@PdVGnYXtSmIKzV1BhRyTCYZoz7tI3TCsqBBzkG0RjP75S977yj4B">Try it online!</a> (Play with argument <code>py</code> or <code>js</code>)</p>
<pre><code class="lang-Python">d1 = <span class="hljs-string">"import sys; print('d1 =', repr(d1)); print('d2 =', repr(d2)); print(d2 if len(sys.argv) &gt; 1 and sys.argv[1] == 'js' else d1)"</span>
d2 = <span class="hljs-string">"console.log(`d1 = ${JSON.stringify(d1)}`); console.log(`d2 = ${JSON.stringify(d2)}`); console.log(process.argv.length &gt; 2 &amp;&amp; process.argv[2] == 'py' ? d1 : d2);"</span>
<span class="hljs-keyword">import</span> sys; print(<span class="hljs-string">'d1 ='</span>, repr(d1)); print(<span class="hljs-string">'d2 ='</span>, repr(d2)); print(d2 <span class="hljs-keyword">if</span> len(sys.argv) &gt; <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> sys.argv[<span class="hljs-number">1</span>] == <span class="hljs-string">'js'</span> <span class="hljs-keyword">else</span> d1)
</code></pre>
<p>JavaScript: <a target="_blank" href="https://tio.run/##zY6xCsIwFEV3v@JSxLSghWZUqruDDo4iWNpYU2oSkiAU8dvjqyIq@gGu71zOO01xLlxppfETpSsRQpUhRyRPRlsP17kZjJXKx6wHbAwrjI2rLElegL8B/gJ0lwe0QsWkSQtbnxPMkaFQFZ6XbbZDnoM1jkG0ToDM0aA3Iiq1croVaavreH/PGl6Wm/UqdZ78tTx0fcd1Tw8/p/znlH9PjdWlcI@SlEJrf6RCjtEI72jLH5GmY1hQIaYg2ywa/HthCMF0Nw">Try it online!</a> (Play with argument <code>py</code> or <code>js</code>)</p>
<pre><code class="lang-JavaScript">d1 = <span class="hljs-string">"import sys; print('d1 =', repr(d1)); print('d2 =', repr(d2)); print(d2 if len(sys.argv) &gt; 1 and sys.argv[1] == 'js' else d1)"</span>
d2 = <span class="hljs-string">"console.log(`d1 = ${JSON.stringify(d1)}`); console.log(`d2 = ${JSON.stringify(d2)}`); console.log(process.argv.length &gt; 2 &amp;&amp; process.argv[2] == 'py' ? d1 : d2);"</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`d1 = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(d1)}</span>`</span>); <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`d2 = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(d2)}</span>`</span>); <span class="hljs-built_in">console</span>.log(process.argv.length &gt; <span class="hljs-number">2</span> &amp;&amp; process.argv[<span class="hljs-number">2</span>] == <span class="hljs-string">'py'</span> ? d1 : d2);
</code></pre>
<p>There you go. It's a valid 2nd-order MultiQuine (Bi-Quine)</p>
<p>If you wanna play around more, <a target="_blank" href="http://www.madore.org/~david/">David Madore</a> has written a <em>C/Perl</em> Bi-Quine. For fun, only the <a target="_blank" href="http://www.madore.org/~david/programs/selfrep/biquine.c">C version</a> is given out; if you want the <em>Perl</em> version you will have to run the program with the <a target="_blank" href="http://foldoc.org/xyzzy">magic word</a>: <strong>xyzzy</strong> as the argument. In the C version, <code>c_data</code> is the main data set and <code>perl_data</code> is an intron; in the Perl version, of course, things are reversed.</p>
<p>One peculiar thing, you might notice in the above <em>C/Perl</em> example, is that the <em>data</em> section is encoded as an array of numbers. This is a common trick to avoid having to deal with nested quoting problem, while writing Quines. In fact if one wanted to, each data set could use a different coding mechanism, or even an encryption mechanism, although you'd need a key to decrypt it in the <em>code</em> section.</p>
<p>Stay tuned for the next post on <a target="_blank" href="https://darshan.hashnode.dev/rhquine">Radiation-Hardened-Quine</a>!</p>
<hr />
<p>Sources and references:</p>
<ul>
<li><p><a target="_blank" href="http://www.madore.org/~david/computers/quine.html">Quines (self-replicating programs)</a>, blog by David Madore.</p>
</li>
<li><p><a target="_blank" href="https://github.com/rvantonder/pentaquine">Pentaquine</a>, GitHub repo by Rijnard van Tonder.</p>
</li>
<li><p><a target="_blank" href="https://github.com/coolwanglu/quine-chameleon#variants">Quine Chameleon</a>, GitHub repo by Lu Wang.</p>
</li>
<li><p><a target="_blank" href="https://en.wikipedia.org/wiki/Six_degrees_of_separation">The small world theory</a>, wiki page.</p>
</li>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=YbRU0b7UOmA">Six Degrees of Separation - Quote</a>, Youtube video clip from the movie Six Degrees of Separation.</p>
</li>
<li><p><a target="_blank" href="https://darshan.hashnode.dev/quine">Quine</a>, <a target="_blank" href="https://darshan.hashnode.dev/intron">Intron</a>, <a target="_blank" href="https://darshan.hashnode.dev/quinerelay">QuineRelay</a>, <a target="_blank" href="https://darshan.hashnode.dev/multiquine">MultiQuine</a>, set of four self-referencing blogs that link to each other in a fully-connected graph.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[(3 of 7) QuineRelay: Birth of the Quine serpent]]></title><description><![CDATA[In the previous adventures of the Quine blog series, we explored how to write our own Quines and Introns. Today we will see what QuineRelays are and how you can leverage Introns to create them.
Imagine a circle of programs, each written in a differen...]]></description><link>https://blog.darshanas.dev/quinerelay</link><guid isPermaLink="true">https://blog.darshanas.dev/quinerelay</guid><category><![CDATA[dna]]></category><category><![CDATA[quine]]></category><category><![CDATA[self-rep]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Darshan A S]]></dc:creator><pubDate>Sat, 20 Jul 2024 10:41:10 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1720258068048/1d1cc589-3593-41c7-8de5-628a0b5b9806.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the previous adventures of the Quine blog series, we explored how to write our own <a target="_blank" href="https://darshan.hashnode.dev/quine">Quines</a> and <a target="_blank" href="https://darshan.hashnode.dev/intron">Introns</a>. Today we will see what <a target="_blank" href="https://darshan.hashnode.dev/quinerelay">QuineRelays</a> are and how you can leverage Introns to create them.</p>
<p>Imagine a circle of programs, each written in a different language. Each program in this circle has a single purpose: to print the source code of the next program in the sequence. The last program then closes the loop by printing the source code of the very first program.</p>
<p>Essentially, QuineRelays are a set of n programs, in n different languages such that:</p>
<ul>
<li><p>Each program outputs the source of the next.</p>
</li>
<li><p>The last program outputs back the source of the first one.</p>
</li>
</ul>
<p>Think Circular-Linked-List here. The last example is a mind-blower!</p>
<p>Let's see some examples to grasp the concept better.</p>
<h2 id="heading-2nd-order-relay">2nd-order relay</h2>
<p>This 2nd-order <a target="_blank" href="https://darshan.hashnode.dev/quinerelay#heading-3rd-order-relay">QuineRelay</a> features a JavaScript program that prints a Python program, which then prints the original JavaScript program back. It's a dynamic duo of self-replication.</p>
<p>JavaScript → Python ⥀</p>
<p>JavaScript: <a target="_blank" href="https://tio.run/##y0osSyxOLsosKNEts/j/Pzk/rzg/J1UvJz9dQ6PQNt7WLqGgKDOvREOl2ivY30@vuATIS89Mq9RIQFOqUl2oHR1bq6mhqZmgWQvEQMb//wA">Try it online!</a></p>
<pre><code class="lang-Javascript"><span class="hljs-built_in">console</span>.log((q=<span class="hljs-function"><span class="hljs-params">_</span>=&gt;</span><span class="hljs-string">`print(<span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(<span class="hljs-string">`console.log((q=<span class="hljs-subst">${q+[]}</span>)())`</span>)}</span>)`</span>)())
</code></pre>
<p>Python: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/v6AoM69EQyk5P684PydVLyc/XUOj0Dbe1i4BIqNS7RXs76dXXALkpWemVWokoClVqS7Ujo6t1dTQ1EzQrAViIENJ8/9/AA">Try it online!</a></p>
<pre><code class="lang-Python">print(<span class="hljs-string">"console.log((q=_=&gt;`print(${JSON.stringify(`console.log((q=${q+[]})())`)})`)())"</span>)
</code></pre>
<h2 id="heading-3rd-order-relay">3rd-order relay</h2>
<p>Things get even more interesting with a 3rd-order <a target="_blank" href="https://darshan.hashnode.dev/quinerelay#heading-4th-order-relay">QuineRelay</a>. This one starts with a Haskell program, which outputs a Python program, which outputs a Ruby program, and finally, the Ruby program loops back to the original Haskell program.</p>
<p>Haskell → Python2 → Ruby ⥀</p>
<p>Haskell: <a target="_blank" href="https://tio.run/##vY89C4MwFEX3/oqHFF5CQlHaqcWxW7eOxSHGFKUaTYzUf2/jFxW6dzvvcLmXl4v2pcpyGAwISEHGTefuzt407P3JGDxcfdVdBVHI0SBHgsmo27x@LwEvv0r@KjErismuEoWODQT/GAsgyNQTDBE85ZKeG1toBymTuSVRSBkagsyqxpLUH3xhuWHhmeLSo0hPwSrXWQ3H08G3sJ7NAEpnl@0WrFtROAVx@RuZmsYmkCuIGebs2BUMwwc">Try it online!</a></p>
<pre><code class="lang-Haskell"><span class="hljs-title">q</span> a b c=putStrLn $ b ++ [toEnum <span class="hljs-number">10</span>,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']
<span class="hljs-title">main</span>=q <span class="hljs-string">"q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']"</span> <span class="hljs-string">"def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'"</span> <span class="hljs-string">"def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end"</span>
</code></pre>
<p>Python2: <a target="_blank" href="https://tio.run/##rY6xDoIwFEV3v@KFmLRNGwPqpGF0c3M0DKXUwEBpa4n49bVQiCSubued3Nz79NvVndp7X8kHGMxZyQQ5adsoByUVtcVZSigyGFErtcVlONjMYsU8MEEbg5O/FCVs6pF4IGCl662Cw3EXWuhAI4BU1Xm9BctWlk5B1PJG5QYQldPYBGIBHiFmx66waIBDCSLXvbs5e1WwDSelcHfdRfUtZClDJjyKUTHqZ9295kCQXyV@FY@KoCIh3n8A">Try it online!</a></p>
<pre><code class="lang-Python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">q</span>(<span class="hljs-params">a,b,c</span>):</span><span class="hljs-keyword">print</span> b+chr(<span class="hljs-number">10</span>)+<span class="hljs-string">'q('</span>+repr(b)+<span class="hljs-string">','</span>+repr(c)+<span class="hljs-string">','</span>+repr(a)+<span class="hljs-string">')'</span>
q(<span class="hljs-string">"def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'"</span>,<span class="hljs-string">"def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end"</span>,<span class="hljs-string">"q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']"</span>)
</code></pre>
<p>Ruby: <a target="_blank" href="https://tio.run/##vY69CsIwFEZ3n@JShJuQIC06KR3d3BylQ5JG2sG0iSnWp69Jf7Dg7nZy@Mg9rpPvYSj1HTTpKTjtO2dgf9ipyrGeTQDalKe4sURwyRWF1tXGg2RZOg7xIWqTW0CmiaQMR1ALiAmmbfxrY0ny55MJTywIkKDytvNX7y4GtuHJGNx8czbdA7KUo0WOBIuon1XzmgdBfpX6VWJSFItwZh19XKJDBcnSEGUJMqdbNzbzmdWKYznFhA7DBw">Try it online!</a></p>
<pre><code class="lang-Ruby"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">e</span><span class="hljs-params">(x)</span></span> <span class="hljs-keyword">return</span> <span class="hljs-number">34</span>.chr+x+<span class="hljs-number">34</span>.chr <span class="hljs-keyword">end</span>;<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">q</span><span class="hljs-params">(a,b,c)</span></span> print b+<span class="hljs-number">10</span>.chr+<span class="hljs-string">'main=q '</span>+e(b)+<span class="hljs-string">' '</span>+e(c)+<span class="hljs-string">' '</span>+e(a)+<span class="hljs-string">' '</span>+<span class="hljs-number">10</span>.chr <span class="hljs-keyword">end</span>
q(<span class="hljs-string">"def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end"</span>,<span class="hljs-string">"q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']"</span>,<span class="hljs-string">"def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'"</span>)
</code></pre>
<h2 id="heading-4th-order-relay">4th-order relay</h2>
<p>Here's a 4th-order <a target="_blank" href="https://darshan.hashnode.dev/quinerelay#heading-128th-order-ouroboros-relay">QuineRelay</a>: Ruby → Java → C# → Python ⥀</p>
<p><a target="_blank" href="https://github.com/IRus/Quine">GitHub</a></p>
<p>Across cultures, there are several shared symbolisms. One of them is the <a target="_blank" href="https://en.wikipedia.org/wiki/Ouroboros">Ouroboros</a>, which has references in Egyptian, Greek, Roman, Hindu, Siberian, Norse, African, and South American mythologies. The Ouroboros is an ancient symbol depicting a serpent or dragon consuming its own tail, representing the eternal cycle of creation and destruction.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721333546475/51ed4670-9904-4ac3-8cfd-21acb1dbca57.jpeg" alt class="image--center mx-auto" /></p>
<blockquote>
<p>In Norse mythology, <a target="_blank" href="https://en.wikipedia.org/wiki/J%C3%B6rmungandr">Jörmungandr</a> is a colossal serpent, the middle child of Loki and the giantess Angrboða. Odin cast Jörmungandr into the ocean surrounding Midgard (the realm of humans), where the serpent grew so immense that it encircled the world and grasped its own tail. As a result of it surrounding Midgard (the Earth) it is referred to as the World Serpent - Ouroboros. Jörmungandr releasing its tail is one of the signs of the beginning of <a target="_blank" href="https://en.wikipedia.org/wiki/Ragnar%C3%B6k">Ragnarök</a> (the final battle of the world).</p>
</blockquote>
<p>Quine relays embody this symbolism quite neatly, as each program in the cycle gives rise to the next, only to be reborn in the end, and hence are nicknamed <em>Ouroboros</em> programs.</p>
<h2 id="heading-128th-order-ouroboros-relay">128th-order Ouroboros relay</h2>
<p>Hold on to your seats. Here's a 128th-order Ouroboros <a target="_blank" href="https://darshan.hashnode.dev/quinerelay#heading-2nd-order-relay">QuineRelay</a>. Yes, you read that right. 128 !!!</p>
<p>Ruby → Rust → Scala → ... (120 others) ... → Python → R → Ratfor → rc → REXX ⥀</p>
<p><a target="_blank" href="https://github.com/mame/quine-relay">GitHub</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721333635071/45640ca0-5fc2-463e-983e-f4a2370d2226.png" alt class="image--center mx-auto" /></p>
<p>As if this wasn't impressive enough, it includes an easter egg. The original Ruby code when zoomed out contains an Ouroboros dragon!</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721333658583/52a61f8f-5201-4ba1-9cb9-2a515822d269.jpeg" alt class="image--center mx-auto" /></p>
<h2 id="heading-writing-your-own-quinerelay">Writing your own QuineRelay</h2>
<p>Let's start the Python intron we wrote earlier and try to turn it into a 2nd-order QuineRelay.</p>
<p>Python Intron: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/PzOvpCg/T8FWQb28NCkpMQdEpJQmAZE6V0piSSJQRqmgCKhKQx2mVF1HoSi1oEgDwtfUtFaAKoCoh0mDeAhJME@Ji2pG/f8PAA">Try it online!</a></p>
<pre><code class="lang-Python">intron = <span class="hljs-string">'wubbalubbadubdub'</span>
data = <span class="hljs-string">"print('intron =', repr(intron)); print('data =', repr(data)); print(data)"</span>
print(<span class="hljs-string">'intron ='</span>, repr(intron)); print(<span class="hljs-string">'data ='</span>, repr(data)); print(data)
</code></pre>
<p>Using the magic of introns, we can now easily put the <em>code</em> part of a sibling Quine of a different language into the intron. Producing a program of the form:</p>
<p>Python:</p>
<pre><code class="lang-Python">intron = <span class="hljs-string">"code part of sibling"</span>
data = <span class="hljs-string">"code part of self"</span>
print(<span class="hljs-string">'intron ='</span>, repr(intron)); print(<span class="hljs-string">'data ='</span>, repr(data)); print(data)
</code></pre>
<p>Since, each variable is just, acting as <em>data</em> of a different Quine. Let's rename <code>data</code> and <code>intron</code>, into <code>d1</code> and <code>d2</code> respectively.</p>
<p>Python:</p>
<pre><code class="lang-Python">d1 = <span class="hljs-string">"code part of self"</span>
d2 = <span class="hljs-string">"code part of sibling"</span>
print(<span class="hljs-string">'d1 ='</span>, repr(d1)); print(<span class="hljs-string">'d2 ='</span>, repr(d2)); print(d1)
</code></pre>
<p>Now, <code>d2</code> acts as an intron, but the above program still tried to print code part of self. To have it print source of the next, let's <code>print(d2)</code> instead of <code>print(d1)</code> at the end.</p>
<p>Python:</p>
<pre><code class="lang-Python">
d1 = <span class="hljs-string">"code part of self"</span>
d2 = <span class="hljs-string">"code part of sibling"</span>
print(<span class="hljs-string">'d1 ='</span>, repr(d1)); print(<span class="hljs-string">'d2 ='</span>, repr(d2)); print(d2)
</code></pre>
<p>We already know that the contents of <code>d1</code> is just a copy of line 3. But we don't yet have the contents of <code>d2</code>.</p>
<p>Say, we wanted to create a QuineRelay with JavaScript. Let's write a similar intron in JS.</p>
<p>JavaScript:</p>
<pre><code class="lang-JavaScript">d1 = <span class="hljs-string">"code part of sibling"</span>
d2 = <span class="hljs-string">"code part of self"</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`d1 = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(d1)}</span>`</span>); <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`d2 = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(d2)}</span>`</span>); <span class="hljs-built_in">console</span>.log(d1);
</code></pre>
<p>Now, line 3 of the above JS intron, is the <em>code</em> of the sibling program we wanted! Paste each other's <em>code</em> as introns in the other. Note. We need to add <code>d1 + ''</code> in js to avoid some quoting mismatches</p>
<p>Python: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/P8VQwVZBqaAoM69EQx3EUddRKEotKNJIMdTUtFaASRghSRghJIBsJS6QpIJScn5ecX5Oql5OfrpGAthUlWqvYH8/veISoNL0zLRKkJG1CUC9qEqNsCo1wlQKNFRbQV1d01qJiwL3/v8PAA">Try it online!</a></p>
<pre><code class="lang-Python">d1 = <span class="hljs-string">"print('d1 =', repr(d1)); print('d2 =', repr(d2)); print(d2)"</span>
d2 = <span class="hljs-string">"console.log(`d1 = ${JSON.stringify(d1)}`); console.log(`d2 = ${JSON.stringify(d2)}`); console.log(d1 + '');"</span>
print(<span class="hljs-string">'d1 ='</span>, repr(d1)); print(<span class="hljs-string">'d2 ='</span>, repr(d2)); print(d2)
</code></pre>
<p>JavaScript: <a target="_blank" href="https://tio.run/##y0osSyxOLsosKNEts/j/P8VQwVZBqaAoM69EQx3EUddRKEotKNJIMdTUtFaASRghSRghJIBsJS6QpIJScn5ecX5Oql5OfrpGAthUlWqvYH8/veISoNL0zLRKkJG1CUC9qEqNsCo1wlQKNFRbQV1d01qJi46W/f8PAA">Try it online!</a></p>
<pre><code class="lang-JavaScript">d1 = <span class="hljs-string">"print('d1 =', repr(d1)); print('d2 =', repr(d2)); print(d2)"</span>
d2 = <span class="hljs-string">"console.log(`d1 = ${JSON.stringify(d1)}`); console.log(`d2 = ${JSON.stringify(d2)}`); console.log(d1 + '');"</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`d1 = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(d1)}</span>`</span>); <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`d2 = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(d2)}</span>`</span>); <span class="hljs-built_in">console</span>.log(d1 + <span class="hljs-string">''</span>);
</code></pre>
<p>There you go. It's a proper 2nd order QuineRelay! A Python program, that prints a JavaScript program, that prints the original Python program back in a cycle.</p>
<p>Creating a QuineRelay is an exercise in creative coding and understanding how different languages represent and manipulate strings. It involves weaving together introns from various programs, each containing the code to replicate its next neighbour.</p>
<blockquote>
<p>At its core, an nth-order relay is a game of n clever ways to escape quotes across n programming languages.</p>
</blockquote>
<p>Stay tuned for the next post on <a target="_blank" href="https://darshan.hashnode.dev/multiquine">MultiQuine</a>!</p>
<hr />
<p>Sources and references:</p>
<ul>
<li><p><a target="_blank" href="https://en.wikipedia.org/wiki/Quine_(computing)#Ouroboros_programs">Ouroboros programs</a>, wiki page.</p>
</li>
<li><p><a target="_blank" href="http://blog.sigfpe.com/2008/02/third-order-quine-in-three-languages.html">A Third Order Quine in Three Languages</a>, blog by sigfpe.</p>
</li>
<li><p><a target="_blank" href="https://github.com/IRus/Quine">Chain Quine</a>, GitHub repo by Ibragimov Ruslan.</p>
</li>
<li><p><a target="_blank" href="https://github.com/mame/quine-relay">128 Quine Relay</a>, GitHub repo by Yusuke Endoh.</p>
</li>
<li><p><a target="_blank" href="https://darshan.hashnode.dev/quinerelay">QuineRelay. Birth of the Quine serpent</a>, self-referencing blog where a word in each n-order example hyperlinks to the next, cyclically.</p>
</li>
<li><p><a target="_blank" href="https://en.wikipedia.org/wiki/J%C3%B6rmungandr">Jörmungandr</a>, wiki page.</p>
</li>
<li><p><a target="_blank" href="https://en.wikipedia.org/wiki/Ouroboros">Ouroboros</a>, wiki page.</p>
</li>
<li><p><a target="_blank" href="https://youtu.be/6avJHaC3C2U?si=zEERK5IyEtrtTD3h">Art of Code</a>, talk by Dylan Beattie</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[(2 of 7) Intron: Junk or not Junk, that is the quine-tion]]></title><description><![CDATA[Last time, we cracked the code of Quines, those clever programs that echo their own source code. We crafted them with surgical precision, including only the bare essentials for self-replication.
But here's a question: Can we sneak in some extra, even...]]></description><link>https://blog.darshanas.dev/intron</link><guid isPermaLink="true">https://blog.darshanas.dev/intron</guid><category><![CDATA[quine]]></category><category><![CDATA[dna]]></category><category><![CDATA[self-rep]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Darshan A S]]></dc:creator><pubDate>Sat, 13 Jul 2024 03:20:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1720257986350/0c260bd0-86a9-40bf-8852-9f798a22b021.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Last time, we cracked the code of <a target="_blank" href="https://darshan.hashnode.dev/quine">Quines</a>, those clever programs that echo their own source code. We crafted them with surgical precision, including only the bare essentials for self-replication.</p>
<p>But here's a question: Can we sneak in some extra, even <em>useless</em>, bits of data into an existing Quine without breaking it?</p>
<p>Enter the world of <a target="_blank" href="http://darshan.hashnode.dev/intron"><strong>Introns</strong></a>, those sneaky bits of data that can hitch a ride within a Quine. Just like the non-coding regions of DNA in biology, these introns are extra passengers that don't directly contribute to the Quine's function, but they get copied along for the ride nonetheless.</p>
<p>In the world of biology, Introns are often called "Junk DNA". Only ~2.5% of the genome encodes sequences that make proteins. The functions for the remaining &gt;90% of our genome is not well understood, but seem to play a crucial role in evolution and gene regulation.</p>
<blockquote>
<p>"Introns are the dark matter of the eukaryotic genome."</p>
</blockquote>
<p>Similarly, in the world of programming, introns might seem like a useless thing to do, but the possible existence of introns will be the key feature in making <a target="_blank" href="https://darshan.hashnode.dev/quinerelay">QuineRelays</a> and <a target="_blank" href="https://darshan.hashnode.dev/multiquine">MultiQuines</a> possible. (some things we will talk about in the upcoming blogs)</p>
<h2 id="heading-writing-your-own-intron">Writing your own Intron</h2>
<p>Let's take our Python Quine from last time and see how to inject an intron.</p>
<p>Original Python Quine: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/PyWxJFHBVkGpoCgzr0RDHcJV11EoSi0o0gDxNDWtFSCSYJ4SF7Eq//8HAA">Try it online!</a></p>
<pre><code class="lang-Python">data = <span class="hljs-string">"print('data =', repr(data)); print(data)"</span>
print(<span class="hljs-string">'data ='</span>, repr(data)); print(data)
</code></pre>
<p>Let's introduce a variable called <code>intron</code> and set it to a seemingly junk string, such as <code>'wubbalubbadubdub'</code>. We'll add it to the <em>data</em> portion of our Quine.</p>
<p>Python:</p>
<pre><code class="lang-Python">intron = <span class="hljs-string">'wubbalubbadubdub'</span>
data = <span class="hljs-string">"print('data =', repr(data)); print(data)"</span>
print(<span class="hljs-string">'data ='</span>, repr(data)); print(data)
</code></pre>
<p>To maintain the Quine's integrity, we need to mirror this addition in the <em>code</em> portion. We'll use the <code>repr(intron)</code> technique to ensure the intron is replicated accurately Python:</p>
<pre><code class="lang-Python">intron = <span class="hljs-string">'wubbalubbadubdub'</span>
data = <span class="hljs-string">"print('data =', repr(data)); print(data)"</span>
print(<span class="hljs-string">'intron ='</span>, repr(intron)); print(<span class="hljs-string">'data ='</span>, repr(data)); print(data)
</code></pre>
<p>Finally, we update the <code>data</code> variable to hold the latest <em>code</em> section.</p>
<p>Python: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/PzOvpCg/T8FWQb28NCkpMQdEpJQmAZE6V0piSSJQRqmgCKhKQx2mVF1HoSi1oEgDwtfUtFaAKoCoh0mDeAhJME@Ji2pG/f8PAA">Try it online!</a></p>
<pre><code class="lang-Python">intron = <span class="hljs-string">'wubbalubbadubdub'</span>
data = <span class="hljs-string">"print('intron =', repr(intron)); print('data =', repr(data)); print(data)"</span>
print(<span class="hljs-string">'intron ='</span>, repr(intron)); print(<span class="hljs-string">'data ='</span>, repr(data)); print(data)
</code></pre>
<p>It's still a valid Quine. And now it's an IntronQuine!</p>
<p>Imagine a year with only ten months! That's how the Romans initially rolled, with January and February being late additions to the party. Romans were also the reason we have the seemingly random leap days thrown around every few years. And to top it all off, did you know that in 1752, India skipped 11 whole days from its calendar?</p>
<p>Run the above IntronQuine and notice how the <code>intron</code> is faithfully reproduced in the output. Try setting <code>intron = 'your name here'</code> The program will still work flawlessly, replicating itself along with your custom message.</p>
<p>The key takeaway is that, quite obviously, an intron can be modified with great ease without breaking the existing Quine; it is a kind of subliminal information that is reproduced with the Quine, although it is not necessary to the Quine. You can change the value of the variable <code>intron</code> to any arbitrary string (as long as it's syntactically right) and the program continues to be a Quine!</p>
<blockquote>
<p>This also means, in principle, you can convert any arbitrary program into a Quine, by just encoding its source code as an intron!</p>
</blockquote>
<p>Oh, and why was the above calendar bit relevant, you might ask? It wasn't. (Or was it?) Much like those seemingly "junk" leap days in a calendar, this is a seemingly "junk" paragraph just hitching a ride on this blog. Sounds familiar? One might even call it an <em>Intron</em> of this very blog! To learn more about the fascinating and quirky story behind it, checkout <a target="_blank" href="https://darshan.hashnode.dev/leap-days-and-the-quest-for-the-perfect-calendar">Leap days, and the quest for the perfect Calendar</a>.</p>
<p>Like I said, Introns might seem like a useless thing to do, but the possible existence of them will open the doors to mind-bending interactions between several Quines.</p>
<p>Stay tuned for the next post on <a target="_blank" href="https://darshan.hashnode.dev/quinerelay"><strong>QuineRelay</strong></a>!</p>
<hr />
<p>Sources and references:</p>
<ul>
<li><p><a target="_blank" href="https://en.wikipedia.org/wiki/Intron">Intron (Biology)</a>, wiki page.</p>
</li>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=a-zEbokJAgY">Self-replicating Python code | Quine</a>, Youtube video by Lex Fridman.</p>
</li>
<li><p><a target="_blank" href="https://mcmanuslab.ucsf.edu/content/illuminating-dark-matter-genome">Illuminating the Dark Matter of the Genome</a>, article by McManus Lab.</p>
</li>
<li><p><a target="_blank" href="https://darshan.hashnode.dev/intron">Intron: Junk or not Junk, that is the quine-tion</a>, self-referencing blog that contains an Intron.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[(1 of 7) Quine: Self replicating computer programs]]></title><description><![CDATA[A Quine, (/kwi:n/ pronounced Kwine) is a self-replicating computer program, a digital life if you will. It takes no input and prints a copy of its own source code.
This may sound impossible, trivial, or completely uninteresting, depending on your tem...]]></description><link>https://blog.darshanas.dev/quine</link><guid isPermaLink="true">https://blog.darshanas.dev/quine</guid><category><![CDATA[self-rep]]></category><category><![CDATA[quine]]></category><category><![CDATA[dna]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Darshan A S]]></dc:creator><pubDate>Sat, 06 Jul 2024 10:28:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1720248258937/fac7cd7f-41a5-4ec2-bd25-b4a279a712fe.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A <a target="_blank" href="https://darshan.hashnode.dev/quine"><em>Quine</em></a>, (/<em>kwi:n</em>/ pronounced Kwine) is a self-replicating computer program, a digital life if you will. It takes no input and prints a copy of its own source code.</p>
<p>This may sound impossible, trivial, or completely uninteresting, depending on your temper and your knowledge of computer science. Trust me, it is not only not impossible, by the end of this post you'll have written your first Quine in Python.</p>
<p>The name "Quine" was coined by <a target="_blank" href="https://en.wikipedia.org/wiki/Douglas_Hofstadter">Douglas Hofstadter</a>, in his popular inter-disciplinary book, <a target="_blank" href="https://en.wikipedia.org/wiki/G%C3%B6del,_Escher,_Bach">Gödel, Escher, Bach</a>, in honour of the philosopher <a target="_blank" href="https://en.wikipedia.org/wiki/Willard_Van_Orman_Quine">Willard Van Orman Quine</a> (1908-2000), who made an extensive study of indirect self-reference, and in particular for the following paradox-producing expression, known as Quine's paradox:</p>
<blockquote>
<p>"yields falsehood when preceded by its quotation" yields falsehood when preceded by its quotation</p>
</blockquote>
<p>The easiest way to write a Quine, of course, is to read the source file on the disk and print its contents. That may be done, but it is considered cheating; besides, the program might not know where the source file is, it may have access to only the compiled data, or the programming language may simply forbid that sort of operation.</p>
<h2 id="heading-your-first-quine">Your first Quine</h2>
<p>Before we start,</p>
<blockquote>
<p>"If you have never done this, I urge you to try it on your own. The discovery of how to do it is a revelation that far surpasses any benefit obtained by being told how to do it." – Ken Thompson</p>
</blockquote>
<p>That said, below is a step-by-step guide to writing your first Quine in Python:</p>
<p>Let's start with 2 pieces in our program.</p>
<ul>
<li><p>A variable to hold the string representation of the source code; call this the <em>Data</em> part.</p>
</li>
<li><p>A print statement to output the variable; call this the <em>Code</em> part.</p>
</li>
</ul>
<p>If we are to take an analogy with cellular biology (thanks to Douglas Hofstadter, again), think of a Quine as a cell with its own DNA. What I have called the <em>data</em> would be the DNA, and the <em>code</em> would be the rest of the cell. The cell is able to create a new cell using the DNA, and this involves, among other things, replicating the DNA itself. So the DNA (the data) contains all the necessary information for the replication, but without the cell (the code), or at least some other code to make the data live, it is a useless, inert, piece of data.</p>
<h4 id="heading-step-1">Step 1</h4>
<p>Let's have a variable to store the <em>data</em> part. And since we don't know the final source code yet, let's use <code>???</code> as a placeholder. Print statements will read the <code>data</code> to replicate itself, thereby acting as the <em>code</em> part.</p>
<p>Python Input: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/PyWxJFHBVkHJ3t5eiaugKDOvRAMkpPn/PwA">Try it online!</a></p>
<pre><code class="lang-python">data = <span class="hljs-string">"???"</span>
print(data)
</code></pre>
<p>Output:</p>
<pre><code class="lang-python">???
</code></pre>
<p>Not quite what we're looking for, is it?</p>
<h4 id="heading-step-2">Step 2</h4>
<p>We can't just print the contents of the <code>data</code> variable. We need to print the very act of declaring the variable itself. Let's add another <code>print</code> statement to do just that.</p>
<p>Python Input: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/PyWxJFHBVkHJ3t5eiaugKDOvREMdIqauowBiaForQITBnP//AQ">Try it online!</a></p>
<pre><code class="lang-python">data = <span class="hljs-string">"???"</span>
print(<span class="hljs-string">'data ='</span>, data); print(data)
</code></pre>
<p>Output:</p>
<pre><code class="lang-python">data = ???
???
</code></pre>
<p>Sweet. It's starting to look structurally similar. But we still have those pesky <code>???</code> marks.</p>
<h4 id="heading-step-3">Step 3</h4>
<p>Notice that the second line of output is <code>???</code>, which corresponds to the part of the source code we initially didn't know how to fill. This means, we can replace <code>???</code> to whatever the second line of source code becomes.</p>
<p>Python Input: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/PyWxJFHBVkGpoCgzr0RDHcJV11EAMTStFSDCYI4SF2E1//8DAA">Try it online!</a></p>
<pre><code class="lang-python">data = <span class="hljs-string">"print('data =', data); print(data)"</span>
print(<span class="hljs-string">'data ='</span>, data); print(data)
</code></pre>
<p>Output:</p>
<pre><code class="lang-python">data = print(<span class="hljs-string">'data ='</span>, data); print(data)
print(<span class="hljs-string">'data ='</span>, data); print(data)
</code></pre>
<p>Almost there! But we seem to be missing the double quotes in line 1.</p>
<h4 id="heading-step-4">Step 4</h4>
<p>In Python</p>
<ul>
<li><p><code>str('hi')</code> outputs <code>hi</code></p>
</li>
<li><p><code>repr('hi')</code> outputs <code>"hi"</code>, with quotes!</p>
</li>
</ul>
<p>That's exactly what we need. Let's wrap <code>data</code> in the second line with <code>repr</code> to get those quotes.</p>
<p>Python Input: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/PyWxJFHBVkGpoCgzr0RDHcJV11EAMTStFSDCYI4SF7qaotSCIogcqsr//wE">Try it online!</a></p>
<pre><code class="lang-python">data = <span class="hljs-string">"print('data =', data); print(data)"</span>
print(<span class="hljs-string">'data ='</span>, repr(data)); print(data)
</code></pre>
<p>Output:</p>
<pre><code class="lang-python">data = <span class="hljs-string">"print('data =', data); print(data)"</span>
print(<span class="hljs-string">'data ='</span>, data); print(data)
</code></pre>
<p>Yay! Line 1 in the source and output are an exact match. But in doing so, we altered line 2 of the source!</p>
<h4 id="heading-step-5">Step 5</h4>
<p>We can fix this by setting the value of the <code>data</code> variable to the updated <em>code</em> part.</p>
<p>Python Input: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/PyWxJFHBVkGpoCgzr0RDHcJV11EoSi0o0gDxNDWtFSCSYJ4SF7Eq//8HAA">Try it online!</a></p>
<pre><code class="lang-python">data = <span class="hljs-string">"print('data =', repr(data)); print(data)"</span>
print(<span class="hljs-string">'data ='</span>, repr(data)); print(data)
</code></pre>
<p>Output:</p>
<pre><code class="lang-python">data = <span class="hljs-string">"print('data =', repr(data)); print(data)"</span>
print(<span class="hljs-string">'data ='</span>, repr(data)); print(data)
</code></pre>
<p>Viola! There you have it. A valid Quine!</p>
<p>Although I've picked Python here, it's purely due to personal familiarity and brevity, there's nothing special about it. Quines are a general consequence of the so-called <a target="_blank" href="https://en.wikipedia.org/wiki/Kleene%27s_recursion_theorem">Kleene's Fixed-Point theorem</a>, which essentially means that a Quine (in fact, infinitely many) can be written in any Turing-complete programming language.</p>
<p>Almost all programming languages you've heard or used so far are Turing complete. No, not HTML and CSS, they are not programming languages, let alone Turing-complete. Get over it.</p>
<p>One word of warning: this code/data distinction in Quines is pleasant and often helpful. It is not, however, completely valid in all circumstances</p>
<h2 id="heading-shortest-quine">Shortest Quine</h2>
<p>In 1983, <a target="_blank" href="https://en.wikipedia.org/wiki/Ken_Thompson">Ken Thompson</a> in his Turing Award acceptance speech, <a target="_blank" href="https://fermatslibrary.com/s/reflections-on-trusting-trust">Reflections on Trusting Trust</a>, presented the persistent compiler backdoor attack now known as the <em>Thompson hack</em> or <em>Trusting trust attack</em>. Although the paper itself is widely considered a seminal computer security work in its own right, he also made a peculiar statement about Quines.</p>
<blockquote>
<p>”In college, before video games, we would amuse ourselves by posing programming exercises. One of the favorites was to write the shortest self-reproducing program. Since this is an exercise divorced from reality, the usual vehicle was FORTRAN. Actually, FORTRAN was the language of choice for the same reason that three-legged races are popular.“ – Ken Thompson</p>
</blockquote>
<p>I claim no knowledge whatsoever of FORTRAN. So, here's the shortest Quine in Python:</p>
<p>Python: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/P95WPd5Wtci6oCgzr0QjXlU1XlMdzonX/P8fAA">Try it online!</a></p>
<pre><code class="lang-python">_=<span class="hljs-string">'_=%r;print(_%%_)'</span>;print(_%_)
</code></pre>
<p>Looks esoteric, doesn't it? But don't let the compactness fool you. Let's decipher how we could have arrived at the shortest one ourselves.</p>
<h4 id="heading-step-1-1">Step 1</h4>
<p>Let's start with</p>
<p>Python Input: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/P0XBVkHd3t5enaugKDOvRCNF8/9/AA">Try it online!</a></p>
<pre><code class="lang-Python">d = <span class="hljs-string">'???'</span>
print(d)
</code></pre>
<p>Output:</p>
<pre><code class="lang-Python">???
</code></pre>
<h4 id="heading-step-2-1">Step 2</h4>
<p>Let's add the entire source code from previous step into <code>d</code>.</p>
<p>Python Input: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/P0XBVkEdRNjb28fkFRRl5pVopGiqc8FY//8DAA">Try it online!</a></p>
<pre><code class="lang-Python">d = <span class="hljs-string">'d = ???\nprint(d)'</span>
print(d)
</code></pre>
<p>Output:</p>
<pre><code class="lang-Python">d = ???
print(d)
</code></pre>
<p>If you noticed the output. We just want to replace <code>???</code> with the exact value of variable <code>d</code> and we are done!</p>
<h4 id="heading-step-3-1">Step 3</h4>
<p>There's a nifty trick in Python to do this. The <code>str(d)</code> and <code>repr(d)</code> have a shortcut symbol of <code>%s</code> and <code>%r</code> respectively, and can be used like below:</p>
<pre><code class="lang-Python">print(<span class="hljs-string">'Bruce Wayne is %s'</span> % <span class="hljs-string">'Batman'</span>) <span class="hljs-comment"># outputs: Bruce Wayne is Batman</span>
print(<span class="hljs-string">'Bruce Wayne is %r'</span> % <span class="hljs-string">'Batman'</span>) <span class="hljs-comment"># outputs: Bruce Wayne is 'Batman'</span>
</code></pre>
<p>Using that, replace <code>???</code> with <code>%r</code> and substitute <code>d</code> onto itself in line 2.</p>
<p>Python Input: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/P0XBVkEdRKgWxeQVFGXmlWikaKpzQVkKqgopmv//AwA">Try it online!</a></p>
<pre><code class="lang-Python">d = <span class="hljs-string">'d = %r\nprint(d)'</span>
print(d % d)
</code></pre>
<p>Output:</p>
<pre><code class="lang-Python">d = <span class="hljs-string">'d = %r\nprint(d)'</span>
print(d)
</code></pre>
<p>So close! We are just missing the <code>% d</code> in the second line.</p>
<h4 id="heading-step-4-1">Step 4</h4>
<p>Let's add that <code>% d</code> in line 1 of the source. But hold on <code>% d</code> would make Python look for a second value to be replaced. To tell Python not to, we have to escape the <code>%</code> symbol. This can be done by adding another <code>%</code> in front.</p>
<p>Python Input: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/P0XBVkEdRKgWxeQVFGXmlWikKKiqKqRoqnPBuUDe//8A">Try it online!</a></p>
<pre><code class="lang-Python">d = <span class="hljs-string">'d = %r\nprint(d %% d)'</span>
print(d % d)
</code></pre>
<p>Output:</p>
<pre><code class="lang-Python">d = <span class="hljs-string">'d = %r\nprint(d %% d)'</span>
print(d % d)
</code></pre>
<p>This is a valid Quine!</p>
<h4 id="heading-step-5-1">Step 5</h4>
<p>Now, to make it short. Let's get rid of the newline character and combine both lines using <code>;</code></p>
<pre><code class="lang-Python">d = <span class="hljs-string">'d = %r;print(d %% d)'</span>;print(d % d)
</code></pre>
<p>Variables can start with underscores. A single underscore is in and of itself a valid variable. Let's replace <code>d</code> with <code>_</code></p>
<pre><code class="lang-Python">_ = <span class="hljs-string">'_ = %r;print(_ %% _)'</span>;print(_ % _)
</code></pre>
<p>Get rid of all the spaces.</p>
<p>Python: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/P95WPd5Wtci6oCgzr0QjXlU1XlMdzonX/P8fAA">Try it online!</a></p>
<pre><code class="lang-Python">_=<span class="hljs-string">'_=%r;print(_%%_)'</span>;print(_%_)
</code></pre>
<p>There you have it. The shortest Quine.</p>
<p>Here's an interesting one: An Error-Quine in Python. The source code is an error-like text that throws the exact error when executed. The only difference being the output now goes to <em>stderr</em> instead of <em>stdout</em>.</p>
<p>Python: <a target="_blank" href="https://tio.run/##K6gsycjPM/7/X0HBLTMnVUEpOT8lVa8kM19JRyEnMy9VwZBLQQG/XByXZ15Kal5JIlAmz7WoKL/ISqE0L7WiIDW5JDVFIRMs@f8/AA">Try it online!</a></p>
<pre><code class="lang-python">  File <span class="hljs-string">"quine.py"</span>, line <span class="hljs-number">1</span>
    File <span class="hljs-string">"quine.py"</span>, line <span class="hljs-number">1</span>
    ^
IndentationError: unexpected indent
</code></pre>
<h2 id="heading-some-more-quines">Some more Quines</h2>
<p>Ruby: <a target="_blank" href="https://tio.run/##KypNqvz/v9hWqdhWtcC6oLSkWKFYVbVYCcYs5vr/HwA">Try it online!</a></p>
<pre><code class="lang-Ruby">s=<span class="hljs-string">"s=%p;puts s%%s"</span>;puts s%s
</code></pre>
<p>Lua: <a target="_blank" href="https://tio.run/##yylN/P@/2Fap2Fa1sKAoM69Eo9gqLb8oNxHI0NRUwhTi@v8fAA">Try it online!</a></p>
<pre><code class="lang-Lua">s="s=%qprint(s:format(s))"print(s:format(s))
</code></pre>
<p>Perl 5: <a target="_blank" href="https://tio.run/##K0gtyjH9/1@l2LZQA0yoFmtaFxRl5pWkAfk6KkAeOp/r/38A">Try it online!</a></p>
<pre><code class="lang-Perl">$s=<span class="hljs-string">q($s=q(%s)</span>;<span class="hljs-keyword">printf</span>($s,$s););<span class="hljs-keyword">printf</span>($s,$s);
</code></pre>
<p>JavaScript: <a target="_blank" href="https://tio.run/##y0osSyxOLsosKNHNy09J/f@/2Fap2FY1yzo5P684PydVLyc/XaNYp1hTCUOE6/9/AA">Try it online!</a></p>
<pre><code class="lang-JavaScript">s=<span class="hljs-string">"s=%j;console.log(s,s)"</span>;<span class="hljs-built_in">console</span>.log(s,s)
</code></pre>
<p>C: <a target="_blank" href="https://tio.run/##S9ZNT07@/z83MTNPo1izuqAoM68kTaPYVglDRDVZtVjVUCVZx9hEp1jTulYJxvj/HwA">Try it online!</a></p>
<pre><code class="lang-C">main(s){<span class="hljs-built_in">printf</span>(s=<span class="hljs-string">"main(s){printf(s=%c%s%1$c,34,s);}"</span>,<span class="hljs-number">34</span>,s);}
</code></pre>
<p>Java: <a target="_blank" href="https://tio.run/##y0osS9TNSsn@/z85J7G4WCGwuqA0KSczWaG4JLEESJXlZ6Yo5CZm5mkElxRl5qVHxyZqVkOYCsW2SuRoUk1WLVY1VEm2Dq4sLknN1csvLdErAMqVpGkU6xib6BRrWtfWKuGV5fr/HwA">Try it online!</a></p>
<pre><code class="lang-Java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Q</span></span>{<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[]a)</span></span>{String s=<span class="hljs-string">"class Q{public static void main(String[]a){String s=%c%s%1$c;System.out.printf(s,34,s);}}"</span>;System.out.printf(s,<span class="hljs-number">34</span>,s);}}
</code></pre>
<p>Rust: <a target="_blank" href="https://tio.run/##KyotLvn/Py1PITcxM09Ds7qgKDOvRFFDCSECE6o2sLKv1QGTmrW1SjqElWjW/v8PAA">Try it online!</a></p>
<pre><code class="lang-Rust"><span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>(){<span class="hljs-built_in">print!</span>(<span class="hljs-string">"fn main(){{print!({0:?},{0:?})}}"</span>,<span class="hljs-string">"fn main(){{print!({0:?},{0:?})}}"</span>)}
</code></pre>
<p>HQ9+: <a target="_blank" href="https://hci.mkps.app/">Try pasting it Online</a></p>
<p><a target="_blank" href="https://esolangs.org/wiki/HQ9%2B">Wiki page of HQ9+ language</a></p>
<pre><code class="lang-c">q
</code></pre>
<p>Quines are a fascinating topic and an art in their own right. We'll go more in-depth and explore topics like <a target="_blank" href="https://darshan.hashnode.dev/intron"><strong>Intron</strong></a>, <a target="_blank" href="https://darshan.hashnode.dev/quinerelay"><strong>QuineRelay</strong></a>, <a target="_blank" href="https://darshan.hashnode.dev/multiquine"><strong>MultiQuine</strong></a>, and much more in the follow-up posts.</p>
<p>Stay tuned!</p>
<hr />
<p>Sources and references:</p>
<ul>
<li><p><a target="_blank" href="https://en.wikipedia.org/wiki/Quine_\(computing\)">Quine (computing)</a>, wiki page.</p>
</li>
<li><p><a target="_blank" href="https://darshan.hashnode.dev/quine">Quine: Self replicating computer programs</a>, a self-referencing blog.</p>
</li>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=a-zEbokJAgY">Self-replicating Python code | Quine</a>, Youtube video by Lex Fridman.</p>
</li>
<li><p><a target="_blank" href="https://www.cs.cmu.edu/~rdriley/487/papers/Thompson_1984_ReflectionsonTrustingTrust.pdf">Reflections on Trusting Trust</a>, paper by Ken Thompson.</p>
</li>
<li><p><a target="_blank" href="https://www.goodreads.com/book/show/24113.G_del_Escher_Bach">Gödel, Escher, Bach: An Eternal Golden Braid</a>, book by Douglas R. Hofstadter.</p>
</li>
<li><p><a target="_blank" href="http://www.madore.org/~david/computers/quine.html">Quines (self-replicating programs)</a>, blog by David Madore.</p>
</li>
<li><p><a target="_blank" href="https://www.nyx.net/~gthompso/quine.htm">The Quine Page</a>, blog by Gary P. Thompson II.</p>
</li>
<li><p><a target="_blank" href="https://cs.lmu.edu/~ray/notes/quineprograms/">Quine Programs</a>, blog by Ray Toal.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Leap days, and the quest for the perfect Calendar.]]></title><description><![CDATA[The story of calendars is a cosmic interplay between astronomy, religion, and history.
And, if you think your schedule is chaotic, just be glad you weren't around when the Romans were messing with time.
Humans have long relied on the cycle of Day and...]]></description><link>https://blog.darshanas.dev/history-of-calendars</link><guid isPermaLink="true">https://blog.darshanas.dev/history-of-calendars</guid><category><![CDATA[leapday]]></category><category><![CDATA[calendar]]></category><category><![CDATA[calendars]]></category><category><![CDATA[leap_year]]></category><dc:creator><![CDATA[Darshan A S]]></dc:creator><pubDate>Sat, 20 Apr 2024 09:55:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1713561743439/fc89b971-78e4-4346-b2ef-376f1ce83c4d.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The story of calendars is a cosmic interplay between astronomy, religion, and history.
And, if you think your schedule is chaotic, just be glad you weren't around when the Romans were messing with time.</p>
<p>Humans have long relied on the cycle of Day and Night for hunting, gathering, and finding their way home after one too many fermented berries.</p>
<p>But our ancestors needed more than just day and night; they needed to predict the fickle seasons for planting, harvesting, and knowing when to throw those wild solstice parties. In short, they needed a calendar!</p>
<p>Enter the Romans!
After all, we all think about the Roman Empire at least once a day, right? RIGHT?</p>
<h3 id="heading-lunar-calendar">Lunar calendar</h3>
<p>Anywho, conveniently enough, like the cycle of day and night, nature had given us yet another cyclical clock in the sky. The Moon! and its phases. The moon, waxing and waning like clockwork every 29.5 days seemed like a natural way to track time. That's how the word Month (literally "Moon-th") originated.</p>
<p>Their first king, <a target="_blank" href="https://en.wikipedia.org/wiki/Romulus">Romulus</a>, established a lunar calendar, based on the same idea. It consisted of 10 months, starting with spring (Martius, now March) and ending with the onset of winter (December). The system is usually said to have left the remaining days before the next Spring as an unorganized "winter" since they were irrelevant to the farming cycle.</p>
<p>They began by naming months after their gods. but by the fifth month, they threw their hands up and just named them after the good old Latin numbers.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>No.</td><td>Months</td><td>Meaning</td><td>Length in days</td></tr>
</thead>
<tbody>
<tr>
<td>1</td><td>Martius</td><td>Month of <em>Mars</em>, the Roman god of war.</td><td>31</td></tr>
<tr>
<td>2</td><td>Aprilis</td><td>Month of goddess <em>Apru</em>. (Aphrodite)</td><td>30</td></tr>
<tr>
<td>3</td><td>Maius</td><td>Month of <em>Maia</em>, a Roman goddess of growth and fertility.</td><td>31</td></tr>
<tr>
<td>4</td><td>Junius</td><td>Month of <em>Juno</em>, the Roman goddess of marriage and queen of the gods.</td><td>30</td></tr>
<tr>
<td>5</td><td>Quintilis</td><td>The Latin word <em>quintus</em> meaning "fifth.</td><td>31</td></tr>
<tr>
<td>6</td><td>Sextilis</td><td>The Latin word <em>sextus</em> meaning "sixth".</td><td>30</td></tr>
<tr>
<td>7</td><td>September</td><td>The Latin word <em>septem</em> meaning "seven".</td><td>30</td></tr>
<tr>
<td>8</td><td>October</td><td>The Latin word <em>octo</em> meaning "eight".</td><td>31</td></tr>
<tr>
<td>9</td><td>November</td><td>The Latin word <em>novem</em> meaning "nine".</td><td>30</td></tr>
<tr>
<td>10</td><td>December</td><td>The Latin word <em>decem</em> meaning "ten".</td><td>30</td></tr>
<tr>
<td></td><td></td><td>Length of the year</td><td>304</td></tr>
</tbody>
</table>
</div><h3 id="heading-lunisolar-calendar">Lunisolar calendar</h3>
<p>Problem was, predicting the end of that chaotic "winter" period was about as easy as herding cats.</p>
<p>So <a target="_blank" href="https://en.wikipedia.org/wiki/Numa_Pompilius">Numa Pompilius</a>, the second king of Rome, tried something.
He reasoned that roughly 12 moon (lunar) cycles occurred between springs (solar), giving a year <code>12 * 29.5 = 354</code> days. But hold on! Even-numbers were considered bad luck in ancient Rome, so Numa tacked an extra day onto the year, making the target length, a peculiar 355 days long. Then, to appease the odd-number-loving gods, he removed a day from the 30-day months, leaving behind <code>304 - 6 = 298</code> days.</p>
<p>With 12 moon cycles to cover, Numa still had <code>355 - 298 = 57</code> days left over. He split these into two new months: one with 29 days and another with the dreaded even number of 28. The first, named <em>Januarius</em> after <em>Janus</em>, the god of beginnings and transitions, was strategically placed at the start of the year. As for the other month, stuck with its even-numbered fate, it was dedicated to spiritual purification and shoved to the end. They called it <em>Februarius</em>, derived from the Latin word "<em>Februa</em>" meaning "purification."</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>No.</td><td>Months</td><td>Length in days</td></tr>
</thead>
<tbody>
<tr>
<td>1</td><td>Januarius</td><td>29</td></tr>
<tr>
<td>2</td><td>Martius</td><td>31</td></tr>
<tr>
<td>3</td><td>Aprilis</td><td>29</td></tr>
<tr>
<td>4</td><td>Maius</td><td>31</td></tr>
<tr>
<td>5</td><td>Junius</td><td>29</td></tr>
<tr>
<td>6</td><td>Quintilis</td><td>31</td></tr>
<tr>
<td>7</td><td>Sextilis</td><td>29</td></tr>
<tr>
<td>8</td><td>September</td><td>29</td></tr>
<tr>
<td>9</td><td>October</td><td>31</td></tr>
<tr>
<td>10</td><td>November</td><td>29</td></tr>
<tr>
<td>11</td><td>December</td><td>29</td></tr>
<tr>
<td>12</td><td>Februarius</td><td>28</td></tr>
<tr>
<td></td><td>Length of the year</td><td>355</td></tr>
</tbody>
</table>
</div><p>But, each year fell short of a tropical year by a few days, causing the seasons to drift like a ship without a rudder. After some head-scratching and sky-gazing, the Romans realized their calendar was off by a whopping 45 days every four years!</p>
<p>So Numa decided to throw in an extra, intercalary month, called <em>Mercedonius</em>, every other year. This bonus month alternated between 22 and 23 days. To make things tidier, and since Feb 23rd was <a target="_blank" href="https://en.wikipedia.org/wiki/Terminalia_(festival)">Terminalia</a> (festival of God Terminus, who presided over boundaries), Numa ended Feb on 23rd and moved the remaining 5 days to the intercalating month to make them 27 and 28 days long. And at the same time, Jan and Feb both were moved to the beginning of the year.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>No.</td><td>Months</td><td>Year 1</td><td>Year 2</td><td>Year 3</td><td>Year 4</td></tr>
</thead>
<tbody>
<tr>
<td>1</td><td>Januarius</td><td>29</td><td>29</td><td>29</td><td>29</td></tr>
<tr>
<td>2</td><td>Februarius</td><td>28</td><td>23</td><td>28</td><td>23</td></tr>
<tr>
<td></td><td>Mercedonius</td><td>0</td><td>27</td><td>0</td><td>28</td></tr>
<tr>
<td>3</td><td>Martius</td><td>31</td><td>31</td><td>31</td><td>31</td></tr>
<tr>
<td>4</td><td>Aprilis</td><td>29</td><td>29</td><td>29</td><td>29</td></tr>
<tr>
<td>...</td><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td></tr>
<tr>
<td>12</td><td>December</td><td>29</td><td>29</td><td>29</td><td>29</td></tr>
<tr>
<td></td><td>Length of the year</td><td>355</td><td>377</td><td>355</td><td>378</td></tr>
</tbody>
</table>
</div><p>Essentially making the average of all 4 years into <code>(355 + 377 + 355 + 378) / 4 = 366.25</code> This jumbled mess of intercalary months and shifting start dates became known as the Roman Republican Calendar. It was a far cry from perfect, but hey, it was a start.</p>
<p>This is still too many days, but it could have worked. Every 19 solar years would line up with 235 lunar months. Add enough leap month and eventually everything will reset itself. The seasons will be back on track.</p>
<p>But the leap months weren't added as planned. Politicians would get them to extend leap month when it's their office and to skip the month when it's their opponents. Also, it was a custom that intercalation had to be personally announced by the chief pontiff in Rome. So, during wars, when the king was out of the city, the leap month was forgotten for years.</p>
<h3 id="heading-solar-calendar">Solar calendar</h3>
<p>By the time <a target="_blank" href="https://en.wikipedia.org/wiki/Julius_Caesar">Julius Caesar</a> came to power, things were confusing. He had spent a long time in Egypt, where 365-day calendars were all the rage. So in 46 BC he threw the lunisolar calendar away and ordered his scholars to help design a solar calendar. He got rid of the intercalary month of <em>Mercedonius</em>, leaving the original 355 days long year. Caesar then sprinkled the missing 10 days across various months, reaching a total of 365.</p>
<p>The year 46 BC was the last of the old system and included three intercalary months, the first inserted in February and two more—<strong>Intercalaris Prior</strong> and <strong>Posterior</strong>—before December, coincidentally making the year of Caesar's third consulship last for 446 days!</p>
<p>But wait! By now, astronomers knew the tropical year was slightly longer—closer to 365.25 (365 days and 6 hours). So, Caesar added a leap day every four years. However, in a move that would baffle future generations, he inserted it after February 23rd, by doubling 24th, right smack in between the month – there were indeed <a target="_blank" href="https://en.wikipedia.org/wiki/Bissextus">two days dated 24 February</a>!</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>No.</td><td>Months</td><td>Length in days</td></tr>
</thead>
<tbody>
<tr>
<td>1</td><td>January</td><td>31</td></tr>
<tr>
<td>2</td><td>February</td><td>28 + (1 every 4 years)</td></tr>
<tr>
<td>3</td><td>March</td><td>31</td></tr>
<tr>
<td>4</td><td>April</td><td>30</td></tr>
<tr>
<td>5</td><td>May</td><td>31</td></tr>
<tr>
<td>6</td><td>June</td><td>30</td></tr>
<tr>
<td>7</td><td>July</td><td>31</td></tr>
<tr>
<td>8</td><td>August</td><td>31</td></tr>
<tr>
<td>9</td><td>September</td><td>30</td></tr>
<tr>
<td>10</td><td>October</td><td>31</td></tr>
<tr>
<td>11</td><td>November</td><td>30</td></tr>
<tr>
<td>12</td><td>December</td><td>31</td></tr>
<tr>
<td></td><td>Length of the year</td><td>365 + (1 every 4 years)</td></tr>
</tbody>
</table>
</div><p>Following Caesar's assassination, the priests mistakenly added the <a target="_blank" href="https://en.wikipedia.org/wiki/Bissextile">bissextile</a> leap day every three years due to their inclusive counting. To bring the calendar back to its proper place, <a target="_blank" href="https://en.wikipedia.org/wiki/Augustus">Augustus</a>, the next King, was obliged to suspend intercalation for one or two decades.</p>
<p>As a tribute to their egos (ahem, I mean contributions), the 7th and 8th months were renamed Julius (for Caesar, obviously) and Augustus (for his successor). </p>
<h3 id="heading-a-better-solar-calendar">A better solar calendar</h3>
<p>Unfortunately, the Julian calendar wasn't flawless. It assumed the average solar year was exactly 365.25 days long, overestimating by about 14 minutes. By the year 1582, the excess leap days introduced by the Julian algorithm had caused the calendar to drift such that the <a target="_blank" href="https://en.wikipedia.org/wiki/March_equinox">March equinox</a> was occurring well before its nominal 21 March date. This date was important to the Christian churches because it is fundamental to the calculation of the date of Easter.</p>
<p>To reinstate the association, the then Pope, <a target="_blank" href="https://en.wikipedia.org/wiki/Pope_Gregory_XIII">Pope Gregory XIII</a>, did something crazy to the year 1582. He skipped 10 whole days from the calendar! Thursday, October 4th was followed immediately by Friday, October 15th! Good luck negotiating the rent, I guess?</p>
<p><a target="_blank" href="https://www.timeanddate.com/calendar/custom.html?year=1582&amp;country=13&amp;typ=0&amp;display=0&amp;cols=4&amp;lang=en&amp;cdt=31&amp;holm=1&amp;df=1"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1713560284351/8a1f0458-4a5a-4dc4-bf62-50cc778e1962.png" alt="Calendar of the year 1582 at Italy." class="image--center mx-auto" /></a></p>
<p>To ensure that the drift doesn't happen again, Gregory introduced a new calendar, aptly named the Gregorian calendar.
By this time, it was known that a solar year was close to 365.24 days (That's 365 days, 5 hours, 45 minutes, and 36 seconds; for those still keeping track), while the Julian year was 365.25 days. This meant every <code>1 / (365.25 - 365.24) = 100</code> years an extra day has been accumulated. So, the Gregorian calendar chose to skip adding a leap day every centennial year (those divisible by 100)</p>
<p>But, the solar year was a smidge longer, around 365.2425 days long. (365 days, 5 hours, 49 minutes, and 12 seconds) Which meant every <code>1/ (365.2425 - 365.24) = 400</code> years a day has been lost. So, the Gregorian calendar chose to add back a leap day every centennial year divisible by 400.</p>
<p>This is the calendar we all know (and love?) today. Every year that is exactly divisible by 4, you add a leap day; except for years that are exactly divisible by 100, then you remove the leap day that you would have otherwise added; except if it is divisible by 400, you add back the leap day!</p>
<p>It took 170 more years, for Britain and its colonies, including India, to follow suit. They skipped 11 days, wherein, Wednesday, 1752, September 2nd was followed by Thursday, September 14.</p>
<p><a target="_blank" href="https://www.timeanddate.com/calendar/custom.html?year=1752&amp;country=35&amp;display=0&amp;cols=4&amp;cdt=31&amp;holm=1&amp;df=1"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1713560442212/021584be-8d8e-41b5-a66c-bdf9a65dfad1.png" alt="Calendar of the year 1752 at India." class="image--center mx-auto" /></a></p>
<p>In fact, the world is still adopting the Gregorian. Saudi Arabia, for instance, jumped on the Gregorian wagon as late as 2016!</p>
<h3 id="heading-future">Future</h3>
<p>So, we finally nailed it right? Close, but not quite.</p>
<p>Turns out, the solar year is even shorter than the Gregorian calendar accounts for. The latest measurements put it at a mind-bogglingly precise 365.2421897 days. (365 days, 5 hours, 48 minutes, 45 seconds, 19.008 mili-seconds). How long does this take to accumulate an extra day?  <code>1 / (365.2425 - 365.2421897) ~= 3223</code> years!</p>
<p>Following the pattern, it seems logical to add back a leap day every 4,000 years, averaging out to 365.24225 days. This would make the calendar drift by a day only once every <code>1 / (365.24225 - 365.2421897) ~= 16584</code> years! So, why aren't we doing it?</p>
<p>Well, for starters, as we saw with Gregorian, getting the entire world to adopt a new calendar is nowhere close to easy. But there's a more fundamental reason: it wouldn't be worth the effort.</p>
<p>The solar year—the time it takes Earth to orbit the sun—is gradually increasing, as is the length of a day. Currently, the length of the year is changing in the sixth decimal place over a person's lifetime. Various cosmic forces are at play here, deserving a blog post in their own right. Any correction based on today's solar year length would become obsolete in a few millennia, requiring yet another adjustment anyway.</p>
<p>So, for now, we're stuck with the Gregorian calendar, a marvel of human ingenuity with its quirks and imperfections. What makes this feat even more remarkable is that telescopes weren't even invented when these calendars were designed! All of this was achieved with the naked eye, by observing the shadows cast by sticks and gazing up at the celestial dance above.</p>
<p>As we navigate the complexities of our modern world, perhaps we can take a cue from the ever-shifting cosmos and embrace the fluidity of time itself. After all, isn't life a little more adventurous when we're not always running on schedule?</p>
<h3 id="heading-bonus">Bonus</h3>
<ul>
<li>Use <a target="_blank" href="https://www.timeanddate.com/calendar/?year=2024&amp;country=35">timeanddate.com</a> to look at calendars of any year in any given country.</li>
<li>Try finding the 10-day skip in October of 1582 in Italy!</li>
<li>Try finding the 11-day skip in September of 1752 in India!</li>
<li>Look at the <a target="_blank" href="https://en.wikipedia.org/wiki/List_of_adoption_dates_of_the_Gregorian_calendar_by_country">Adoption dates of Gregorian Calendar by Country</a> table, to find when a given country adopted the Gregorian and how many days they skipped to synchronize.</li>
<li>Which country skipped the most days? Check out the same in the timeanddate tool above.</li>
</ul>
<p>Sources and references:</p>
<ul>
<li><a target="_blank" href="https://en.wikipedia.org/wiki/Roman_calendar">Roman Calendar</a>, wiki page.</li>
<li><a target="_blank" href="https://en.wikipedia.org/wiki/List_of_adoption_dates_of_the_Gregorian_calendar_by_country">Adoption dates of the Gregorian Calendar by country</a>, wiki page.</li>
<li><a target="_blank" href="https://youtu.be/IJhgZBn-LHg?si=fR0jHkk3K79RhlPF">How Earth Moves</a>, video by VSauce.</li>
<li><a target="_blank" href="https://www.grc.nasa.gov/www/k-12/Numbers/Math/Mathematical_Thinking/calendar_calculations.htm#:~:text=The%20tropical%20year%20is%20the,46%20seconds%2C%20or%20365.2422%20days.">Calendar calculations</a>,  by NASA.</li>
<li><a target="_blank" href="https://youtu.be/AgKaHTh-_Gs?si=eFmQgqjmJP_oEHlO">Why Does February Only Have 28 Days?</a>, video by "Be Smart".</li>
<li><a target="_blank" href="https://youtu.be/WkhrL8NMycU?si=BSD1brY2OTGxK_jz">Neil deGrasse Tyson Explains Why We Have Leap Days</a>, video by StarTalk.</li>
</ul>
]]></content:encoded></item></channel></rss>