246
\$\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
  • 6
    \$\begingroup\$ Do you not mean, "Golf you a quine for greater good!"? \$\endgroup\$ Commented May 3, 2011 at 2:49
  • 64
    \$\begingroup\$ @muntoo it's a play on "Learn you a Haskell for Great Good". \$\endgroup\$ Commented May 3, 2011 at 2:52
  • 28
    \$\begingroup\$ Did anybody notice that this is question 69? \$\endgroup\$
    – aidan0626
    Commented Oct 24, 2020 at 22:47

459 Answers 459

1 2 3
4
5
16
8
\$\begingroup\$

APL, 22 bytes

1⌽22⍴11⍴'''1⌽22⍴11⍴'''

This is part of the FinnAPL Idiom Library.

        '''1⌽22⍴11⍴'''  ⍝ The string literal '1⌽22⍴11⍴' (quotes in string)
     11⍴                ⍝ Fill an 11-element array with these characters
                        ⍝ But the string has length 10, so we get '1⌽22⍴11⍴''
  22⍴                   ⍝ Do this again for 22 chars: '1⌽22⍴11⍴'''1⌽22⍴11⍴''
1⌽                      ⍝ Rotate left (puts quote at the back)

Try it on ngn/apl

\$\endgroup\$
8
\$\begingroup\$

ed(1), 45 bytes

We have quines for TECO, Vim, and sed, but not the almighty ed?! This travesty shall not stand. (NB: See also, this error quine for ed)

Try it Online!

a
a
,
,t1
$-4s/,/./
,p
Q
.
,t1
$-4s/,/./
,p
Q

Stolen from here. It should be saved as a file quine.ed then run as follows: (TIO seems to work a bit differently)

$ ed < quine.ed
\$\endgroup\$
8
\$\begingroup\$

Python 3, 34 Bytes

print((s:='print((s:=%r)%%s)')%s)

As far as I'm aware this is shorter than any other published Python 3 quine, mainly by virtue of having been written in the future with respect to most of them, so it can use :=.

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$
    – rydwolf
    Commented Mar 4, 2022 at 0:17
  • 2
    \$\begingroup\$ There is _='_=%r;print(_%%_)';print(_%_) (32 bytes w/newline), basically the Python 2 quine ported to Python3 \$\endgroup\$
    – SuperPizz
    Commented Apr 17, 2022 at 17:46
7
\$\begingroup\$

Befunge 98 - 17 11 characters

<@,+1!',k9"

Or if using g is allowed:

Befunge 98 - 12 10

<@,g09,k8"
\$\endgroup\$
2
  • \$\begingroup\$ Explanation for why using g may not be allowed? \$\endgroup\$
    – MD XF
    Commented Jun 9, 2017 at 23:56
  • \$\begingroup\$ @MDXF g is arguably reading the source code. Befunge copies the code to the execution space and g reads the character at the x, y position in the execution space \$\endgroup\$
    – Justin
    Commented Jun 11, 2017 at 6:50
7
\$\begingroup\$

TECO, 20 bytes

<Tab>V27:^TJDV<Esc>V27:^TJDV

The <Esc> should be replaced with ASCII 0x1B, and the <Tab> with 0x09.

  • <Tab>V27:^TJDV<Esc> inserts the text <Tab>V27:^TJDV. This is not because there is a text insertion mode which TECO starts in by default. Instead, <Tab> text <Esc> is a special insertion command which inserts a tab, and then the text. A string whose own initial delimiter is part of the text -- very handy.
  • V prints the current line.
  • 27:^T prints the character with ASCII code 27 without the usual conversion to a printable representation.
  • J jumps to the beginning of the text.
  • D deletes the first character (the tab).
  • V prints the line again.
\$\endgroup\$
1
7
\$\begingroup\$

Python 2, 31 bytes

s="print's=%r;exec s'%s";exec s

It's 2 bytes longer than the shortest Python quine on this question, but it's much more useful, since you don't need to write everything twice.

For example, to print a program's own source code in sorted order, we can just do:

s="print''.join(sorted('s=%r;exec s'%s))";exec s

Another example by @feersum can be found here.

Notes

The reason the quine works is because of %r's behaviour. With normal strings, %r puts single quotes around the string, e.g.

>>> print "%r"%"abc"
'abc'

But if you have a single quotes inside the string, it uses double quotes instead:

>>> print "%r"%"'abc'"
"'abc'"

This does, however, mean that the quine has a bit of a problem if you want to use both types of quotes in the string.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Python appends a leading newline to the output, so you will have to add a newline at the end of your program (making your program 32 bytes, not 31). \$\endgroup\$
    – MilkyWay90
    Commented May 4, 2019 at 17:30
7
\$\begingroup\$

RETURN, 18 bytes

"34¤¤,,,,"34¤¤,,,,

Try it here.

First RETURN program on PPCG ever! RETURN is a language that tries to improve DUP by using nested stacks.

Explanation

"34¤¤,,,,"         Push this string to the stack
          34       Push charcode of " to the stack
            ¤¤     Duplicate top 2 items
              ,,,, Output all 4 stack items from top to bottom
\$\endgroup\$
0
7
\$\begingroup\$

S.I.L.O.S, 2642 2593 bytes

Credits to Rohan Jhunjhunwala for the algorithm.

A = 99
set A 112
A + 1
set A 114
A + 1
set A 105
A + 1
set A 110
A + 1
set A 116
A + 1
set A 76
A + 1
set A 105
A + 1
set A 110
A + 1
set A 101
A + 1
set A 32
A + 1
set A 65
A + 1
set A 32
A + 1
set A 61
A + 1
set A 32
A + 1
set A 57
A + 1
set A 57
A + 1
set A 10
A + 1
set A 67
A + 1
set A 32
A + 1
set A 61
A + 1
set A 32
A + 1
set A 57
A + 1
set A 57
A + 1
set A 10
A + 1
set A 66
A + 1
set A 32
A + 1
set A 61
A + 1
set A 32
A + 1
set A 103
A + 1
set A 101
A + 1
set A 116
A + 1
set A 32
A + 1
set A 67
A + 1
set A 10
A + 1
set A 108
A + 1
set A 98
A + 1
set A 108
A + 1
set A 68
A + 1
set A 10
A + 1
set A 67
A + 1
set A 32
A + 1
set A 43
A + 1
set A 32
A + 1
set A 49
A + 1
set A 10
A + 1
set A 112
A + 1
set A 114
A + 1
set A 105
A + 1
set A 110
A + 1
set A 116
A + 1
set A 32
A + 1
set A 115
A + 1
set A 101
A + 1
set A 116
A + 1
set A 32
A + 1
set A 65
A + 1
set A 32
A + 1
set A 10
A + 1
set A 112
A + 1
set A 114
A + 1
set A 105
A + 1
set A 110
A + 1
set A 116
A + 1
set A 73
A + 1
set A 110
A + 1
set A 116
A + 1
set A 32
A + 1
set A 66
A + 1
set A 10
A + 1
set A 112
A + 1
set A 114
A + 1
set A 105
A + 1
set A 110
A + 1
set A 116
A + 1
set A 76
A + 1
set A 105
A + 1
set A 110
A + 1
set A 101
A + 1
set A 32
A + 1
set A 65
A + 1
set A 32
A + 1
set A 43
A + 1
set A 32
A + 1
set A 49
A + 1
set A 10
A + 1
set A 66
A + 1
set A 32
A + 1
set A 61
A + 1
set A 32
A + 1
set A 103
A + 1
set A 101
A + 1
set A 116
A + 1
set A 32
A + 1
set A 67
A + 1
set A 10
A + 1
set A 105
A + 1
set A 102
A + 1
set A 32
A + 1
set A 66
A + 1
set A 32
A + 1
set A 68
A + 1
set A 10
A + 1
set A 70
A + 1
set A 32
A + 1
set A 61
A + 1
set A 32
A + 1
set A 57
A + 1
set A 57
A + 1
set A 10
A + 1
set A 69
A + 1
set A 32
A + 1
set A 61
A + 1
set A 32
A + 1
set A 103
A + 1
set A 101
A + 1
set A 116
A + 1
set A 32
A + 1
set A 70
A + 1
set A 10
A + 1
set A 108
A + 1
set A 98
A + 1
set A 108
A + 1
set A 71
A + 1
set A 10
A + 1
set A 70
A + 1
set A 32
A + 1
set A 43
A + 1
set A 32
A + 1
set A 49
A + 1
set A 10
A + 1
set A 112
A + 1
set A 114
A + 1
set A 105
A + 1
set A 110
A + 1
set A 116
A + 1
set A 67
A + 1
set A 104
A + 1
set A 97
A + 1
set A 114
A + 1
set A 32
A + 1
set A 69
A + 1
set A 10
A + 1
set A 69
A + 1
set A 32
A + 1
set A 61
A + 1
set A 32
A + 1
set A 103
A + 1
set A 101
A + 1
set A 116
A + 1
set A 32
A + 1
set A 70
A + 1
set A 10
A + 1
set A 105
A + 1
set A 102
A + 1
set A 32
A + 1
set A 69
A + 1
set A 32
A + 1
set A 71
A + 1
printLine A = 99
C = 99
B = get C
lblD
C + 1
print set A 
printInt B
printLine A + 1
B = get C
if B D
F = 99
E = get F
lblG
F + 1
printChar E
E = get F
if E G

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Good job! May I "borrow this post for my github repository? \$\endgroup\$ Commented Aug 26, 2016 at 19:12
  • \$\begingroup\$ @RohanJhunjhunwala Sure. \$\endgroup\$
    – Leaky Nun
    Commented Aug 26, 2016 at 19:18
7
\$\begingroup\$

S.I.L.O.S, 3057 bytes

A = 99
def S set 
S A 112
A + 1
S A 114
A + 1
S A 105
A + 1
S A 110
A + 1
S A 116
A + 1
S A 76
A + 1
S A 105
A + 1
S A 110
A + 1
S A 101
A + 1
S A 32
A + 1
S A 65
A + 1
S A 32
A + 1
S A 61
A + 1
S A 32
A + 1
S A 57
A + 1
S A 57
A + 1
S A 10
A + 1
S A 72
A + 1
S A 32
A + 1
S A 61
A + 1
S A 32
A + 1
S A 56
A + 1
S A 51
A + 1
S A 10
A + 1
S A 112
A + 1
S A 114
A + 1
S A 105
A + 1
S A 110
A + 1
S A 116
A + 1
S A 32
A + 1
S A 100
A + 1
S A 101
A + 1
S A 102
A + 1
S A 32
A + 1
S A 10
A + 1
S A 112
A + 1
S A 114
A + 1
S A 105
A + 1
S A 110
A + 1
S A 116
A + 1
S A 67
A + 1
S A 104
A + 1
S A 97
A + 1
S A 114
A + 1
S A 32
A + 1
S A 72
A + 1
S A 10
A + 1
S A 112
A + 1
S A 114
A + 1
S A 105
A + 1
S A 110
A + 1
S A 116
A + 1
S A 76
A + 1
S A 105
A + 1
S A 110
A + 1
S A 101
A + 1
S A 32
A + 1
S A 32
A + 1
S A 83
A + 1
S A 32
A + 1
S A 10
A + 1
S A 67
A + 1
S A 32
A + 1
S A 61
A + 1
S A 32
A + 1
S A 57
A + 1
S A 57
A + 1
S A 10
A + 1
S A 66
A + 1
S A 32
A + 1
S A 61
A + 1
S A 32
A + 1
S A 103
A + 1
S A 101
A + 1
S A 116
A + 1
S A 32
A + 1
S A 67
A + 1
S A 10
A + 1
S A 108
A + 1
S A 98
A + 1
S A 108
A + 1
S A 68
A + 1
S A 10
A + 1
S A 67
A + 1
S A 32
A + 1
S A 43
A + 1
S A 32
A + 1
S A 49
A + 1
S A 10
A + 1
S A 112
A + 1
S A 114
A + 1
S A 105
A + 1
S A 110
A + 1
S A 116
A + 1
S A 67
A + 1
S A 104
A + 1
S A 97
A + 1
S A 114
A + 1
S A 32
A + 1
S A 72
A + 1
S A 10
A + 1
S A 112
A + 1
S A 114
A + 1
S A 105
A + 1
S A 110
A + 1
S A 116
A + 1
S A 32
A + 1
S A 32
A + 1
S A 65
A + 1
S A 32
A + 1
S A 10
A + 1
S A 112
A + 1
S A 114
A + 1
S A 105
A + 1
S A 110
A + 1
S A 116
A + 1
S A 73
A + 1
S A 110
A + 1
S A 116
A + 1
S A 32
A + 1
S A 66
A + 1
S A 10
A + 1
S A 112
A + 1
S A 114
A + 1
S A 105
A + 1
S A 110
A + 1
S A 116
A + 1
S A 76
A + 1
S A 105
A + 1
S A 110
A + 1
S A 101
A + 1
S A 32
A + 1
S A 65
A + 1
S A 32
A + 1
S A 43
A + 1
S A 32
A + 1
S A 49
A + 1
S A 10
A + 1
S A 66
A + 1
S A 32
A + 1
S A 61
A + 1
S A 32
A + 1
S A 103
A + 1
S A 101
A + 1
S A 116
A + 1
S A 32
A + 1
S A 67
A + 1
S A 10
A + 1
S A 105
A + 1
S A 102
A + 1
S A 32
A + 1
S A 66
A + 1
S A 32
A + 1
S A 68
A + 1
S A 10
A + 1
S A 70
A + 1
S A 32
A + 1
S A 61
A + 1
S A 32
A + 1
S A 57
A + 1
S A 57
A + 1
S A 10
A + 1
S A 69
A + 1
S A 32
A + 1
S A 61
A + 1
S A 32
A + 1
S A 103
A + 1
S A 101
A + 1
S A 116
A + 1
S A 32
A + 1
S A 70
A + 1
S A 10
A + 1
S A 108
A + 1
S A 98
A + 1
S A 108
A + 1
S A 71
A + 1
S A 10
A + 1
S A 70
A + 1
S A 32
A + 1
S A 43
A + 1
S A 32
A + 1
S A 49
A + 1
S A 10
A + 1
S A 112
A + 1
S A 114
A + 1
S A 105
A + 1
S A 110
A + 1
S A 116
A + 1
S A 67
A + 1
S A 104
A + 1
S A 97
A + 1
S A 114
A + 1
S A 32
A + 1
S A 69
A + 1
S A 10
A + 1
S A 69
A + 1
S A 32
A + 1
S A 61
A + 1
S A 32
A + 1
S A 103
A + 1
S A 101
A + 1
S A 116
A + 1
S A 32
A + 1
S A 70
A + 1
S A 10
A + 1
S A 105
A + 1
S A 102
A + 1
S A 32
A + 1
S A 69
A + 1
S A 32
A + 1
S A 71
A + 1
printLine A = 99
H = 83
print def 
printChar H
printLine  S 
C = 99
B = get C
lblD
C + 1
printChar H
print  A 
printInt B
printLine A + 1
B = get C
if B D
F = 99
E = get F
lblG
F + 1
printChar E
E = get F
if E G

Try it online!

I am ashamed to say this took me a while to write even though most of it was generated by another java program. Thanks to @MartinEnder for helping me out. This is the first quine I have ever written. Credits go to Leaky Nun for most of the code. I "borrowed his code" which was originally inspired by mine. My answer is similar to his, except it shows the "power" of the preprocessor. Hopefully this approach can be used to golf of bytes if done correctly. The goal was to prevent rewriting the word "set" 100's of times.
Please check out his much shorter answer!

\$\endgroup\$
4
  • \$\begingroup\$ How does this work? \$\endgroup\$
    – Leaky Nun
    Commented Aug 26, 2016 at 18:44
  • \$\begingroup\$ It's borked from my understanding @LeakyNun but it essentially writes it source code to the memory buffer, and then prints out commands to write itself to the memory buffer, and then writes itself out \$\endgroup\$ Commented Aug 26, 2016 at 18:46
  • \$\begingroup\$ From my point of view this is not a quine? \$\endgroup\$
    – Leaky Nun
    Commented Aug 26, 2016 at 18:55
  • \$\begingroup\$ @LeakyNun it's borked... let me fix... should I delete, fix and undelete? \$\endgroup\$ Commented Aug 26, 2016 at 18:58
7
\$\begingroup\$

ಠ_ಠ, 6 bytes

ಠಠ

This used to work back when the interpreter was still buggy but that's fixed now. However, you can try it in the legacy version of the interpreter!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ It seems that also works in the interpreter provided. Also, ಠ is 3 bytes in UTF-8 \$\endgroup\$ Commented Aug 19, 2016 at 21:53
  • \$\begingroup\$ Your link is dead, and can you upload the language source to GitHub so that it can be added to TIO? \$\endgroup\$
    – Pavel
    Commented Jan 30, 2017 at 4:28
  • \$\begingroup\$ codepen.io/molarmanful/pen/rxJmqx all the code happens to be there. \$\endgroup\$ Commented Jan 30, 2017 at 17:58
7
\$\begingroup\$

Husk, 8 bytes

S+s"S+s"

Try it online!

Husk is a new golfing functional language created by me and Zgarb. It is based on Haskell, but has an intelligent inferencer that can "guess" the intended meaning of functions used in a program based on their possible types.

Explanation

This is a quite simple program, composed by just three functions:

S is the S combinator from SKI (typed) combinator calculus: it takes two functions and a third value as arguments and applies the first function to the value and to the second function applied to that value (in code: S f g x = f x (g x)).

This gives us +"S+s"(s"S+s"). s stands for show, the Haskell function to convert something to a string: if show is applied to a string, special characters in the string are escaped and the whole string is wrapped in quotes.

We get then +"S+s""\"S+s\"". Here, + is string concatenation; it could also be numeric addition, but types wouldn't match so the other meaning is chosen by the inferencer.

Our result is then "S+s\"S+s\"", which is a string that gets printed simply as S+s"S+s".

\$\endgroup\$
7
\$\begingroup\$

Bash, 48 bytes

Q=\';q='echo "Q=\\$Q;q=$Q$q$Q;eval \$q"';eval $q

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Hm the leaderboard gives a shorter one, although that one uses sed while you are only using builtins. \$\endgroup\$ Commented Feb 7, 2018 at 17:14
  • \$\begingroup\$ After a search, I think this is currently the shortest with only builtins and a "normal" quine construction. \$\endgroup\$ Commented Feb 7, 2018 at 17:28
  • \$\begingroup\$ Thanks for taking a look @ØrjanJohansen! I'd like to differentiate this from the other solutions, but I don't know if I should change this title or the ones that use core utils... I'm happy with just coming up with the program to be honest! 😊 \$\endgroup\$ Commented Feb 7, 2018 at 19:22
  • \$\begingroup\$ Bash + coreutils seems to be fairly common, so I'd suggest to edit the header of the other answer. \$\endgroup\$
    – Laikoni
    Commented Feb 10, 2018 at 12:07
7
+100
\$\begingroup\$

Reflections, 1.81x10375 bytes

Or to be more accurate, 1807915590203341844429305353197790696509566500122529684898152779329215808774024592945687846574319976372141486620602238832625691964826524660034959965005782214063519831844201877682465421716887160572269094496883424760144353885803319534697097696032244637060648462957246689017512125938853808231760363803562240582599050626092031434403199296384297989898483105306069435021718135129945 bytes.

The relevant section of code is:

+#::(1   \/  \    /: 5;;\
          >v\>:\/:4#+     +\
     /+#   /   2 /4):_    ~/
     \ _   2:#_/ \  _(5#\ v#_\
         *(2 \;1^    ;;4) :54/
         \/ \    1^X    \_/

Where each line is preceeded by 451978897550835461107326338299447674127391625030632421224538194832303952193506148236421961643579994093035371655150559708156422991206631165008739991251445553515879957961050469420616355429221790143067273624220856190036088471450829883674274424008061159265162115739311672254378031484713452057940090950890560145649762656523007858600799824096074497474620776326517358755429533782443 spaces. The amount of spaces is a base 128 encoded version of the second part, with 0 printing all the spaces again.

Edit: H.PWiz points out that the interpreter probably doesn't support this large an integer, so this is all theoretical

How It Works:

+#::(1  Pushes the addition of the x,y coordinates (this is the extremely large number)
        Dupe the number a couple of times and push one of the copies to stack 1
            \
             >
                   Pushes a space to stack 2
            *(2
            \/


             /  \
             >v >:\
        /+#   /   2   Print space number times
        \ _   2:#_/


                #      Pop the extra 0
                \;1^   Switch to stack 1 and start the loop


                   /:4#+      +\
                    /4):_     ~/    Divide the current number by 128
                    \  _(5#\ v      Mod a copy by 128
                   ^      4) :
                           \_/


                             v#_\  If the number is not 0:
                   ^    ;;4) :54/  Print the number and re-enter the loop

                     /: 5;;\
             v\      4             If the number is 0:
                     4             Pop the excess 0
              :            \       And terminate if the divided number is 0
                                   Otherwise return to the space printing loop
              \     1^X

Conclusion: Can be golfed pretty easily, but maybe looking for a better encoding algorithm would be best. Unfortunately, there's basically no way to push an arbitrarily large number without going to that coordinate.

\$\endgroup\$
14
  • \$\begingroup\$ Does the interpreter support integers this large? \$\endgroup\$
    – H.PWiz
    Commented Mar 25, 2018 at 0:42
  • \$\begingroup\$ @H.PWiz, erm, probably not. The interpreter is written in JS, which has a max integer size of 2^53-1 \$\endgroup\$
    – Jo King
    Commented Mar 25, 2018 at 0:52
  • 1
    \$\begingroup\$ Nevermind support for large integers. Where and how are you going to store the file? :P \$\endgroup\$
    – Dennis
    Commented Mar 25, 2018 at 1:58
  • 2
    \$\begingroup\$ Now I really want to prove that this answer is highly suboptimal, but first I have to learn the language... \$\endgroup\$
    – user202729
    Commented Apr 1, 2018 at 13:52
  • 1
    \$\begingroup\$ Since V8 added support for BigInts, the interpreter integer limit should no longer be a problem - provided you can find a computer that can handle it. \$\endgroup\$
    – Etheryte
    Commented May 7, 2018 at 8:49
7
+500
\$\begingroup\$

Brain-Flak, 1805 bytes

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

Try it online!

-188 bytes by avoiding code duplication

Like Wheat Wizard's answer, I encode every closing bracket as 1. The assignment of numbers to the four opening brackets is chosen to minimize the total length of the quine:

2: ( - 63 instances
3: { - 41 instances
4: < - 24 instances
5: [ -  5 instances

The other major improvement over the old version is a shorter way to create the code points for the various bracket types.

The decoder builds the entire quine on the second stack, from the middle outward. Closing brackets that have yet to be used are stored below a 0 on the second stack. Here is a full explanation of an earlier version of the decoder:

# For each number n from the encoder:
{

 # Push () on second stack (with the opening bracket on top)
 <>(((((()()()()()){}){}){}())[()])<>

 # Store -n for later
 (([{}])

  # n times
  {<({}())

    # Replace ( with (()
    <>((({}())[()]))<>

  >}{}

  # Add 1 to -n
  ())

  # If n was not 1:
  ((){[()]<

     # Add 1 to 1-n
     (({}())<

       # Using existing 40, push 0, 91, 60, 123, and 40 in that order on first stack
       <>(({})<(([(({})())]((()()()()()){})({}{}({})(<>)))({})()()())>)

     # Push 2-n again
     >)

     # Pop n-2 entries from stack
     {({}()<{}>)}{}

     # Get opening bracket and clear remaining generated brackets
     (({}<{{}}>{})

      (<

        # Add 1 if n was 2; add 2 otherwise
        # This gives us the closing bracket
        ({}(){()(<{}>)}

         # Move second stack (down to the 0) to first stack temporarily and remove the zero
         <<>{({}<>)<>}{}>

        # Push closing bracket
        )

      # Push 0
      >)

     # Push opening bracket
     )

     # Move values back to second stack
     <>{({}<>)<>}

   # Else (i.e., if n = 1):
   >}{})

 {

  # Create temporary zero on first stack
  (<{}>)

  # Move second stack over
  <>{({}<>)<>}

  # Move 0 down one spot
  # If this would put 0 at the very bottom, just remove it
  {}({}{(<()>)})

  # Move second stack values back
  <>{({}<>)<>}}{}

}

# Move to second stack for output
<>
\$\endgroup\$
2
  • \$\begingroup\$ It looks like you have a stray newline at the end of your code. You can save a byte by removing it. \$\endgroup\$
    – 0 '
    Commented Dec 28, 2017 at 18:55
  • 6
    \$\begingroup\$ @0 ' Since Brain-Flak prints with a trailing newline. It is necessary for it to be a quine \$\endgroup\$
    – H.PWiz
    Commented Jan 19, 2018 at 18:32
7
\$\begingroup\$

APL (Dyalog Unicode), 18 bytesSBCS

@ngn's updated version of the classic APL quine, using a modern operator to save four bytes.

1⌽,⍨9⍴'''1⌽,⍨9⍴'''

Try APL!

'''1⌽,⍨9⍴''' the characters '1⌽,⍨9⍴'

9⍴ cyclically reshape to shape 9; '1⌽,⍨9⍴''

,⍨ concatenation selfie; '1⌽,⍨9⍴'''1⌽,⍨9⍴''

1⌽ cyclically rotate one character to the left; 1⌽,⍨9⍴'''1⌽,⍨9⍴'''

\$\endgroup\$
7
+250
\$\begingroup\$

Alchemist, 720 657 637 589 bytes

-68 bytes thanks to Nitrodon!

0n0n->1032277495984410008473317482709082716834303381684254553200866249636990941488983666019900274253616457803823281618684411320510311142825913359041514338427283749993903272329405501755383456706244811330910671378512874952277131061822871205085764018650085866697830216n4+Out_"0n0n->"+Out_n4+nn+n0n
4n0+4n0+n->Out_"n"
n+n+4n0+n0+n0+n0->Out_"+"
n+n+n+4n0+n0+n0->Out_n
4n+4n0+n0->Out_"4"
4n+n+4n0->Out_"\""
4n+n+n+n0+n0+n0->Out_"->"
4n+n+n+n+n0+n0->Out_"\n"
4n+4n+n0->Out_"Out_"
4n+4n+n->Out_"\\"
nn->4n0+4n0+nnn
n0+n4->n
nnn+4n+4n+n4->nn+n00
nnn+0n4->n+n40
n40+0n00+n4->n4+nn
n40+0n+n00->n4+n40

Try it online!

Warning, takes far longer than the lifetime of the universe to execute, mostly cause we have to transfer that rather large number on the first line back and forth between multiple atoms repeatedly. Here's a version that outputs the first few lines in a reasonable amount of time.

Here is the encoder that turns the program into the data section

Everything but the large number on the first line is encoded using these 8 9 tokens:

0 n + -> " \n Out_ \ 4

That's why all the atom names are composed of just n,0 and 4.

As a bonus, this is now fully deterministic in what order the rules are executed.

Explanation:

Initialise the program
0n0n->        If no n0n atom (note we can't use _-> since _ isn't a token)
      n4+Out_"0n0n->"+Out_n4+nn4+n0n
      NUMn4           Create a really large number of n4 atoms  
      +Out_"0n0n->"   Print the leading "0n0n->"
      +Out_n4         Print the really large number
      +nn             Set the nn flag to start getting the next character
      +n0n            And prevent this rule from being called again


Divmod the number by 9 (nn and nnn flag)
nn->4n0+4n0+nnn          Convert the nn flag to 8 n0 atoms and the nnn flag
n0+n4->n                 Convert n4+n0 atoms to an n atom
nnn+4n+4n+n4->nn+n00     When we're out of n0 atoms, move back to the nn flag
                         And increment the number of n00 atoms
nnn+0n4->n+n40           When we're out of n4 atoms, add another n atom and set the n40 flag


Convert the 9 possible states of the n0 and n atoms to a token and output it (nn flag)
n+4n0+4n0->Out_"n"           1n+8n0 -> 'n'
n+n+4n0+n0+n0+n0->Out_"+"    2n+7n0 -> '+'
n+n+n+4n0+n0+n0->Out_n       3n+6n0 -> '0'
4n+4n0+n0->Out_"4"           4n+5n0 -> '4'
4n+n+4n0->Out_"\""           5n+4n0 -> '"'
4n+n+n+n0+n0+n0->Out_"->"    6n+3n0 -> '->'
4n+n+n+n+n0+n0->Out_"\n"     7n+2n0 -> '\n'
4n+4n+n0->Out_"Out_"         8n+1n0 -> 'Out_'
4n+4n+n->Out_"\\"            9n+0n0 -> '\'

Reset (n40 flag)
n40+0nn+n00->n4+n40    Convert all the n00 atoms back to n4 atoms
n40+0n00+n4->n4+nn     Once we're out of n00 atoms set the nn flag to start the divmod
\$\endgroup\$
0
7
\$\begingroup\$

Brian & Chuck, 211 143 138 133 129 98 86 84 bytes

?.21@@/BC1@c/@/C1112BC1BB/@c22B2%C@!{<?
!.>.._{<+>>-?>.---?+<+_{<-?>+<<-.+?ÿ

Try it online!

old version:

?{<^?_>{_;?_,<_-+_;._;}_^-_;{_^?_z<_>>_->_->_*}_-<_^._=+_->_->_->_-!_	?_;}_^_}<?
!.>.>.>.+>._<.}+>.>.>?<{?_{<-_}<.<+.<-?<{??`?=

Try it online!

New code. Now the data isn't split into nul separated chunks, but the nul will be pulled through the data.

Brian:
?                   start Chuck

.21@@/BC1@c/@/C1112BC1BB/@c22B2%C@!
                    data. This is basically the end of the Brian code and the Chuck code reversed 
                    and incremented by four. This must be done because the interpreter tries
                    to run the data, so it must not contain runnable characters

                    ASCII 3 for marking the end of the code section
{<?                 loop the current code portion of Chuck

Chuck:
code 1 (print start of Brian code)
.>..               print the first 3 characters of Brian

code 2 (print the data section)
{<+>>-              increment char left to null and decrement symbol right to null
                    for the first char, this increments the question mark and decrements the
                    ASCII 1. So the question mark can be reused in the end of the Chuck code
?>.                 if it became nul then print the next character
---?+<+             if the character is ASCII 3, then the data section is printed.
                    set it 1, and set the next char to the left 1, too

code 3 (extract code from data)
{<-                 decrement the symbol left to the nul
?>+<<-.             if it became nul then it is the new code section marker, so set the old one 1
                    and print the next character to the left
+?                  if all data was processed, then the pointer can't go further to the left
                    so the char 255 is printed. If you add 1, it will be null and the code ends.
ÿ                   ASCII 255 that is printed when the end of the data is reached
\$\endgroup\$
7
\$\begingroup\$

Klein, 330 bytes

"![	.;	=90*/[	.9(#;	=[>[.	>1#	98='9[	'7[.>;	[*;	)	=#0,*[	=.>9(.	=*(#(#([	.0#8;#(#;	[*9>[;	=> [*?	[9(;;"\
/																																																																																																														<
>:1+0\
 /:)$<
>\?\ /
?2 $
:9>(:\
(8\/?<
\+ <
 *
 >$1-+\
>/?:) /
 >+)$)$)\
/1$9<$)$<
\+:?\<
>?!\+@
\:)<<

Try it online!

This works in all topologies, mostly by completely avoiding wrapping. The first list encodes the rest of the program offset by one, so newlines are the unprintable (11).

\$\endgroup\$
7
+50
\$\begingroup\$

Grok, 42 bytes

iIilWY!}I96PWwI10WwwwIkWwwwIhWq``
   k   h

Try it Online!

It is very costly to output anything in Grok it seems.

\$\endgroup\$
7
\$\begingroup\$

!@#$%^&*()_+, 1128 1116 1069 960 877 844 540 477 407 383 33 bytes

Edit: Woah... -304 B with space

Edit 2: Bruh.

Edit 3: Thanks Jo King for the idea! I outgolfed ya!

A stack-based language(The first on TIO's list!)

It's a big pile of unprintables though

N0N  (!&+$*)^(!&@^)!

(Spaces are NUL bytes)

Try it online!

Here's the code, but in Control Pictures form:

␙N0␖␑␘N␙␚␔␛␀␖␑␘␀␐(!&␐+$*)^(!&@^)!

Explanation

␙N0␖␑␘N␙␚␔␛␀␖␑␘␀␐                 Data
                 (!&␐+$*)         Push the stack, reversed and +16 back on top
                         ^(!&@^)! Print everything reversed, including the length (Hence the final `!`)

It does error on overflow though...

\$\endgroup\$
4
  • \$\begingroup\$ Will it get to BF length? I wonder... \$\endgroup\$
    – SuperPizz
    Commented Apr 18, 2022 at 11:44
  • \$\begingroup\$ Almost there... \$\endgroup\$
    – SuperPizz
    Commented Apr 18, 2022 at 12:02
  • 2
    \$\begingroup\$ just as a note, i have posted a quine in this language before, though don't let that stop you improving this further \$\endgroup\$
    – Jo King
    Commented Apr 18, 2022 at 13:27
  • \$\begingroup\$ SO messed up the unprintables \$\endgroup\$
    – SuperPizz
    Commented Apr 18, 2022 at 18:34
6
\$\begingroup\$

Commodore Basic, 54 41 characters

1R─A$:?A$C|(34)A$:D♠"1R─A$:?A$C|(34)A$:D♠

Based on DLosc's QBasic quine, but modified to take advantage of Commodore Basic's shortcut forms. In particular, the shorter version of CHR$(34) makes using it directly for quotation marks more efficient than defining it as a variable.

As usual, I've made substitutions for PETSCII characters that don't appear in Unicode: = SHIFT+A, = SHIFT+E, | = SHIFT+H.

Edit: You know what? If a string literal ends at the end of a line, the Commodore Basic interpreter will let you leave out the trailing quotation mark. Golfed off 13 characters.

Alternatively, if you want to skirt the spirit of the rules,

1 LIST

LIST is an instruction that prints the current program's code. It is intended for use in immediate mode, but like all immediate-mode commands, it can be used in a program (eg. 1 NEW is a self-deleting program). Nothing shorter is possible: dropped spaces or abbreviated forms get expanded by the interpreter and displayed at full length.

\$\endgroup\$
5
  • \$\begingroup\$ Its not opening the file and reading its input, but I do agree that this is a bit cheaty. \$\endgroup\$
    – yyny
    Commented Feb 26, 2015 at 21:34
  • \$\begingroup\$ @MartinBüttner, is the new version better? \$\endgroup\$
    – Mark
    Commented Feb 27, 2015 at 7:53
  • \$\begingroup\$ @Mark I can't read that, but it looks like a quine to me. ;) \$\endgroup\$ Commented Feb 27, 2015 at 9:06
  • \$\begingroup\$ @YoYoYonnY It reads its source, tho. \$\endgroup\$ Commented Jun 28, 2016 at 21:54
  • \$\begingroup\$ This is is no way cheating. Don't be confused by the fact that the statement is named READ; it's purely a way of initializing variables from values found elsewhere in the source code. No I/O is being performed; READ A:DATA 1 is just a long way of writing A=1 that lets the A and 1 not be next to each other. It's mainly there because there's no composite literal syntax in the language. \$\endgroup\$
    – Mark Reed
    Commented Nov 29, 2022 at 19:53
6
\$\begingroup\$

Jolf, 4 bytes

Q«Q«
Q    double (string)
 «   begin matched string
  Q« capture that

This transpiles to square(`Q«`) (I accidentally did string doubling in the square function), which evaluates to Q«Q«. Note that q is the quining function in Jolf, not Q ;).

\$\endgroup\$
6
\$\begingroup\$

Vitsy, 11 9 8 6 Bytes

This programming language was obviously made past the date of release for this question, but I thought I'd post an answer so I can a) get more used to it and b) figure out what else needed to be implemented.

'rd3*Z

The explanation is as follows:

'rd3*Z
'           Start recording as a string.

(wraps around once, capturing all the items)

'           Stop recording as a string. We now have everything recorded but the original ".
 r          Reverse the stack
  b3*       This equates the number 39 = 13*3 (in ASCII, ')
     Z      Output the entire stack.
\$\endgroup\$
6
\$\begingroup\$

Fuzzy Octo Guacamole, 4 bytes

_UNK

I am not kidding. Due to a suggestion by @ConorO'Brien, K prints _UNK. The _UN does nothing really, but actually sets the temp var to 0, pushes 0, and pushes None.

The K prints "_UNK", and that is our quine.

\$\endgroup\$
1
  • \$\begingroup\$ I was bored today and decided to quine in FOG. Couldn't figure it out, this is very clever \$\endgroup\$ Commented Jun 26, 2016 at 19:43
6
\$\begingroup\$

C++, 286 284 236 bytes

Now with extra golf!

#include<iostream>
int main(){char a[]="#include<iostream>%sint main(){char a[]=%s%s%s,b[]=%s%s%s%s,c[]=%s%sn%s,d[]=%s%s%s%s;printf(a,c,b,a,b,b,d,b,b,b,d,b,b,d,d,b);}",b[]="\"",c[]="\n",d[]="\\";printf(a,c,b,a,b,b,d,b,b,b,d,b,b,d,d,b);}

I'm currently learning C++, and thought "Hey, I should make a quine in it to see how much I know!" 40 minutes later, I have this, a full 64 114 bytes shorter than the current one. I compiled it as:

g++ quine.cpp

Output and running:

C:\Users\Conor O'Brien\Documents\Programming\cpp
λ g++ quine.cpp & a
#include<iostream>
int main(){char a[]="#include<iostream>%sint main(){char a[]=%s%s%s,b[]=%s%s%s%s,c[]=%s%sn%s,d[]=%s%s%s%s;printf(a,c,b,a,b,b,d,b,b,b,d,b,b,d,d,b);}",b[]="\"",c[]="\n",d[]="\\";printf(a,c,b,a,b,b,d,b,b,b,d,b,b,d,d,b);}
\$\endgroup\$
6
\$\begingroup\$

Cheddar, 56 bytes

var a='var a=%s;print a%@"39+a+@"39';print a%@"39+a+@"39

Try it online!

I felt like trying to make something in Cheddar today, and this is what appeared...

\$\endgroup\$
6
\$\begingroup\$

05AB1E, 16 17 bytes

"34çs«DJ"34çs«DJ

With trailing newline.

Try it online!

Explanation:

"34çs«DJ"        # push string
         34ç     # push "
            s«   # swap and concatenate
              DJ # duplicate and concatenate
\$\endgroup\$
0
6
\$\begingroup\$

05AB1E, 19 bytes

Thanks to @Oliver for a correction (trailing newline)

"D34ç.øsJ"D34ç.øsJ

There is a trailing newline.

Try it online!

"D34ç.øsJ"             Push this string
          D            Duplicate
           34          Push 34 (ASCII for double quote mark)
             ç         Convert to char
              .ø       Surround the string with quotes
                s      Swap
                 J     Join. Implicitly display
\$\endgroup\$
4
  • \$\begingroup\$ Try it online! - codegolf.stackexchange.com/a/161069/59376 - If you want to post this 22 byte answer on this question, you can :). Don't feel I deserve the credit. \$\endgroup\$ Commented Apr 2, 2018 at 17:47
  • \$\begingroup\$ Probably wasn't available in the old version of 05AB1E yet when you posted your answer, but you can golf 2 bytes by changing sJ (swap, join) to ì (prepend). Try it online. \$\endgroup\$ Commented Sep 7, 2018 at 12:17
  • \$\begingroup\$ @KevinCruijssen Thanks. I like to keep the language as it was before the challenge (even though that's not required anymore), so I'll leave it as it is \$\endgroup\$
    – Luis Mendo
    Commented Sep 7, 2018 at 13:21
  • 1
    \$\begingroup\$ @LuisMendo Fine by me. There are already two shorter 05AB1E answers anyway. Was just stating it as a possibility. :) \$\endgroup\$ Commented Sep 7, 2018 at 13:24
6
\$\begingroup\$

Clojure, 91 bytes

((fn [x] (list x (list (quote quote) x))) (quote (fn [x] (list x (list (quote quote) x)))))
\$\endgroup\$
6
\$\begingroup\$

Mathematica, 68 bytes

Print[#<>ToString[#,InputForm]]&@"Print[#<>ToString[#,InputForm]]&@"
\$\endgroup\$
1 2 3
4
5
16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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