234
\$\begingroup\$

Using your language of choice, golf a quine.

A quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output.

No cheating -- that means that you can't just read the source file and print it. Also, in many languages, an empty file is also a quine: that isn't considered a legit quine either.

No error quines -- there is already a separate challenge for error quines.

Points for:

  • Smallest code (in bytes)
  • Most obfuscated/obscure solution
  • Using esoteric/obscure languages
  • Successfully using languages that are difficult to golf in

The following Stack Snippet can be used to get a quick view of the current score in each language, and thus to know which languages have existing answers and what sort of target you have to beat:

var QUESTION_ID=69;
var OVERRIDE_USER=98;

var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";var COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk";var answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;function answersUrl(index){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}
function commentUrl(index,answers){return"https://api.stackexchange.com/2.2/answers/"+answers.join(';')+"/comments?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}
function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(data){answers.push.apply(answers,data.items);answers_hash=[];answer_ids=[];data.items.forEach(function(a){a.comments=[];var id=+a.share_link.match(/\d+/);answer_ids.push(id);answers_hash[id]=a});if(!data.has_more)more_answers=!1;comment_page=1;getComments()}})}
function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(data){data.items.forEach(function(c){if(c.owner.user_id===OVERRIDE_USER)
answers_hash[c.post_id].comments.push(c)});if(data.has_more)getComments();else if(more_answers)getAnswers();else process()}})}
getAnswers();var SCORE_REG=(function(){var headerTag=String.raw `h\d`
var score=String.raw `\-?\d+\.?\d*`
var normalText=String.raw `[^\n<>]*`
var strikethrough=String.raw `<s>${normalText}</s>|<strike>${normalText}</strike>|<del>${normalText}</del>`
var noDigitText=String.raw `[^\n\d<>]*`
var htmlTag=String.raw `<[^\n<>]+>`
return new RegExp(String.raw `<${headerTag}>`+String.raw `\s*([^\n,]*[^\s,]),.*?`+String.raw `(${score})`+String.raw `(?=`+String.raw `${noDigitText}`+String.raw `(?:(?:${strikethrough}|${htmlTag})${noDigitText})*`+String.raw `</${headerTag}>`+String.raw `)`)})();var OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(a){return a.owner.display_name}
function process(){var valid=[];answers.forEach(function(a){var body=a.body;a.comments.forEach(function(c){if(OVERRIDE_REG.test(c.body))
body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)
valid.push({user:getAuthorName(a),size:+match[2],language:match[1],link:a.share_link,})});valid.sort(function(a,b){var aB=a.size,bB=b.size;return aB-bB});var languages={};var place=1;var lastSize=null;var lastPlace=1;valid.forEach(function(a){if(a.size!=lastSize)
lastPlace=place;lastSize=a.size;++place;var answer=jQuery("#answer-template").html();answer=answer.replace("{{PLACE}}",lastPlace+".").replace("{{NAME}}",a.user).replace("{{LANGUAGE}}",a.language).replace("{{SIZE}}",a.size).replace("{{LINK}}",a.link);answer=jQuery(answer);jQuery("#answers").append(answer);var lang=a.language;lang=jQuery('<i>'+a.language+'</i>').text().toLowerCase();languages[lang]=languages[lang]||{lang:a.language,user:a.user,size:a.size,link:a.link,uniq:lang}});var langs=[];for(var lang in languages)
if(languages.hasOwnProperty(lang))
langs.push(languages[lang]);langs.sort(function(a,b){if(a.uniq>b.uniq)return 1;if(a.uniq<b.uniq)return-1;return 0});for(var i=0;i<langs.length;++i)
{var language=jQuery("#language-template").html();var lang=langs[i];language=language.replace("{{LANGUAGE}}",lang.lang).replace("{{NAME}}",lang.user).replace("{{SIZE}}",lang.size).replace("{{LINK}}",lang.link);language=jQuery(language);jQuery("#languages").append(language)}}
body{text-align:left!important}#answer-list{padding:10px;float:left}#language-list{padding:10px;float:left}table thead{font-weight:700}table td{padding:5px}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/codegolf/primary.css?v=f52df912b654"> <div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> 

\$\endgroup\$
3
  • 4
    \$\begingroup\$ Do you not mean, "Golf you a quine for greater good!"? \$\endgroup\$ May 3, 2011 at 2:49
  • 62
    \$\begingroup\$ @muntoo it's a play on "Learn you a Haskell for Great Good". \$\endgroup\$ May 3, 2011 at 2:52
  • 18
    \$\begingroup\$ Did anybody notice that this is question 69? \$\endgroup\$
    – aidan0626
    Oct 24, 2020 at 22:47

417 Answers 417

1
4 5
6
7 8
14
4
\$\begingroup\$

TeaScript, 1 byte

1

Nothing too interesting. But if that's too boring...

TeaScript, 3 bytes

[q|

and if that's to boring...

TeaScript, 15 bytes

(ƒ`(${f})()`)()
\$\endgroup\$
4
\$\begingroup\$

Python, 104 bytes

Uses eval and repr

a = "print 'a','=', repr(eval('a'))\nprint eval('a'),\n"
print 'a','=', repr(eval('a'))
print eval('a'),
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Since this is code-golf, you might want to include a byte count. \$\endgroup\$
    – nyuszika7h
    Apr 28, 2014 at 12:33
  • 1
    \$\begingroup\$ repr(x)==`x` \$\endgroup\$ Mar 10, 2016 at 23:58
4
\$\begingroup\$

JavaScript, 58 54 bytes

I present to you the shortest non-source-reading quine in JavaScript:

console.log(a="console.log(a=%s,uneval(a))",uneval(a))

How have I not thought of this before? Screw that, how has nobody thought of this before? :P

Here's a version that works in all browsers at the cost of 9 bytes:

q='"';console.log(a="q='%s';console.log(a=%s,q,q+a+q)",q,q+a+q)
\$\endgroup\$
4
  • \$\begingroup\$ If this works in the browser, you can make console.log be alert. \$\endgroup\$ Sep 7, 2016 at 1:56
  • 1
    \$\begingroup\$ @ConorO'Brien Nope. The %s in the string only works with console.log. \$\endgroup\$ Sep 7, 2016 at 2:00
  • \$\begingroup\$ ahhh that's what that was. \$\endgroup\$ Sep 7, 2016 at 11:02
  • \$\begingroup\$ Lolp I was trying to do that a while ago, I couldn't figure out string formatting \$\endgroup\$
    – Oliver Ni
    Oct 23, 2016 at 5:26
4
\$\begingroup\$

BotEngine, 4x1=4 bytes

TRUE

The T instruction deletes the active bot and prints TRUE.

\$\endgroup\$
4
\$\begingroup\$

Befunge-98 (cfunge), 8 characters

 'k<@,k␇

represents a literal BEL character (ASCII 7, or Ctrl-G). (Note also that the program starts with a leading space.)

Note that the k command, which is heavily used here, is somewhat imprecisely defined, and this code is outright exploiting several edge cases at once, making this an example of corner-case code. As such, this is somewhat interpreter-dependent; it doesn't work on TIO, for example. cfunge is the Befunge-98 interpreter I normally use locally (and has been tested to be highly conformant with the specification), and it handles this code correctly. (Update: I've been talking to some Befunge experts about this quine, and the consensus is that it's exploiting a bug in cfunge, not behaviour that's defensible by the specification. Still a valid answer, though, because languages are defined by their implementation and this is the sort of corner case that has no right answers, only wrong answers.)

This program would also work in Unefunge-98 and Trefunge-98, but I'm not sure if any of the pre-existing interpreters for those handle k in the way we need, so it may be noncompeting in those languages.

Verification

$ xxd /tmp/quine.b98
00000000: 2027 6b3c 402c 6b07                       'k<@,k.
$ ./cfunge /tmp/quine.b98 | xxd
00000000: 2027 6b3c 402c 6b07                       'k<@,k.

Explanation

General principles

We know that in fungeoids, it's normally easiest to wrap a string around the code, so that the code is inside and outside the string literal at the same time. However, another trick for shortening quines is to use a string representation which doesn't need escaping, so that we don't need to spend bytes to represent the string delimiter itself. So I decided to see if these techniques could be combined.

Befunge-98 normally uses " as a string delimiter. However, you can also capture a single character using ', and you can make any command into a sort of lightweight loop (in a confusing and buggy way) using k. As such, k' functions as a sort of makeshift length-prefixed string literal. And of course, a length-prefixed string literal has no problems in escaping its own delimiter, as it doesn't have any sort of string terminator at all, meaning that the entire range of octets (in fact, the entire range of cell values) are available to exist within the string.

We can actually do even better; we no longer have to stop the string at its opening delimiter (we can stop it anywhere), so we can wrap it multiple times around the program to grab not only the k' itself, but also the length of the string (which is in this case written as a character code, thus the literal backspace). The program will continue execution just after the end of the string, i.e. just after the last character captured, which is exactly where we want it. (Bear in mind that Befunge strings are printed in reverse order to pushing them; the most common form, NUL-terminated strings, are called "0gnirts" by the community because of this, and length-prefixed strings follow the same principle. Thus if we want the length to end up at the start of the string, we have to push it last.)

As an extra bonus, this also means that we can wrap multiple times around the program with no penalty; all that matters is that the last character we see is the string length (which is at the end of the program). By an amazing stroke of luck, k' specifies length-prefixed string (sort-of; k is weird), and 'k (the same two characters in reverse order) pushes 107, which happens to loop round the program multiple times and end up in exactly the right place (this only had a 1 in 8 chance of working out). Because we have to reverse the program direction anyway (to read the string in the reverse of the natural reading order, meaning that it gets printed in the same order it appeared in the original program), we can use the same two characters for both pushing the length, and pushing the string itself, at no cost.

Of course, this now captures a risk of counting as a literal-only program, and thus not a proper quine under PPCG rules. Luckily, wrapping round from one end of the program to the other produces a literal space character, and spaces at the ends of the line (i.e. leading and trailing whitespace) aren't captured as part of a string. Thus, if we start the program with a space, we can encode that space (which isn't part of the string literal) via the implicit space that we get from wrapping the program (i.e. the leading space is encoded by the ' next to it, rather than by itself), just sneaking within the proper quine rules. The easiest way to see this is to delete the leading space from the program; you'll get the same output as the program with the leading space (thus effectively proving that it doesn't encode itself, because even if you remove it it still gets printed).

Detailed description

 'k<@,k␇
 'k       Push 107 to the stack
   <      Set execution delta to leftwards
 'k       Push the next 107 characters to the stack: "'␠␇k, … @<ck'␠␇"
     ,k   Pop a length from the stack, output that many characters
     ,    Output the top stack element
    @     Exit the program

You can note that k has some odd ideas of where to start reading the string from (for the first k that runs), or where to leave the IP afterwards (for the second k that runs); this is just the way k happens to work (you think of k as taking an "argument", the command to run, but it doesn't actually move the IP to skip the "argument"; so if the command inside the loop doesn't affect the IP or the IP's movement, it'll end up being the next command that runs and the loop runs one more time). The literal BEL, ASCII 7, is interpreted by the second k as a loop counter, so the , inside the k will print the first 7 characters, then the , outside the k (which is the same character in the source) will print the 8th just before the program exits.

\$\endgroup\$
4
\$\begingroup\$

Turtlèd, 68 63 53 54 bytes (newlines)

rewrote

@##'@r,r,r-{ +.r_}r{ +.r_}'#'@r,r,r-{ +.r_}r{ +.r_}'f

Try it online!

Explanation

@#         Set character variable to #. Useful because this cannot occur in string var     
  #                        #  Set the string var to...
   '@r,r,r-{ +.r_}r{ +.r_}'   This string (quotes included. coincidental)
                            '@r,r,r            Write out first three chars of program: @##
                                   -{ +.r_}r   Write out the string var, then #, move right
                                            { +.r_} Write out the string var again, write #
                                                   'f   overwrite last # with f
\$\endgroup\$
4
\$\begingroup\$

Alice, 45 bytes

Credit to Martin Ender for the use of %, r, and y to obtain the characters "/\ without escaping.

/?.!eO%?.*y1?@~mtz!!4\
\"Y!Z1hrZRoY@*m*h%1Y{/

Try it online!

This program runs entirely in ordinal mode. Because of how ordinal mode programs need to be formatted, this is significantly longer than Martin Ender's cardinal mode quine.

In ordinal mode, the instruction pointer moves diagonally, and commands work on strings instead of integers. The diagonal movement is what makes this tricky: there is even a challenge specifically about formatting a program for ordinal mode. While it's possible to sidestep the entire issue by putting the same string on both lines, this approach ends up slightly longer at 52 bytes.

\".!e1%r.Ryh?*.Ooo1m@z1!{
\".!e1%r.Ryh?*.Ooo1m@z1!{/

Try it online!

Explanation

This is a standard template for ordinal mode, with an additional mirror to allow the program to loop back to the beginning. Linearized, the code is as follows:

".!e1%r.RyY?*~*t%!Y4?Y!ZOh?Z*o1@@mmhz1!{

As with many Fungeoid quines, the " wraps around to itself and puts this entire program in a string literal. Since string mode treats mirrors as mirrors (instead of literals), the string that gets pushed is exactly the linearized code, excluding the ".

.!     Duplicate the string, and move the copy to tape
e1%    Split on "1", placing "@@mmhz" and "!{" on top of the stack.
       The other two parts are irrelevant.
r      Expand !{ into the entire range from code point 33 to 123.
.R     Duplicate and reverse this range
y      Modify the string @@mmhz by changing every character in the range 33-123 
       with the corresponding character in the reversed range.
       The result of this transformation is \\//4" .
       This allows us to get these characters without escaping them.
Y?*~*  Split this string in half by unzipping, and put the halves on either
       side of the original string.  The new string is \/"sourcecode\/4 .
t%     Extract the newly added 4 at the end, and use it to split on the single 4 in the code.

At this point, we have two strings corresponding to approximately half of the code. The top of the stack has the second half of the program and the right side mirrors, and corresponds to these output bytes:

 ? ! O ? * 1 @ m z ! \
  Y Z h Z o @ m h 1 {/

The string below that has the first half of the program, along with the left side mirrors and quote:

/ . e % . y ? ~ t !
\" ! 1 r R Y * * % Y

Neither string currently contains the 4 that was used to split the string.

!      Move second half string to the tape.
Y      Unzip first half: the top of the stack now contains the characters
       from the first half that will end up in the first row of the output.
4      Append the digit 4 to this string.
?Y     Copy second half back from tape and unzip: the top of the stack contains
       characters from the second half that will end up in the second row
!      Move this onto the tape.
Z      Zip the two halves of the first row together.
O      Output this with a linefeed.
h      Temporarily remove the initial \ so the next zip will work right.
?Z     Copy the string back from the tape, and zip the second row together.
       This Z isn't the exact inverse of Y since the second half is longer.
       The resulting behavior is exactly what we want.
*o     Join with the previously removed \ and output.
1      Append 1 to the irrelevant string on the top of the stack.
@      Terminate.

The 52-byte quine works on exactly the same principle, except that it doesn't need the ordinal formatting section of the 45-byte quine.

\$\endgroup\$
4
\$\begingroup\$

Awk, 64 bytes

BEGIN{c="BEGIN{c=%c%s%c;printf c,34,c,34}";printf c,34,c,34}
\$\endgroup\$
4
\$\begingroup\$

Operation Flashpoint scripting language,  22  15 bytes

q={"q={"+q+"}"}

Call with:

hint call q

Output:

q={"q={"+q+"}"}

Old version (22 bytes):

q={format["q={%1}",q]}
\$\endgroup\$
4
\$\begingroup\$

JavaScript, 53 bytes

A quine without any uses of uneval or Function.prototype.toString.

Q="q=Q.link()[8];alert(`Q=${q+Q+q};eval(Q)`)";eval(Q)

Can avoid use of template strings for + 1 byte.

Q="q=Q.link()[8];alert('Q='+q+Q+q+';eval(Q)')";eval(Q)


JavaScript, 74 bytes

Another approach avoiding uneval and Function.prototype.toString:

console.log(a="console.log(a=%s%s%s,q=a.link()[8],a,q)",q=a.link()[8],a,q)

\$\endgroup\$
4
+400
\$\begingroup\$

Pain-Flak, 2009 bytes

><))(())(())()(())(())()(())(())(())(())()()(())()(())(())()()()(())(())()(())(())()()(())(())(())()(())(())()()(())(())(())()(())(())()(())(())()(())(())()(())()(())()(())()(())()(())(())(())()()(())()()(())(())(())()()(())(())(())()()()(())(())(())()()()(())(())()()(())()(())()()(())(())()()()(())(())(())(())(())(())()(())()()()(())()(())()()(())(())()()()(())(())(())()()()(())(())()()(())()(())(())()()(())()()(())(())(())()()(())(())(())(())(())(())()()()(())(())(())()()(())(())(())(())()()(())()()(())()()()(())(())()()(())()(())(())()()(())(())(())(())(())(())()()(())()()()(())(())()(())(())()()(())()(())()()(())()()()(())(())()()(())(())(())(())(())(())()()(())()()()(())()(())(())()(())()()(())(())()(())()()()()(())()(())(())(())(())(())(())(())(())()(())(())(())()()(())()(())(())(())(())(())()()()(())()(())(())()()(())(())(())()()(())()(())(())(())()()(())()(())()(())(())(())(())()()(())()(())()()()()(())()(())()(())()()()(())(())(())()(())(())()()(())(())(())()()(())()(())(())(())()()(())()(())()(())(())(())()()(())(())(())()(())(())()(())(())()(())(())()(())(())()(())()(())()(())()(())()()()(())(())()(())(())()()(())()(())()(())()()()(())()(())(())(())()(())()()()()(())()()(())(())()(())()(())(())(())(())()()(())(())(())()()()(())(())(())()()()(())(())()()(())()(())()()(())(())()()()(())(())(())()()()(())(())(())(())()(())(())(())(())()(())()()()()(())(())()()(())()(())()(())()(())(())()()()(())(())(())()(())(())()()(())()(())()()(())()()()(())(())()(())(())(())(())()()(())()()()()(())()(())()(())(())()()()(())(())(())()(())(())(())()()(())(())(())()()(())(())(())()()(())(())(())()(())(())()(())(())()(())(())()(())(())()(())()(())()(())()(())()(())()(())(())()()()(())()()((}><))))))()()()()((}{(}{(}{()((><))]}{[()(>})}{)((><)))}{])([()(((><{><})}{><(><{}{<())(}])([)>))}{)(>))))()()()()((}{())}{()}{(}{)((>))])}{([))}{()}{(}{)><((()}{()((<(<(()])(})()>}{<({[}{>})}{)(>}{<({<}{)}{>}}{{<}{(><(<({}{(}}{)}{><(><})>)(<({{><})}{><(><{}{{}}{{)))))()()()((}{)((}{)(><)}{(()()((

Try it online!

(Trailing newline because the interpreter outputs a newline.)

-176 bytes by improving the Brain-Flak quine

Basically a straight port of my Brain-Flak quine. The only major difference is the >< at the beginning, which is required in order to keep the data section out of the output.

><                 switch to right stack; effectively does nothing since both stacks are empty
))((...))()()((    data, same format as the Brain-Flak quine
}main loop{        same as the Brain-Flak quine, but with different constants
}}{{               clear current stack; this does nothing
)))))()()()((}{)((}{)(><)}{(()()((   push opening >< at beginning of code

Since this is Pain-Flak, the code also goes backward after it is finished going forward. As such, I had to ensure that this does not mess up the output.

))()())}{(><)(}{))(}{))()()()(((((   push 70, 68, 5, 4 on right stack
}}{{               clear right stack
}pool niam{        do nothing because top of stack is zero
))()()((...))((    push constants on right stack in reverse order
><                 switch to left stack and implicitly output
\$\endgroup\$
5
  • 1
    \$\begingroup\$ The only Pain-Flak solutions I've seen are ones directly translated from Brain-Flak... \$\endgroup\$
    – Jo King
    Apr 10, 2018 at 5:21
  • \$\begingroup\$ I will give the bounty but kinda lame that it is just a port \$\endgroup\$ Apr 25, 2018 at 10:39
  • \$\begingroup\$ @Christopher it is your fault that you made Pain-Flak so similar to Brain-Flak :P \$\endgroup\$
    – ASCII-only
    Apr 30, 2018 at 22:13
  • \$\begingroup\$ @ascii painflak update is removing swap stack \$\endgroup\$ Apr 30, 2018 at 22:47
  • \$\begingroup\$ @Christopher then it'll just be translated Mini-flak? \$\endgroup\$
    – ASCII-only
    Apr 30, 2018 at 23:09
4
\$\begingroup\$

Reflections, 9228 bytes

As the program has 8620 lines, I won't put it here completely.

First line:

\

Then, for each character in the last part (below), there are n newlines for ASCII character n, followed by:

+
#

After it, the last part is (the # is actually already covered by the previous section):

#  /  \          /+\  /#_=0v\
>~ <       /(1:1)#@/       ~
\(0/      2)     \3)(2:2)(3^ 0#+#@
           \:(24):(4#_#_#_  /
      +               \*             (4\
      \*(1(2                          +/

Test it!

Working on explanation.

I had to fix a bug in the interpreter for this one. Does that count as adding a feature just for a challenge?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ :| the URL encoding format is terrible \$\endgroup\$
    – ASCII-only
    May 22, 2018 at 0:57
4
\$\begingroup\$

2DFuck, 1352 1289 bytes

!xv>>>>x>x>>>>>>x>x>x>x>>x>x>x>>x>x>>x>x>x>x>x>>x>x>>x>x>>x>x>x>x>x>>x>x>x>x>x>>x>x>x>x>>x>x>>x>x>x>>x>x>>x>>>x>>>>>>x>>>>>x>>x>>>x>x>>x>>>x>>>>>>x>x>x>>>>>x>>>x>>>x>x>>x>x>x>x>x>>x>x>x>x>>>x>x>x>x>>x>x>x>x>x>x>>x>x>>>>>>>>>>x>x>>>>>x>>x>>>x>x>>x>>>x>>>>>>x>>>>>x>>x>>>x>x>>x>>>x>>>>>>x>x>x>>>>>x>>>x>>>x>x>x>>x>x>>x>x>>x>x>x>x>>x>x>>>x>x>>>>x>>x>>>x>>>>>>>>>>x>>>x>>>x>x>x>>x>x>>x>x>x>x>x>>x>x>>x>>>x>x>>>>x>>x>>>x>>>>>>>>>>x>>>x>>>>>>>>>>x>>>x>>>x>x>>x>x>>x>x>>x>x>x>x>>x>x>>x>x>>x>>>x>x>>>>x>>x>>>x>>>x>x>>x>x>>x>x>>x>x>x>>x>x>>x>x>x>>x>>>x>x>>>>>x>>>>x>x>>>>x>>x>>>x>>>>>>>>>>x>>>x>>>x>x>>x>x>x>x>>x>x>x>>x>x>>x>x>>>x>x>>>>x>>x>>>x>>>>>>>>>>x>>x>>>x>>>x>x>x>>x>x>x>x>x>x>>x>x>>>x>x>>>>x>>>x>>>>>>>>>>x>>x>>>x>>>x>x>x>>x>x>x>x>x>>x>x>x>>>x>x>>>>x>>>x>>>>>>>>>>x>>x>>>x>>>x>x>>x>x>>x>x>>x>x>x>x>x>>x>x>>>x>x>>>>x>>>x>>>>>>>>>>x>>>x>>>x>x>>x>x>x>x>x>>x>x>x>x>>>x>x>>>>x>>x>>>x>>>x>x>>x>x>x>x>>x>x>>x>x>x>>x>x>>>x>x>>>x>x>>>x>x>>>x>x>>>x>x>>>>>x>>>>x>x>>>>>>>>x>x>>>x>x>>>>>>>>>>x>x>>>>>x>>x>>>x>x>^x!..!.!....!.!.!....!....!...!.!..!.![<r!]![vr[!.!....!...]..!.....!.>^r!]![<r!]![vr[!..!.!.!...!.]r![>r[!..!.!....!.!]r![>r[>r[!.!.!.!...!.!.!]r![!.!.!.!..!.!..!]<]r![>r[!.!...!..!.!.]r![>r![!..!.....!.]r[>r![!..!....!..]r[>r![!.!.!.!....!.]r[>r[!.!....!...]r![!.!...!.!..!.]]]]]<]>]]>^r!]

Try it online!

Shaved off 63 bytes with Huffman coding! New explanation in progress.

\$\endgroup\$
1
  • \$\begingroup\$ Some thoughts; you can reverse the data encoding to avoid the second ![<r!], and you can save some bytes through reusing .s (for example all characters have a leading zero). 1131 bytes. I think you might get better huffman encoding through multiple or partial characters \$\endgroup\$
    – Jo King
    Sep 18, 2020 at 7:51
4
\$\begingroup\$

Z80Golf, 120 bytes

00000000: 21f5 76e5 2180 10e5 21cd 00e5 2180 7ce5  !.v.!...!...!.|.
00000010: 21cd 00e5 21e1 7de5 2106 14e5 2110 e8e5  !...!.}.!...!...
00000020: 2100 80e5 21e5 cde5 211b 3ee5 2100 80e5  !...!...!.>.!...
00000030: 211a cde5 2180 13e5 21cd 00e5 211b 1ae5  !...!...!...!...
00000040: 2180 1be5 21cd 00e5 213e 21e5 2106 14e5  !...!...!>!.!...
00000050: 0614 3e21 cd00 801b 1b1a cd00 8013 1acd  ..>!............
00000060: 0080 1b3e e5cd 0080 10e8 0614 e17d cd00  ...>.........}..
00000070: 807c cd00 8010 f576                      .|.....v

Try it online!

Verification:

$ ./z80golf a.bin | xxd
00000000: 21f5 76e5 2180 10e5 21cd 00e5 2180 7ce5  !.v.!...!...!.|.
00000010: 21cd 00e5 21e1 7de5 2106 14e5 2110 e8e5  !...!.}.!...!...
00000020: 2100 80e5 21e5 cde5 211b 3ee5 2100 80e5  !...!...!.>.!...
00000030: 211a cde5 2180 13e5 21cd 00e5 211b 1ae5  !...!...!...!...
00000040: 2180 1be5 21cd 00e5 213e 21e5 2106 14e5  !...!...!>!.!...
00000050: 0614 3e21 cd00 801b 1b1a cd00 8013 1acd  ..>!............
00000060: 0080 1b3e e5cd 0080 10e8 0614 e17d cd00  ...>.........}..
00000070: 807c cd00 8010 f576                      .|.....v

$ ./z80golf a.bin | diff -s a.bin -
Files a.bin and - are identical

Looks like no one tried to make a proper quine in machine code yet, so here is one.

Although the machine code is loaded to memory, it does NOT read any address occupied by the code. Instead, it uses the stack space to setup required data.

Disassembly

start:
  ld hl, $76f5
  push hl
  ld hl, $1080
  push hl
  ld hl, $00cd
  push hl
  ld hl, $7c80
  push hl
  ld hl, $00cd
  push hl
  ld hl, $7de1
  push hl
  ld hl, $1406
  push hl
  ld hl, $e810
  push hl
  ld hl, $8000
  push hl
  ld hl, $cde5
  push hl
  ld hl, $3e1b
  push hl
  ld hl, $8000
  push hl
  ld hl, $cd1a
  push hl
  ld hl, $1380
  push hl
  ld hl, $00cd
  push hl
  ld hl, $1a1b
  push hl
  ld hl, $1b80
  push hl
  ld hl, $00cd
  push hl
  ld hl, $213e
  push hl
  ld hl, $1406
  push hl

  ld b, 20
loop1:
  ld a, $21
  call $8000
  dec de
  dec de
  ld a, (de)
  call $8000
  inc de
  ld a, (de)
  call $8000
  dec de
  ld a, $e5
  call $8000
  djnz loop1

  ld b, 20
loop2:
  pop hl
  ld a, l
  call $8000
  ld a, h
  call $8000
  djnz loop2

  halt

At start, the stack pointer sp is zero, just like other registers. Pushing some values causes sp to decrease, so the values are stacked in the memory region $ffxx.

The combination ld hl, $xxxx and push hl seems like the best option to dump predefined values into some memory space. It takes 4 bytes to store 2 bytes; any other option I could think of uses 3 or more bytes to store only one byte.

The first loop prints the ld hl, $xxxx (21 xx xx) and push hl (e5) instructions for the data, from the bottom of the stack (the address, represented by de, is decreased starting from $0000).

ld b, $xx and djnz label combined forms a fixed-times looping construct. It is only 4 bytes, which is optimal in Z80 (unless the loop count is already saved in another register).

But there is an endianness problem here, so simply sweeping the memory addresses in decreasing order does not work. So I had to add a pair of dec de and inc de at the cost of 2 bytes (plus 4 bytes to push the 2 bytes into the stack).

The second loop prints the main code by popping data from the stack.

Possible improvement ideas

Since the code is longer than $38 or 56 bytes, we can't use rst $38 in place of call $8000. Having call $8000 6 times in total, it's a great opportunity for golf. I considered placing call $8000; ret at address $38, but then I have to reduce the main code into 26 bytes or lower.

I also thought of moving the code to the front by adding some jr, so that I can embed the call $8000; ret in the code part. But then I can't use the efficient "pop and print" loop. It prints the data in reverse order of pushing, so it can't be used to print the push part; the "print" overwrites the stack with the return address, so it can't be used to print the first part either.

Finally, there is room for alternative encoding since some bytes frequently appear in the code. But Z80 itself is severely limited in arithmetic...

\$\endgroup\$
4
\$\begingroup\$

J, 27 bytes

echo(,quote)'echo(,quote)'

Try it online!

It's surprising that there's no proper J quine submission yet.

How it works

echo(,quote)'echo(,quote)'
            'echo(,quote)'  The string `s`
      quote                 Wrap `s` with single quotes
     ,                      Concat s to the above; `(f g)x` is equal to `x f g x`
echo                        Explicitly print the result

echo is needed because the result of a non-REPL line is not printed.

\$\endgroup\$
4
\$\begingroup\$

MathGolf, 9 bytes

ÿ_'ÿ¬_'ÿ¬

Try it online!

Explanation:

ÿ_'ÿ¬_'ÿ¬
ÿ          Start string of length 4
 _'ÿ¬      Push "_'ÿ¬"
     _     Duplicate it
      'ÿ   Push the character "ÿ"
        ¬  Rotate stack so the "ÿ" is at the bottom
           Implicitly output "ÿ", "_'ÿ¬", "_'ÿ¬" join together
\$\endgroup\$
1
  • \$\begingroup\$ ÿ'ÿ¬_'ÿ¬_ is another 9-byter. \$\endgroup\$
    – maxb
    Jan 7, 2019 at 11:56
4
\$\begingroup\$

Symbolic Python, 30 bytes

_="_='_='+`_`+';__(_)'";__(_)

Try it online!

Yet another eval quine. The lack of being able to do %r in Symbolic Python is made up for Python 2's backticks, which are an alias for the repr function.

Explanation:

_="                   ";       # Assign the string to the _ variable
                        __(_)  # Eval the string
   _=                          # Assign to the _ variable
     '_='                      # The initial _=
         +`_`                  # The Python representation of the string
             +';__(_)'         # And the final evaling part
                               # Implicitly output the contents of _

An alternative that doesn't use the rather long eval function comes to 31 bytes, but I actually like it more.

_=';_="_="+`_`+_';_="_="+`_`+_

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Golunar, 951 bytes (394.25 bytes as an integer value)

19370043316195921194914466480856680152267470323963447024756684537596068724128114478400203537500358028693578705195025299449504112473728653294217406768139954805008550643064305270958743186186490023003785512463398359429631224818444304976679217098389450981926661910005004089131207223929032173888419619738722341444212432611576207643452266161684471788295034889655803444137372629364500165719019777515305922257911642994836581634099155833200157295629218533465854143898419293035289733581625252699105530843807023973345521520887128012736565874423200184723012755626596238647926406709693583878890472621210970350861368171259284533764490596207310864352873729240842719608391238098412446205860013948766486129442046252306334230243913196704614648889659870117069927719874852423159076941049170045933025772364248625729725500550726133134993128102614696728457139079375133324957922066270555810085574853273966267981675757808791933974619299446035844180580831907739236954600685575

Golunar is the decimal representation of the number of zeros that a Unary code would need. It translates to this brainfuck code:

->+>+>+>>>>>>>>>>>>+>+>>>+>+>+>+>+>>>>>>+>>>+>>>+>>>+>>>+>>>+>>+>>>>+>+>>+>>>+>>>+>>>+>>>+>>>+>>>+>>>+>+>>>>>>>+>+>+>+>>>+>>>>+>+>+>>>+>+>+>>+>>+>>>+>+>+>+>+>+>>>>>+>+>+>>>+>+>+>>>>>>>>+>+>>>>>+>>+>>>+>>>+>+>>+>+>+>>+>>+>>>+>+>>>+>+>>+>+>+>+>>+>>>+>+>+>>>>>+>+>+>>>+>+>+>>>>>>>>+>+>>>>>>>+>+>+>+>>>+>>>>+>>+>+>+>+>+>>>>>>+>>+>>>>+>+>>>>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>>+>>>>+>>>+>>>>>>+>+>+>+>>>>>>+>>>+>>+>+>+>+>+>>>+>+>+>>>+>>>+>+>>+>+>>+>+>>+>+>>+>+>>+>+>>+>+>>+>+>>>>>>+>+>+>+>>+>>>>+>>>+>>>>>>+>+>+>+>>+>>>>+>>>+>>>>>>+>+>+>+>>+>>>>+>>>+>>>>>>+>+>+>+>>+>>>>+>>>+>>>>>>+>+>+>>>+>+>>>+>+>+>>>+>+>>+>+>+>>+>>+>>>+>+>>>+>+>>+>+>>+>>>+>>>>>>+>>>+>>>>>>+>>>+>>+>+>+>>>>>+>+>+>+>>>>>>+>>>>>>+>>+>+>+>>+>>+>>>+>>>+>+>+>+>>>>+>+>+>+>>+>>>+>>>+>+>+>>>>>+>+>>>>>>>+>+>+>+>+>>+>>>+>+>+>+>>>>+>+>>+>>>>>>+>>>+>>>>>>+>>+>+>+>>>>>+>+>>+>>>>>>>>>+>+>>+>+>>+>+[[>>+[>]+>++>+[<]<-]>>[>]<<-[<]<<+]+>+>-[>]++>++>++[[-<+]-<[-<[>++<-[>++<-[>++<-[>++<-[>--------<<[-]++>-[>++<-]]]]]]>[<+>-]+<<]>>[>>]<[>]<-[[-<+]-<<+>[>>]<[>]]<+]<[<<]>>[+++++++[<++++++>-]<.>>>]

Try it online!

I couldn't find an online Golunar interpreter, but you can use this Golunar to brainfuck converter to get the brainfuck code and then execute it.

This code is inspired by a brainfuck quine of Eric Bosman and Daniel B Cristofani. First it reads a list of bits that represent the bit-codes of the code part, then it builds up a list of bits that represent the ">" and "+" characters needed to print the first list. Those lists are merged and hold the binary value of the Golunar code. In the last step, the decimal value of the binary number are computed and printed.

[ 
tape: [decimal digits], value start marker/VS(-1)(starting cell), [input bits], between binary marker/BB(0), [binary output data]

input bits are 0 or 1, output bits are in reversed order and have the values 1(used) or 2(used and set)
input must be given in reversed order

decimal digits consist of two cells per value (value, digit used marker(DU)(1))

bit values:
> 000
< 001
+ 010
- 011
. 100
, 101
[ 110
] 111

]

-                       set VS

                    set input bits
>+>+>+>>>>>>>>>>>>+>+>>>+>+>+>+>+>>>>>>+>>>+>>>+>>>+>>>+>>>+>>+>>>>+>+>>+>>>+>>>+>>>+>>>+>>>+>>>+>>>
+>+>>>>>>>+>+>+>+>>>+>>>>+>+>+>>>+>+>+>>+>>+>>>+>+>+>+>+>+>>>>>+>+>+>>>+>+>+>>>>>>>>+>+>>>>>+>>+>>>+
>>>+>+>>+>+>+>>+>>+>>>+>+>>>+>+>>+>+>+>+>>+>>>+>+>+>>>>>+>+>+>>>+>+>+>>>>>>>>+>+>>>>>>>+>+>+>+>>>+>>
>>+>>+>+>+>+>+>>>>>>+>>+>>>>+>+>>>>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>+>>+>>>>+>>>+>>>>>>+>+>+>+>
>>>>>+>>>+>>+>+>+>+>+>>>+>+>+>>>+>>>+>+>>+>+>>+>+>>+>+>>+>+>>+>+>>+>+>>+>+>>>>>>+>+>+>+>>+>>>>+>>>+>
>>>>>+>+>+>+>>+>>>>+>>>+>>>>>>+>+>+>+>>+>>>>+>>>+>>>>>>+>+>+>+>>+>>>>+>>>+>>>>>>+>+>+>>>+>+>>>+>+>+>
>>+>+>>+>+>+>>+>>+>>>+>+>>>+>+>>+>+>>+>>>+>>>>>>+>>>+>>>>>>+>>>+>>+>+>+>>>>>+>+>+>+>>>>>>+>>>>>>+>>+
>+>+>>+>>+>>>+>>>+>+>+>+>>>>+>+>+>+>>+>>>+>>>+>+>+>>>>>+>+>>>>>>>+>+>+>+>+>>+>>>+>+>+>+>>>>+>+>>+>>>
>>>+>>>+>>>>>>+>>+>+>+>>>>>+>+>>+>>>>>>>>>+>+>>+>+>>+>

                    list rebuilding loop: append bits for "greater than" and plus symbols to the right of input data
+[                      for each input bit
  [                     while bit is greater than 0
    >>+                 copy bit to out data
    [>]+>++>+           append bits for plus (used markers plus 010)
    [<]<-               decrement in value
  ]
  >>[>]<<-              change most right character from plus to gt (010 to 000)
  [<]                   return to BB
  <<+                   repeat if not on VS
]
+>+>-                   prepare binary to decimal routine: set VS DU and first 1

[>]++>++>+              append bits for minus (011)

                    binary to decimal loop: use "double and add algorithm" to calculate the digits of the decimal value
+[                      if not on VS then
  [-<+]-                restore current bit value and go to VS
  <                     go to first DU
  [                 digit doubling loop
    -<                  remove DU and go to corresponding digit
    [
      >++<-             decrement current value and add 2 to temp value four times
      [
        >++<-
        [
          >++<-
          [
            >++<-
            [                   if value was greater than 4 then
              >---- ----        subtract 8 from temp
              <<[-]++           set next digit temp = 2 (DU plus 1)
              >-                decrement current digit
              [>++<-]           set temp = remaining value * 2
            ]
          ]
        ]
      ]
    ]
    >[<+>-]             set current digit = temp
    +                   set DU
    <<                  go to next digit
  ]                 end of digit doubling loop
  >>[>>]<[>]<           go to current bit
  -[                    if bit is 2 (used plus 1)
    [-<+]-              delete bit and go to VS
    <<+                 increment least significant digit
    >[>>]<[>]           go to current bit
  ]
  <+                    if not on VS then repeat  
]                   end of binary to decimal loop

<[<<]>                  go to most significant digit
>[                  printing loop: for each DU print corresponding value
  +++++++[<++++++>-]<.  add 48 to value (ASCII 0) and print
  >>>                   go to next DU
]
\$\endgroup\$
4
\$\begingroup\$

Keg, 49 43 bytes

\^\(\\\\\,\:\&\^\&\^\,\)\^\#^(\\,:&^&^,)^#

Try it online!

(note the trailing newline...)

-6 bytes thanks to Jo King reminding me that comments exist

This is a horrible mess, and I'm sure it can be outgolfed easily by someone comfortable with Keg's stack, and/or once "/' stop erroring. The first two thirds simply push each character from the last third to the stack in order (such that the last character is on top), and then the last third:

^                   Reverses the stack (such that the first character is on top),
 (         )        then does the following for each item CURRENTLY on the stack:
  \\,               print a backslash,
     :              duplicate the top of the stack,
      &             pop it to the register,
       ^            flip the stack,
        &           push the register,
         ^          flip the stack again,
          ,         and pop and print the top of the stack;
            ^       finally reversing what's left on the stack
             #\n    and commenting out the trailing newline,
                    so the stack is then implicitly printed bottom first,
                    with a trailing newline.

Essentially, it prints the first two thirds while copying itself onto the bottom of the stack, then flips the copy to be implicitly printed.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks, fixed now... wait no \$\endgroup\$ Oct 1, 2019 at 5:15
  • \$\begingroup\$ Now it's fixed \$\endgroup\$ Oct 1, 2019 at 5:44
4
\$\begingroup\$

Keg, 21 bytes

HBZLTXJMIC(":,48*-)#

Try it online!

The string of letters translates to the code section reversed and shifted up by 32. It's lucky all the characters used are in the correct range for shifting.

Explanation:

(       )    # Loop over the stack
 "           # Shift stack left
  :          # Duplicate the letter
   ,         # Print the letter
    48*-     # Subtract 32 from the ordinal value of the letter
         #   # Comment out the newline
             # Print the shifted characters with a trailing newline
\$\endgroup\$
4
\$\begingroup\$

BaCon, 54 bytes

Without using the SOURCE$ variable, the smallest Quine is 55 bytes:

s$="s$=%c%s%c:?34,s$,34 FORMAT s$":?34,s$,34 FORMAT s$
\$\endgroup\$
4
  • 2
    \$\begingroup\$ please make the main part of the post the 55 byte noncheating quine \$\endgroup\$ Oct 14, 2016 at 21:05
  • \$\begingroup\$ Done. I wonder why using 'SOURCE$' is cheating, looking at the other contributions? \$\endgroup\$
    – Peter
    Oct 15, 2016 at 20:05
  • 1
    \$\begingroup\$ The other answers are also cheating. This challenge is over five years old; most answers were posted before we had clearly defined rules. Since last month, improper quines are officially forbidden in all challenges, even if the spec doesn't explicitly forbid them. \$\endgroup\$
    – Dennis
    Oct 15, 2016 at 20:13
  • \$\begingroup\$ Thanks for pointing me to this page, I was unaware of it. \$\endgroup\$
    – Peter
    Oct 16, 2016 at 20:29
4
\$\begingroup\$

MAWP, 60 bytes

Thanks @JoKing for saving a whopping 55 bytes!

48923792915350878792773358929392915992930\%\[52WW\M!:\]\[;\]

Try it!

It's finally done!

Explanation:

The quine is essentially split into three parts. The first part is the data, and is represented by the big blob of numbers in the start (excluding a 0). It represents the ASCII codes of the rest three parts of the code. The second part is the decoder that both turns the long stack of separate numbers into a smaller stack with multi-digit numbers and prints it as numbers without destroying it. The last part loops over the stack and prints it as ASCII. Note that we don't have to preserve the data in stack any more.

Old solution:

115 bytes

481263753508787774753508787775350878777479153508787774793479133584793479159930~%52WWM/52WWM52WWM/[52WWM/]/[!:/]/[;]
\$\endgroup\$
8
  • \$\begingroup\$ @JoKing - the auto-pasting of the code in the link doesn't seem to work (at least for the Mac OS X browsers that I've tried), and it's difficult to extract the code from the link itself. Could you include the code itself in a comment? \$\endgroup\$ Aug 17, 2020 at 7:53
  • \$\begingroup\$ @DominicvanEssen could you possibly describe the issue with the auto-copy on the interpreter? I myself work on a Mac, but had no issues developing and testing everything, as so do others who use the interpreter. \$\endgroup\$
    – Dion
    Aug 17, 2020 at 8:57
  • \$\begingroup\$ @JoKing - Thanks! @Dion - When I click on your link (or that of JoKing) in either Chrome or in Firefox, I get the 'MAWP version 1.1 (latest)' web page, but the 'code:' box is empty. In Chrome I can manually copy-paste the code into the box, and it then works fine when I click 'Run code', but it's difficult to get the code out of the link because many of the characters are represented with %s. FYI, I just tried to generate my own link (using the 'generate link' button after pasting-in your code), and this link also opens an empty 'code:' box. Hope this helps.... \$\endgroup\$ Aug 17, 2020 at 15:57
  • \$\begingroup\$ @DominicvanEssen if it doesn't trouble you, would you mind and telling me two things: a) are the query's in the links empty b) does the dev console show any errors on loading and/or link generating? Thanks! \$\endgroup\$
    – Dion
    Aug 17, 2020 at 16:00
  • \$\begingroup\$ @Dion - I'm not a developer so I'm rather out of my depth here, but I'll try to answer. Let me know if I haven't told you what you need to know. The link seems to contain the program (it ends with .../?code=481...b%5D&input=, but the program doesn't end-up in the 'code:' box. I don't really know what the 'dev console' is, but (in Chrome) I clicked on 'View>Developer>Developer tools' and selected the 'console' tab, and there are no errors displaying (either for loading or creating links). \$\endgroup\$ Aug 17, 2020 at 16:11
4
\$\begingroup\$

V, 4 bytes

2i2i

Try it online!

Explanation:

2       " Two times:
 i      " Insert the following:
  2i    " The string '2i'

This is pretty straightforward. In fact, this almost works in regular vim. There is just one minor thing in the way: The string '2i' isn't inserted twice until you hit <esc>. In V, this is solved by implicitly ending every program with an <esc> (Really, that's an oversimplification, but it's close enough to the truth).

Old versions of V always added a newline to the output, which is why I didn't post this earlier. However, in commit b6c238d, this was fixed.

This answer works just because of luck. The approach doesn't extend well to general purpose quines/quine-variations. The shortest quine I'm aware of that can be trivially modified is

ñéÑ~"qpÿ

Try it online!

Explanation:

ñ           " Start recording into register 'q'
 éÑ         " Insert 'Ñ' (uppercase so that the recording doesn't stop here)
   ~        " Toggle the case the character under the cursor (the 'Ñ')
    "qp     " Paste the contents of register 'q'
       ÿ    " Stop recording and play it all back

The reason the ÿ is needed, is because it is implicitly added to the end of macros, a feature that is unfortunate for quines, but very convenient for golf.

The nice thing about this quine is that we can do almost anything inside of the recording and it is still valid since it will be pasted later anyway.

\$\endgroup\$
4
\$\begingroup\$

ShapeScript, 16 bytes

'"%r"@%"0?!"'0?!

Try it online!

Verification

$ shapescript quine.shape | diff -sq - quine.shape
Files - and quine.shape are identical

How it works

'       Push a string that, when evaluated, does the following.
  "%r"  Push this formatting string. %r gets replaced by a string
        representation of the corresponding argument.
  @     Swap the string that is being evaluated on top of the stack.
  %     Apply formatting to the string on top of the stack.
  "0?!" Push that string.
'
0?!     Push a copy of the previous string and evaluate it.

ShapeScript, 32 bytes

'"'"'"'1?3?+3*2?+@+@+@#"0?!"'0?!

This version does not use string formatting. It's not particularly short, but I find it rather interesting.

Try it online!

How it works

'"'     Push a double quote singleton string.
"'"     Push a single quote singleton string.
'       Push a string that, when evaluated, does the following:
  1?3?    Copy the second and fourth topmost stack items.
          This pushes a copy of the single, then the double quote.
  +3*     Concatenate both and repeat the result three times.
  2?+     Copy the third topmost stack item and append it to the string.
          We now have a string of the first seven characters of the source.
  @+      Swap with the string on the stack (the string that is being
          evaluated) and concatenate.
  @+      Swap with the original single quote and concatenate.
  @#      Swap with the original double quote and discard it.
  "0?!"   Push that string.
'
0?!     Push a copy of the above string and evaluate it.
\$\endgroup\$
4
\$\begingroup\$

C++, 117 bytes

#include<cstdio>
#define Q(S)char*q=#S;S
Q(int main(){printf("#include<cstdio>\n#define Q(S)char*q=#S;S\nQ(%s)",q);})

Try it online!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ What compiler does this use? This does not appear to work on gcc. \$\endgroup\$
    – Wheat Wizard
    Jan 13, 2017 at 15:26
  • \$\begingroup\$ It works on my gcc 4.9.2. \$\endgroup\$ Jan 13, 2017 at 15:38
  • 2
    \$\begingroup\$ Ok. According to clang, this is not valid C++, since the return type of main() is missing. I fixed that now. \$\endgroup\$ Jan 13, 2017 at 15:40
  • \$\begingroup\$ My version of gcc was 4.2.1 (I should update some time). Now that main has a return type it works. \$\endgroup\$
    – Wheat Wizard
    Jan 13, 2017 at 15:47
  • \$\begingroup\$ Suggest using auto q to make it standards compliant \$\endgroup\$
    – c--
    Jun 6 at 17:26
4
\$\begingroup\$

Vyxal, 57 bytes

2996557859220556162530996767470:`0123456789+:`\\\`\τ+++τ+

Try it Online!

The big number at the front is the rest of the program encoded using the key 0123456789+:τ\ with a backtick (dammit markdown). The rest of the program constructs the key, decodes this, and concatenates the original number to the result.

\$\endgroup\$
4
\$\begingroup\$

Klein, 13 + 6 = 19 bytes

Martin Ender has made a shorter Quine here

+6 from the 000 topology and the -A flag, (also works in the 001 topology)

<:3+@+3:<"

Try it online!

Explanation

Like described in my earlier not so Quine the " character cannot be made with a string literal so we need to make it by doing math on a similar character, in this case the unit separator character for reasons described later. The difference between this Quine and earlier attempts is that this we can get around the restriction of having to read the string forwards by making the string itself a palindrome. This way reading backwards is the same as reading it forwards.

So when the program starts it passes through a unit separator, then hits < which turns it around, it goes through the " and pushes the entire line. :3+ turns makes a " out of the final unit separator and @ terminates the program causing it to print.

\$\endgroup\$
4
\$\begingroup\$

Vyxal D, 12 bytes

`q\`:Ė\`+`:Ė

Try it Online!

What differences this from the other quine is an eval quine so is easily payload-capable - that is, all the processing code you need can go after the + and only needs to be iterated once.

Will be 9 bytes once parsing bugs get fixed.

`        `   # String of code to be executed
 q           # Quotify / uneval
  \`:Ė\`+    # Append a `:Ė`
          :  # Duplicate
           Ė # Run on itself
\$\endgroup\$
4
\$\begingroup\$

K (oK), 9 bytes

`0:"`0:"

Try it online!

Posted with permission from @Bubbler.

So, oK has this cursed bug feature where prettyprinting of the output still occurs even if you print something else. `0 is the output symbol, and calling print to it with : prints the string, but returns the string as well, which is implicitly prettyprinted, with quotes to show it's a string.

K (oK), 34 bytes

{a:"0x7b613a22,a,0x223b2e617d";.a}

Try it online!

My attempt.

In K, hexadecimal literals represent byte values. 0x7b613a22 is {a:", and 0x223b2e617d is ";.a}, both of which are added to the string. So we set the value a to a string that when evaluated, returns itself wrapped in the boilerplate, and evaluate it.

\$\endgroup\$
7
  • \$\begingroup\$ How is this not cheating? \$\endgroup\$
    – pxeger
    Oct 30, 2021 at 10:25
  • \$\begingroup\$ @pxeger All these seem to be valid... \$\endgroup\$
    – emanresu A
    Oct 30, 2021 at 12:00
  • \$\begingroup\$ All of those are invalid too IMO. Why are they accepted? \$\endgroup\$
    – pxeger
    Oct 30, 2021 at 13:30
  • \$\begingroup\$ Technically, it's not reading it's own file in any way - it's using a feature of the language to stringify a function, which happens to produce its source code, like the Befunge-93 quine. \$\endgroup\$
    – emanresu A
    Oct 30, 2021 at 19:41
  • 1
    \$\begingroup\$ @pxeger Fine, I'll add in a proper quine. Gimme a bit of time to make it work \$\endgroup\$
    – emanresu A
    Oct 31, 2021 at 8:00
4
\$\begingroup\$

Vyxal 2.6.0, 4 bytes

`I`I

Try it Online!

Posting this as a separate answer because it's using overloads not available at the time of the other answer.

Explained

`I`I
`I`   # The string "I"
   I  # prepend a quoted version of that to itself and implicitly print
\$\endgroup\$
3
  • \$\begingroup\$ The code-block in your Vyxal TIO doesn't seem to work. It does see it as 4 bytes and outputs correctly, but the code itself shows as a zero-width space.. Seems there is something wrong in the online compiler. (Your answer looks fine btw.) \$\endgroup\$ Nov 29, 2021 at 10:47
  • \$\begingroup\$ @KevinCruijssen it works perfectly fine on my end. Having said that, it wouldn't surprise me if there are issues with the site...we only launched it yesterday because we finished a language rewrite we've been working on for a few months. Hopefully a PR I'm working on will fix this :p \$\endgroup\$
    – lyxal
    Nov 29, 2021 at 10:53
  • \$\begingroup\$ Welp, this is what I see on my end (latest Chrome version), with these errors in the browser console, and the &ZeroWidthSpace; when I inspect element. 🤷 \$\endgroup\$ Nov 29, 2021 at 11:40
1
4 5
6
7 8
14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.