244
\$\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
  • 27
    \$\begingroup\$ Did anybody notice that this is question 69? \$\endgroup\$
    – aidan0626
    Commented Oct 24, 2020 at 22:47

456 Answers 456

1
3 4
5
6 7
16
6
\$\begingroup\$

Brachylog (2), 26 bytes, language postdates challenge

"ạ~bAh34∧A~ạj"ạ~bAh34∧A~ạj

Try it online!

A function that returns its own source code. (This can be made into a 28-byte full program by adding w after each occurrence of j.)

Explanation

"ạ~bAh34∧A~ạj"ạ~bAh34∧A~ạj
"ạ~bAh34∧A~ạj"               String literal
              ạ              Convert to list of character codes
               ~b            Prepend an element
                  h34          so that the first element is 34
                 A   ∧A        but work with the entire list
                       ~ạ    Convert to string
                         j   Concatenate the string to itself
\$\endgroup\$
6
\$\begingroup\$

C (gcc), 78 70 66 62 bytes

Minus 4 bytes thanks to MD XF (reusing first argument of printf)!

There are a few unprintables in this answer, replaced with ?.

main(){printf("main(){printf(%c%s%1$c,34,'@??');}",34,'@??');}

Here's an xxd:

00000000: 6d61 696e 2829 7b70 7269 6e74 6628 226d  main(){printf("m
00000010: 6169 6e28 297b 7072 696e 7466 2825 6325  ain(){printf(%c%
00000020: 7325 3124 632c 3334 2c27 4005 9027 293b  s%1$c,34,'@..');
00000030: 7d22 2c33 342c 2740 0590 2729 3b7d       }",34,'@..');}

Here's a bash script to generate and execute the program.

62 bytes, part 2

Here's a version that I have tested on my windows machine on gcc (ANSI encoded):

main(){printf("main(){printf(%c%s%1$c,34,'@@0');}",34,'@@0');}

Here's the output:

C:\Users\Conor O'Brien\Documents
λ xxd test.c
00000000: 6d61 696e 2829 7b70 7269 6e74 6628 226d  main(){printf("m
00000010: 6169 6e28 297b 7072 696e 7466 2825 6325  ain(){printf(%c%
00000020: 7325 3124 632c 3334 2c27 4040 3027 293b  s%1$c,34,'@@0');
00000030: 7d22 2c33 342c 2740 4030 2729 3b7d       }",34,'@@0');}

C:\Users\Conor O'Brien\Documents
λ cat test.c
main(){printf("main(){printf(%c%s%1$c,34,'@@0');}",34,'@@0');}
C:\Users\Conor O'Brien\Documents
λ wc test.c -c
62 test.c

C:\Users\Conor O'Brien\Documents
λ gcc test.c -o test
test.c:1:1: warning: return type defaults to 'int' [-Wimplicit-int]
 main(){printf("main(){printf(%c%s%1$c,34,'@@0');}",34,'@@0');}
 ^
test.c: In function 'main':
test.c:1:8: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
 main(){printf("main(){printf(%c%s%1$c,34,'@@0');}",34,'@@0');}
        ^
test.c:1:8: warning: incompatible implicit declaration of built-in function 'printf'
test.c:1:8: note: include '<stdio.h>' or provide a declaration of 'printf'
test.c:1:55: warning: multi-character character constant [-Wmultichar]
 main(){printf("main(){printf(%c%s%1$c,34,'@@0');}",34,'@@0');}
                                                       ^

C:\Users\Conor O'Brien\Documents
λ test
main(){printf("main(){printf(%c%s%1$c,34,'@@0');}$c,34,'@@0');}
C:\Users\Conor O'Brien\Documents
λ

66 bytes

main(){printf("main(){printf(%c%s%1$c,34,4195728);}",34,4195728);}

I have no idea why this works, 100% honest here. But dang, is it short. Only 6 bytes longer than the current best.

Try it online!

70 bytes

main(){printf("main(){printf(%c%s%c,34,4195728,34);}",34,4195728,34);}

Try it online!

78 bytes

main(){printf("main(){printf(%c%s%c,34,%c%c+8,34,34,34);}",34,""+8,34,34,34);}

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ 66 bytes \$\endgroup\$
    – MD XF
    Commented Jul 25, 2017 at 4:46
  • \$\begingroup\$ It works because printf is very weakly typed, so your integer constant gets interpreted as the actual pointer address of the format string constant. Very implementation-dependent, I tried it on a different Linux machine and even there I needed to adjust the numbers. \$\endgroup\$ Commented Jul 25, 2017 at 6:21
  • \$\begingroup\$ @ØrjanJohansen Thanks, I figured it had something to do with addressing. \$\endgroup\$ Commented Jul 25, 2017 at 16:39
  • \$\begingroup\$ Aha, you found out how to get it down to 62 non-locally! \$\endgroup\$
    – MD XF
    Commented Jul 25, 2017 at 17:03
  • \$\begingroup\$ @MDXF Yes I did :> \$\endgroup\$ Commented Jul 25, 2017 at 17:15
6
\$\begingroup\$

Taxi, 1144 1034 970 bytes

"is waiting at Writer's Depot.34 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 r 3 l 2 l 3 l 2 r.Pickup a passenger going to Charboil Grill.Go to Charboil Grill:e 1 l.Pickup a passenger going to KonKat's.Go to Writer's Depot:w 1 r.Pickup a passenger going to KonKat's.Go to KonKat's:n 3 r 2 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 2 l.Pickup a passenger going to Post Office.Pickup a passenger going to Post Office.Go to Post Office:s 1 l 2 r 1 l."is waiting at Writer's Depot.34 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 r 3 l 2 l 3 l 2 r.Pickup a passenger going to Charboil Grill.Go to Charboil Grill:e 1 l.Pickup a passenger going to KonKat's.Go to Writer's Depot:w 1 r.Pickup a passenger going to KonKat's.Go to KonKat's:n 3 r 2 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 2 l.Pickup a passenger going to Post Office.Pickup a passenger going to Post Office.Go to Post Office:s 1 l 2 r 1 l.

Please ignore the output to stderr. Who needs a job if you can quine anyway?

Try it online!

How does this work?

Short answer

This quine starts with a string. If you replace the content of that string by <string>, the code looks like "<string>"<string>, which is "<string> twice. Because the string doesn't contain the double quote, we first get the double quote via its character code, concatenate it with the string, then copy the string and concatenate it with itself. Finally, we print the string.

Long answer

under construction

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

JavaScript (ES6), 28 26 bytes

Run this code in Firefox 34+ (currently in Aurora)'s Web console

(f=x=>alert`(f=${f})()`)()
\$\endgroup\$
2
  • \$\begingroup\$ @rafe-ketler - I believe that this is the shortest ES6 version now :) \$\endgroup\$
    – Optimizer
    Commented Sep 13, 2014 at 22:50
  • \$\begingroup\$ Hmm... doesn't work in the latest version of Firefox (it outputs (f=,)()), I believe you'd have to put the template in parentheses. \$\endgroup\$ Commented Aug 11, 2017 at 19:34
6
\$\begingroup\$

Implicit, 20 bytes

«@171%@187»@171%@187

This didn't work in older versions of Implicit.

Try it online!

How it works

«@171%@187»           Push the string '@171%@187' on the stack. Let's call it s.
           @171       Print '«' (char code 171), without pushing it on the stack.
               %      Print s without popping it from the stack.
                @187  Print '»' (char code 171), without pushing it on the stack.
                      (implicit) Print the top of the stack: s.

Implicit, 26 bytes

«:171,::187,"»:171,::187,"

Try it online!

How it works

«:171,::187,"»              Push the string ':171,::187,"' on the stack.
                            Let's call it s.
              :171          Push 171 (code point of «).
                  ,         Swap s and 171.
                   :        Push a copy of s.
                    :187    Push 187 (code point of »).
                        ,   Swap the copy of s and ».
                         "  Combine the entire stack into a string.
\$\endgroup\$
1
  • 2
    \$\begingroup\$ +1, got the same solution not long after. \$\endgroup\$
    – ATaco
    Commented Sep 22, 2017 at 2:31
6
\$\begingroup\$

Bash + coreutils, 18 bytes

sed p<<a
sed p<<a

It requires a trailing newline and generates a warning.

Posted the Zsh version in a separate answer to fix the leaderboard.

\$\endgroup\$
5
  • \$\begingroup\$ Interesting use of the here-document, but one has to end the text stream manually with Ctrl+D to make the script run, hence the warning. To have it run automatically, an extra line with just a on it is required, but that would break the quine. \$\endgroup\$
    – seshoumara
    Commented Sep 7, 2016 at 8:53
  • \$\begingroup\$ @seshoumara Just put it in a script file and use bash filename to run. \$\endgroup\$
    – jimmy23013
    Commented Sep 7, 2016 at 9:22
  • \$\begingroup\$ Aah, you give an EOF this way, nice. Maybe add that to description. I run it with bash script 2> /dev/null to get rid of STDERR. \$\endgroup\$
    – seshoumara
    Commented Sep 7, 2016 at 9:31
  • \$\begingroup\$ In dash and zsh, you don't need the trailing newline and it won't generate a warning. \$\endgroup\$
    – Dennis
    Commented Sep 8, 2016 at 15:49
  • 1
    \$\begingroup\$ @seshoumara Answers on this site should be functions or complete programs, and not code snippets in a REPL, by default. \$\endgroup\$
    – jimmy23013
    Commented Sep 8, 2016 at 21:12
6
\$\begingroup\$

Underload, 10 Bytes

(:aSS):aSS

Try it online!

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

Perl 6, 31 27 bytes

<"<$_>~~.EVAL".say>~~.EVAL

Try it online!

No messing about with alternative q quotes or .perl, just <> and a good ol' EVAL quine.

Explanation:

<                 >         # Create a list of 
 "<$_>~~.EVAL".say          # The string '"<$_>~~.EVAL".say'
                   ~~       # Smartmatch the list by setting $_ to it
                     .EVAL  # Evaluate the string as code
 "<$_>~~.EVAL"              # Interpolate the $_ list into the string
              .say          # And print it with a newline
\$\endgroup\$
6
\$\begingroup\$

1+, 5424 4978 4808 4112 3962 3748 bytes

11+1+1+1+1+1+11+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+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+1+1+1+1+1+11+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+11+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+111+1+1+1+1+1+1+1+1+1+1+1+1+1+1+11+1+1+1+1+1+1+1+1+11+1+1+1+1+1+1+1+1+1+1+1+1+1+1+11+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+11+11+1+1+1+1+1+1+(|11+1<)\(1|1""+""*++"*;)($|1+11#(1)1""+""*+""*++;1#1+"//"\^\<11+*#()*+\)(%|()#(1)($)"1+1<#)\(&|()#11+"*"*"++;\"1+1<#)

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Congratulations! You beat me to the punch. Nice work!! \$\endgroup\$ Commented Oct 6, 2019 at 1:42
6
\$\begingroup\$

Pyth, 9 8 bytes

p+N
"p+N

Saved one more byte by using the newline operator

(Also I made this ages ago but forgot to edit this so yeah)

Try it online!

\$\endgroup\$
6
+100
\$\begingroup\$

Whispers v2, 38 bytes

> "print('> %r\\n>> ⍎1'%a)"
>> ⍎1

Try it online!

Abuses the fact that there's an eval as Python command (), which I can use to turn it into an arbitrary Python program. But of course, as a less cheaty feeling quine, there's:

Whispers v2, 270 bytes

> [62, 62, 32, 34, 49, 34, 10, 62, 32, 34, 62, 32, 34, 10, 62, 62, 32, 51, 43, 50, 10, 62, 62, 32, 69, 97, 99, 104, 32, 54, 32, 49, 10, 62, 62, 32, 39, 82, 10, 62, 62, 32, 79, 117, 116, 112, 117, 116, 32, 52, 32, 53]
>> "1"
> "> "
>> 3+2
>> Each 6 1
>> 'R
>> Output 4 5

Try it online!

Which encodes the ordinal values of the rest of the program on the first line, then prints the list then the list converted to characters.

Turns out I missed another way to make a slightly cheaty quine. In Whispers, the modulo command directly calls Python's %, which is overloaded with string formatting. This means you can forgo the eval command and do:

Whispers v2, 50 bytes

> '> %r\n>> 1%%1\n>> Output 2'
>> 1%1
>> Output 2

Try it online!

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

beeswax, 17 13 bytes

According to the discussion on Does using SMBF count as a cheating quine? the original version at the bottom would count as a cheating quine, so I am wondering if a small change would make this a “proper” quine. The new version is 4 bytes smaller and does not modify its own source code:

`_4~++~+.}1fJ

Explanation:

                lstack     STDOUT

 _             α[0,0,0]•                 create bees α,β, moving right and left
               β[0,0,0]•

` 4            α[0,0,4]•                 push 4 on top of α lstack, switch β to print mode
β α            β[0,0,0]•                 switch β to character output mode

   ~           α[0,4,0]•                 flip α lstack top and 2nd
    +          α[0,4,4]•                 lstack top = top+2nd
     +         α[0,4,8]•                 lstack top = top+2nd
      ~        α[0,8,4]•                 flip lstack top and 2nd
       +       α[0,8,12]•                lstack top = top+2nd
        .      α[0,8,96]•                lstack top = top*2nd
         }     α[0,8,96]•    ` ASCII(96) output char(lstack top) to STDOUT
          1    α[0,8,1]•                 lstack top = 1
           F   α[1,1,1]•                 all lstack = top
            J  α[1,1,1]•                 jump to (x,y) = (lstack top, lstack 2nd)
`_4~++~+.}1FJ  α[1,1,1]•   _4~++~+.}1FJ  output characters to STDOUT

This version should qualify as proper quine if the Befunge-93 program on Thompson’s Quine Page is listed as proper quine. The Befunge quine below does nothing else than read itself character by character, one character during each implicit loop, and output the character to STDOUT.

:0g,:93+`#@_1+

Correct me if I’m wrong.


Old (cheating?) version.

beeswax is a new 2D esolang on a hexagonal grid. It is inspired by bees, honeycombs and by the Hive board game (which uses hexagonal gaming pieces). beeswax programs are able to modify their own code. Thanks to this ability it is not too hard to create a quine. But the program does not read its own source code, as my explanation shows.

The first beeswax quine in existence:

_4~++~+.@1~0@D@1J

Or equivalently:

*4~++~+.@1~0@D@1J

IPs are called bees, the program area is called honeycomb. Every bee owns a local stack called lstack, carrying 3 unsigned 64 bit integer values.

Explanation:

                                             lstack
                                     • marks top of stack

* or _  create bee(same result in this situation)[0,0,0]•
 4      1st lstack value=4                       [0,0,4]•
  ~        flip 1st/2nd lstack values            [0,4,0]•
   ++      1st=1st+2nd, twice                    [0,4,8]•
     ~                                           [0,8,4]•
      +                                          [0,8,12]•
       .         1st=1st*2nd                     [0,8,96]•
        @  flip 1st/3rd lstack values            [96,8,0]• 
         1     1st=1                             [96,8,1]•
          ~                                      [96,1,8]•
           0   1st=0                             [96,1,0]•
            @                                    [0,1,96]•
             D drop 1st at row=2nd,col.=3rd val. [0,1,96]•
       This drops ASCII(96)= ` beyond the left border.

Dropping a value at a coordinate outside the program—in this case at column 0—grows the honeycomb by 1 column to the left. The coordinate system gets reset, so this column becomes the new column 1. So, growing the honeycomb in ‘negative’ direction is only possible in steps of 1. The grown honeycomb is always a rectangle encompassing all code.

This modifies the program to:

`*4~++~+.@1~0@D@1J

continuing...

               @                                  [96,1,0]•
                1                                 [96,1,1]•
                 J jump to row=1st,column=2nd val.[96,1,1]•
`                  switch to character output mode.
 *4~++~+.@1~0@D@1J    the following characters are printed to STDOUT.

GitHub repository of the Julia package of the beeswax interpreter.

\$\endgroup\$
1
  • \$\begingroup\$ You can generate 96 using 5~3(. Try it online! (TIO still has the bug with the endof vs lastindex) \$\endgroup\$
    – Jo King
    Commented Sep 26, 2020 at 23:22
6
\$\begingroup\$

convey, 54 bytes

'Z  u   u%+}12  !*]+|   |0]!&[&&Z'[
v
v&,~23
"+^,}
}1^"'\''[

Try it online!

As a bonus, I even managed to avoid using any unprintable characters, though there are a few tabs in there.

Explanation

Here's an image of how the code is interpreted (the gif version would take too long, since it takes approximately 23*26=598 steps):

quine

There are two starting conveyor belts here, indicated by the [s on the top and bottom lines. The one on the top line feeds the data string to the rest of the program, while the other one prints the leading ' for the string and adds the other quote to the buffered output.

The top row is fed through a duplicator ("), one side of which is just pushed to output, while the other one has each character incremented by one (+1) to be transformed into the rest of the program before entering a queue (&), which already contains a '. Each character in this queue is delayed by 23 steps (~23), so that it only starts printing after the initial data string has finished being printed.

\$\endgroup\$
6
+100
\$\begingroup\$

Javastack, 64 bytes

"34 char swap add 2 repeat print"34 char swap add 2 repeat print

No TIO yet, watch this space!

Javastack aims to combine the verbosity of Java with the stack-based paradigm to create a truly horrible programming experience. I'm planning to make up for the verbosity with a truly immense library of functions, but it's all a work in progress.

Javastack doesn't have any way to get the character " aside from 34 char, so that's what we start with. We prepend it to the string currently on the stack (which is part of the code) with swap add, double it with 2 repeat, and print it, resulting in the source code.

-12 thanks to Aaron Miller - outgolfing me in my own language!

\$\endgroup\$
7
  • 5
    \$\begingroup\$ Doesn't look too verbose compared to Forth or Factor :P \$\endgroup\$
    – Bubbler
    Commented Aug 5, 2021 at 9:06
  • 2
    \$\begingroup\$ Not that Java if everything doesn't have to be inside a class, you don't have to deal with any qualified names, and you don't have to declare the types of... stack positions? I'm going mad even trying to think about that last point \$\endgroup\$ Commented Aug 5, 2021 at 9:08
  • 3
    \$\begingroup\$ @UnrelatedString Ooh, good idea! \$\endgroup\$
    – emanresu A
    Commented Aug 5, 2021 at 9:09
  • 3
    \$\begingroup\$ "I'm planning to make up for the verbosity with a truly immense library of functions." Sounds more like Factor than Java to me :P \$\endgroup\$
    – user
    Commented Aug 5, 2021 at 15:06
  • \$\begingroup\$ You can get rid of the space before each 34 char for 74 bytes: "34 char swap add duplicate add print"34 char swap add duplicate add print \$\endgroup\$ Commented Aug 5, 2021 at 16:01
6
\$\begingroup\$

Headascii, 41821869 bytes

+OU+++++++++OU++++++++++++++OU+OU+OU+OU+OU+OU++++++++++OU++++OU++++OU++++OU++++OU++++++++++OU++++OU++++OU++++++++++OU+++++++OU++OU+++OU+++++++OU+++++++++++++++OU+++++OU++++OU++++OU+++++++++++++OU+++++++++++OU+OU+OU+OU+OU+OU+OU+++++++++++OU++++++OU++++OU+OU+OU+OU+++++++++++OU++++++++++++++++OU+++++++++++++OU++++++++OU++++++++++++OU+++++++++++++++++OU+++++OU++++++++OU++++++OU+OU+OU+++++++++OU++++++++++++++OU++OU+++++OU++++++++++++++++++++OU+++++++++++++OU+++++++++OU++++++OU++++++++++++++++++OU++++++++++OU+++++++OU++OU+++OU+++++OU++++++OU++++++++OU++++++++++++OU+++++++OU++OU+++OU++++++++++++++++OU+OU+++++++++++++++OU+++++++++++++++++++OU++++OU+++++OU++++++++++++++++++OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+++++++++++OU++++++OU++++++++OU++++++++++++OU+++++++OU+++++++++++++++++OU+++++++++++++++++++OU+++++OU++OU+++OU++++++OU+OU+++++++++OU++++++++++++OU++++++++OUOU++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++OU++++++++OU+++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++++++++++++++OU+++++++++++++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++OU++++++++++++OU+++++++++++++OU+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++OU+++++++++++++++++++++++++++++++++++OU+++++++++++++++++++++++++++++++++++++++++++++OU+++++++++++++++++++++++++++++++++++++++++++++++++OU+++++++OUOU+E.+++++[]]]][]][{UO{^)]]-P++++++P:]+++PD-};N)}:++E.U)!-E:R[{UO):};{UOD+^(])R+++++++++++++++++++++++++++++++++P:};{N()UO:+E;}

Unfortunately there isn't a fancy TIO style interpreter for Headascii, just the same old replit hosted one. you can run that here by running

erun("+OU+++++++++OU++++++++++++++OU+OU+OU+OU+OU+OU++++++++++OU++++OU++++OU++++OU++++OU++++++++++OU++++OU++++OU++++++++++OU+++++++OU++OU+++OU+++++++OU+++++++++++++++OU+++++OU++++OU++++OU+++++++++++++OU+++++++++++OU+OU+OU+OU+OU+OU+OU+++++++++++OU++++++OU++++OU+OU+OU+OU+++++++++++OU++++++++++++++++OU+++++++++++++OU++++++++OU++++++++++++OU+++++++++++++++++OU+++++OU++++++++OU++++++OU+OU+OU+++++++++OU++++++++++++++OU++OU+++++OU++++++++++++++++++++OU+++++++++++++OU+++++++++OU++++++OU++++++++++++++++++OU++++++++++OU+++++++OU++OU+++OU+++++OU++++++OU++++++++OU++++++++++++OU+++++++OU++OU+++OU++++++++++++++++OU+OU+++++++++++++++OU+++++++++++++++++++OU++++OU+++++OU++++++++++++++++++OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+OU+++++++++++OU++++++OU++++++++OU++++++++++++OU+++++++OU+++++++++++++++++OU+++++++++++++++++++OU+++++OU++OU+++OU++++++OU+OU+++++++++OU++++++++++++OU++++++++OUOU++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++OU++++++++OU+++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++++++++++++++++++++++++++++++++++OU+++++++++++++++++++++++++++++++++++++++++++++++OU++++++++++++++++++++++++++OU++++++++++++OU+++++++++++++OU+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++OU+++++++++++++++++++++++++++++++++++OU+++++++++++++++++++++++++++++++++++++++++++++OU+++++++++++++++++++++++++++++++++++++++++++++++++OU+++++++OUOU+E.+++++[]]]][]][{UO{^)]]-P++++++P:]+++PD-};N)}:++E.U)!-E:R[{UO):};{UOD+^(])R+++++++++++++++++++++++++++++++++P:};{N()UO:+E;}")

Alternatively, Try It In Headass!, works the same way but outputs the character codes rather than the actual characters, which I personally deem unacceptable but whatever :P

Explanation

It's basically just the vanilla Encode, Print Encoding, Print Decoding style quine. Still on the hunt for a more clever trick to do it shorter but I thought this would be fun to start as a benchmark lol.

EDIT: was able to save quite a few (2313) bytes by writing a program to (mildly) optimize which characters were assigned which values, changed up the structure of code block 2 to have a loop (as discovered in previous revision), and removing a constant amount from each of the ascii values in the encoding and adding it back on in decoding later instead.

+OU…UOU+OU…+E.  code block 0 (heavily abridged / altered)
                (this block handles encoding)
                encode part 1: many of the following
+                 n +s: increment r0 by n, 1<=n<=20
 O                enqueue r0
  U               set r0 to 0
   …            (these encode which of the characters from
                encode part 2 are going to be decoded later)
    UO          enqueue a 0 to delimit parts 1 and 2
                encode part 2: 20 of the following
       +          n +s: increment r0 by n, n being
                  an ascii character code minus 33
        O         enqueue r0
         U        set r0 to 0
          …     (these encode the actual ascii character
                codes, and we'll add 33 to them later.
                we can call this little index of values
                our "table" of ascii values)
           +E   go to code block 1
             .  end code block

+…[]]]][]][{UO{^)]]-P+…P:]+++PD-};N)}:++E.  code block 1 (edited)
                                            (this block prints the encoding)
+…                                            5 +s: r0 = 5
  [                                           r2 = r0
   ]]]]                                       r0 = r2*4
       [                                      r2 = r0
        ]]                                    r0 = r2*2
          [                                   r2 = r0
+…[]]]][]][                                 r2 = 40
           {UO                    N)}:      for each value x on the queue
               ^                              r1 = x
              {^)       :     D-};            while r1--
                         ]+++P                  print ascii r2+3
                                              (r2==40, 43 is +'s ascii
                                              this loop prints x +s)
                 ]]-P                         print ascii r2*2-1
                     +…                       6 +s: => r0 = 49
                       P                      print ascii r2+9
                                              (these two print O and U to
                                              go after each string of +s)
                                      ++E   go to code block 2
                                         .  end code block

U)!-E:R[{UO):};{UOD+^(])R+…P:};{N()UO:+E;}  code block 2 (edited)
                                            (this block decodes the encoding)
U                                           x = queue.pop()
 )   :                                      if x==0
                                            (meaning we've reached the 0
                                            delimiter separating the two
                                            halves of encoded data)
  !                                           ACTUALLY print 
                                              (explanation below code block)
   -E                                         go to code block -1 (halt)
      R[                                    r2 = x
        {UO):};                             cycle through queue until
                                            we reach the 0 delimiter
                UO                          cycle to next x
               {  D+^(])    :};             while r1++ != r2
                UO                            cycle to next x
                        R+…P                R 33 +s P:print ascii table[x]+33
                               {N()UO:  ;}  cycle through remainder of queue
                                            (language quirk mandates this...
                                            i think these two loops can maybe
                                            be one loop, but its nontrivial)
                                      +E    go to code block 2

So in Headass, P just prints whatever number is in r0. In Headascii, P appends the ascii character for the value at r0 to a global "output" string, which can be printed with !. That's kind of wordy though and didn't fit in the little margin up there. #fermat

\$\endgroup\$
1
  • \$\begingroup\$ Apparently I already wrote a quine in headascii back in 2021. It's 2445 bytes, so it was shorter than the first attempt here, but longer than the most recent revision to date. Approach looks pretty different at a glance, hopefully I can reverse engineer and study it, maybe learn something to golf the whole thing down further. \$\endgroup\$ Commented Sep 15, 2023 at 21:46
6
\$\begingroup\$

JavaScript, 49 bytes

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

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

Can avoid use of template strings for + 1 byte.

eval(Q="q=Q.link()[8];alert('eval(Q='+q+Q+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\$
5
\$\begingroup\$

C++ (350)

#include<iostream>
#include<fstream>
int main(){std::ofstream f;f.open("f.cpp");
#define B(x)x;f<<("B(" #x ")");
#define A(x)f<<("A(" #x ")");x;
B(f<<("#include<iostream>\n#include<fstream>\nint main(){std::ofstream f;f.open(\"f.cpp\");\n#define B(x)x;f<<(\"B(\" #x \")\");\n#define A(x)f<<(\"A(\" #x \")\");x;\n"))A(f<<("f.close();}\n"))f.close();}

Modified version of this.

Makes use of the C++ preprocessor.

\$\endgroup\$
5
\$\begingroup\$

CSS, 47 bytes

<style>:before,*{display:block;content:'<style>

Paste into a blank HTML page to avoid conflict with other tags.

\$\endgroup\$
3
  • \$\begingroup\$ Wouldn't this technically be HTML with embedded CSS in it? Also, what browser did this successfully quine in, because when I test this with an empty HTML file, it displays :before,*{display:block;content:'<style> on the window. \$\endgroup\$ Commented Jul 16, 2017 at 5:08
  • \$\begingroup\$ This doesn't work as HTML has implicit html, head and body tags. \$\endgroup\$
    – null
    Commented Mar 20, 2018 at 16:20
  • \$\begingroup\$ @KonradBorowski But technically HTML still works if you leave out the tags on modern browsers. \$\endgroup\$ Commented Apr 2, 2022 at 18:20
5
\$\begingroup\$

Arcyóu, 1 byte

Q

The interpreter evaluates undefined symbols as strings, and the result of the last expression evaluated is automatically printed at the end of the program. What's interesting is that any undefined identifier can be used; I_am_a_quine! is also a quine.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ This does not satisfy our rules for proper quines as the Q only encodes itself (as does any character in I_am_a_quine!). \$\endgroup\$ Commented May 23, 2017 at 15:35
  • \$\begingroup\$ Agreed @MartinEnder, but the challenge does not specify proper quines. \$\endgroup\$
    – jqkul
    Commented Jun 2, 2017 at 21:30
5
\$\begingroup\$

Rust, 173 158 152 144 137 102 bytes

Tuples plus Rust's debug printing mechanism ( {:?} in a format string ) let me cut it down 30+ characters!

fn main(){let t=("fn main(){let t=", ";print!(\"{}{:?}{}\",t.0,t,t.1)}");print!("{}{:?}{}",t.0,t,t.1)}

Pretty-printed. It's so small there's little left to explain!

fn main() {
    // The space after the comma is necessary, because that's how
    // debug-print formats tuples
    let t=("fn main(){let t=", ";print!(\"{}{:?}{}\",t.0,t,t.1)}");
    print!("{}{:?}{}",t.0,t,t.1)
}
\$\endgroup\$
5
\$\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\$ Commented Sep 7, 2016 at 1:56
  • 1
    \$\begingroup\$ @ConorO'Brien Nope. The %s in the string only works with console.log. \$\endgroup\$ Commented Sep 7, 2016 at 2:00
  • \$\begingroup\$ ahhh that's what that was. \$\endgroup\$ Commented 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
    Commented Oct 23, 2016 at 5:26
5
\$\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\$
5
\$\begingroup\$

tinylisp, 88 bytes

The byte count includes a trailing newline.

((q (g (c (c (q q) g) (c (c (q q) g) ())))) (q (g (c (c (q q) g) (c (c (q q) g) ())))))

Try it online!

There are no strings in tinylisp, but a nontrivial quine is still possible because code is lists and lists are code. The above code is a list which, when evaluated, returns (and therefore prints) itself.

The idea is to pass the list (g (c (c (q q) g) (c (c (q q) g) ()))) to a function which will wrap it in a list, tack a q on the front, and then wrap two copies of that in a list. Which is exactly what the function (q (g (c (c (q q) g) (c (c (q q) g) ())))) does. In-depth explanation available on request, but I wanted to post this before turning in for the night.

\$\endgroup\$
0
5
\$\begingroup\$

Japt, 9 bytes

I've fantasized about a 9-byte Japt quine for years, and now it's finally snapped into place :-D

9îQi"9îQi

Test it online!

Explanation

    "9îQi    Start with this string.               9îQi
  Qi         Insert it before a quotation mark.    9îQi"
9î           Repeat until it reaches length 9.     9îQi"9îQi
\$\endgroup\$
5
\$\begingroup\$

Befunge-93, 25 22 bytes

-2*6<>:#,_@#:-5: _-p<"

Try it online!

Thanks to jimmy23013's answer for inspiring the idea to create the " before the wrapping string literal.

Previous answers have usually relied on non-standard interpreter behaviour in order to wrap a string literal around the code and avoid the extra spaces. My quine however, is compliant with Befunge-93 specs.

Befunge-93 has a bounding box of 80x25 cells, which are initially filled with spaces. This means the wrapping string literal, a staple of 2D quines, usually fills the stack with a lot of excess spaces.

How It Works:

-2*6<  Create the " character
                     " Start the wrapping string literal
                 _-p<  Pop all the spaces until there are none left
                       Note that p is the put command, which basically pops 3 items from the stack
            :-5:  Dupe the 2 and subtract 5 to replace the - that was destroyed
                  Dupe that again to compensate for the _
     >:#,_@#  Print until stack is empty and terminate

Alternatively:

++9*5<>:#,_@#::_$#-< "

also works for 22 bytes.

\$\endgroup\$
5
\$\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\$
5
\$\begingroup\$

Whitespace, 406 bytes

DISCLAIMER: This quine was not created by me, it is created by Smithers. Because this challenge was missing a Whitespace answer I decided to post his/her. If Smithers reads this and wants to post it himself/herself I will of course delete my answer.
Sources: Smithers' website and his/her Whitespace quine source code (note: it's missing a trailing new-line).

[S S S T    S S T   S T T   T   T   T   S T T   T   S S T   T   S S S T S T T   S T T   T   T   T   T   T   S T S S S T S S T   S S T   S T T   T   T   T   S T S T S S S T T   T   S S T   S T T   S S S S T   T   T   T   T   T   S S T   T   T   S T S T S S S T T   T   S S S S S S S T T   T   T   T   T   T   S T S S T   S S S T S T T   T   T   S S S S S T S T S S S T S T T   S T S S S T S S T   S S T   T   T   S S S S S S S T T   S T S S T   T   T   T   T   S S S T T   T   T   S S T   T   S T T   S S T   S T T   T   T   S S S S T   T   S S T   T   T   T   S T S S T   T   S S T   S S T   T   S S S S T   T   S S T   T   S S T   S S T   T   S T S T S T T   S S S T T   S T S N
_Push_67079405567184005086107571748115383207539763039497665210559156555730234138][S N
S _Duplicate][N
S T S N
_Call_Label_PRINT_SPACE][N
S T S N
_Call_Label_PRINT_SPACE][S S S T    S N
_Push_2][S N
T   _Swap][N
S T N
_Call_Label_RECURSIVE_PRINTER][S S S T  S T S N
_Push_10][T N
S S _Print_as_char][S N
N
_Drop][S S S T  T   N
_Push_3][S N
T   _Swap][N
S T N
_Call_Label_RECURSIVE_PRINTER][N
N
N
_Exit][N
S S N
_Create_Label_RECURSIVE_PRINTER][S N
S _Duplicate][N
T   S T N
_If_0_Jump_to_Label_DISCARD_TOP(_AND_PRINT_SPACE)][S T  S S T   N
_Copy_1][S T    S S T   N
_Copy_1][S T    S S T   N
_Copy_1][T  S T S _integer_divide][N
S T N
_Call_Label_RECURSIVE_PRINTER][T    S T T   _Modulo][S N
S _Duplicate][N
T   S T N
_If_0_Jump_to_Label_DISCARD_TOP(_AND_PRINT_SPACE)][S S S T  S S S N
_Push_8][T  S S S _Add][T   N
S S _Print_as_character][N
T   N
_Return][N
S S T   N
_Create_Label_DISCARD_TOP(_AND_PRINT_SPACE)][S N
N
_Discard][N
S S S N
_Create_Label_PRINT_SPACE][S S S T  S S S S S N
_Push_32][T N
S S _Print_as_character][N
T   N
_Return]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Whitespace is a stack-based language only using three characters: spaces, tabs and new-lines. In Whitespace the stack can only contain integers, and there are two options to print something to STDOUT: "Print as number" and "Print as character". In the case of "Print as character" it will print the character based on the unicode value at the top of the stack. Because whitespace uses spaces, tabs and new-lines, it means it'll have to print numbers 32, 9, and 10 respectively as characters to STDOUT for this quine.

Smithers uses a pretty ingenieus piece of code with the magic number (s)he found.

Pseudo-code:

Push 67079405567184005086107571748115383207539763039497665210559156555730234138
Duplicate top
Call function_PRINT_SPACE
Call function_PRINT_SPACE
Push 2
Swap top two
Call function_RECURSIVE_PRINTER
Push 10
Pop and print top as character
Discard top
Push 3
Swap top two
Call function_RECURSIVE_PRINTER
Exit program

function_RECURSIVE_PRINTER:
  Duplicate top
  If 0: Call function_DISCARD_TOP(_AND_PRINT_SPACE)
  Make a copy of the 2nd top item of the stack
  Make a copy of the 2nd top item of the stack
  Make a copy of the 2nd top item of the stack
  Integer-divide top two
  Call function_RECURSIVE_PRINTER
  Modulo top two
  Duplicate top
  If 0: Call function_DISCARD_TOP(_AND_PRINT_SPACE)
  Push 8
  Add top two
  Pop and print top as character
  Return

function_DISCARD_TOP(_AND_PRINT_SPACE):
  Discard top
function_PRINT_SPACE:
  Push 32
  Pop and print top as character
  Return

It first uses a recursive-loop which keeps integer-dividing the initial integer by 2 until it's 0. Once it's 0, it goes back over these values and does modulo-2, printing either a space (if the modulo-2 resulted in 0) or a tab (by adding 8 to the modulo-2 result of 1). This first part is used to print the magic number itself, which only consists of spaces and tabs, because pushing a number in Whitespace is done as follows (and thus doesn't contain any new-lines except for the single trailing one):

  • S: Enable Stack Manipulation
  • S: Push a number
  • S/T: Positive/Negative respectively
  • Some S/T, followed by a trailing N: The number as binary, where S=0 and T=1

After it has printed the spaces and tabs required for pushing the magic number itself, it pushes a 3 and will use the same recursive function with the magic number, integer-dividing and using modulo 3 instead of 2. Which will print the spaces (if the modulo-3 resulted in 0), or tabs/new-lines (by adding 8 to the modulo-3 result).

\$\endgroup\$
2
  • 2
    \$\begingroup\$ You may be interested in my new Whitespace quine \$\endgroup\$
    – Jo King
    Commented Oct 8, 2019 at 3:05
  • \$\begingroup\$ @JoKing Very nice! Well done! :) \$\endgroup\$ Commented Oct 8, 2019 at 7:31
5
\$\begingroup\$

Ruby, 27 bytes

eval s="$><<'eval s=';p s"

Try it online!

$><<'...' is equivalent to print'...' (outputs the string without a newline).

Note the newline at the end of the program.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to the site! Nice first answer :) \$\endgroup\$
    – DJMcMayhem
    Commented Jun 12, 2017 at 17:07
  • 1
    \$\begingroup\$ doesn't p print a newline? \$\endgroup\$
    – Shelvacu
    Commented Aug 11, 2019 at 23:20
  • \$\begingroup\$ @Shelvacu You're right; it should be 27 bytes. Interestingly, I went back and looked around, and I couldn't find any Ruby quines that included a newline in their source to match the output. Most seem to add an extra newline in their output like this one did. I also have a vague memory of coming across a 25-byte Ruby quine somewhere, I think in a demonstration of a new-at-the-time feature, but I haven't been able to find it again. \$\endgroup\$
    – Nnnes
    Commented Aug 12, 2019 at 23:43
5
\$\begingroup\$

1+, 834 bytes

(|11+"*"+"1+\1+/)("|1/()11+^)(2|\""++1+/()""+^)++<+/(#|\##"\+;1#()\^\1#)+<+()()(")(2)(2)()()(")()(2)(")(2)()(")()(")()()()(2)(")()()()(2)()()(2)()(")()()()()(2)(2)(")()()()(2)()()(2)()(")(2)()(")(2)(")()(")()()()(2)(")(2)(2)()(")()(2)(")()()()(2)()()(2)()(")(")()(")()(")()()()(2)()()(2)()(")(2)()(2)()(2)(")()(")(2)(")()()(2)()(")()(2)(")(2)(2)()()(")()(2)(")()(2)(")(2)()(")()()()()(2)(2)(")()(2)(")(")(")(2)()(")(2)(")()()(2)()(")()(2)(")()()()(2)(")()(2)(")()(2)(")(")(")()()()(2)()()(2)()(")(2)()(2)()(2)(")()()()(2)()(")(2)(")(2)()(")()()()()(2)(2)(")()(2)(")()()()(2)(")()()()(2)(")(2)()(")(2)(")()()(2)()(")()()()(2)(")(2)()(2)()(2)(")(")(2)(")(2)()(")()()(2)()(")()(2)(")()()()(2)(")()()()(2)()()(2)()(")()(2)(")()()()(2)(")(")()(2)(")(")(2)()()(")(")()(2)(")()()()(2)(")()()()(2)(")(2)()(2)()(2)(")(2)(")(2)()()(2)(")(")(#)@

Try it online!

Defines all the subroutines before the data section, then calls the (#) subroutine at the end of the data. Instead of using 1s followed by totalling 1+s, we define subroutines for initialisation ((")), which pushes a 2 to the stack, incrementing (()), and doubling plus 2 (ironically, (2)). All of these also push the characters used to call the subroutine to the top of the stack to print after printing the rest. We also offset the data by 32, since all values are above that.

This is most certainly suboptimal, especially since I've been steadily golfing it down from ~2000 bytes. I suspect it can be sub-500 eventually, or even lower with a different strategy.

Here's my program encoder, though it needs some post-fiddling with the first value to make sense.

\$\endgroup\$
5
\$\begingroup\$

Acc!!, 270 bytes

936025123570680582070742833115365117904492153588422750562053064415808293823109091171562255866020953926270476903421472061856963432351191541604543106801928196867870451324872393884426634
Count a while a-183 {
Write 48+(_/10^(182-a))%10
}
Count b while _ {
Write _%128
_/128

Try it online!

Explanation

...                          Set the accumulator to a large number
Count a while a-183 {        Loop from 0 to 182
Write 48+(_/10^(182-a))%10   Print the ath digit of the number
}
Count b while _ {            Loop while the accumulator is not zero
Write _%128                  Print the character of the accumulator modulo 128
_/128                        Integer divide the accumulator by 128
\$\endgroup\$
3
  • \$\begingroup\$ Does base95 (or 92 for that matter) save any bytes? \$\endgroup\$
    – emanresu A
    Commented Aug 1, 2022 at 19:40
  • \$\begingroup\$ @emanresuA I still have to represent newlines, so i don't think i can without making the decoding section much longer. I can do base 126 instead, but that doesn't save anything \$\endgroup\$
    – Jo King
    Commented Aug 2, 2022 at 2:44
  • 1
    \$\begingroup\$ 264 bytes by removing a pair of extraneous braces. \$\endgroup\$ Commented Apr 17 at 16:19
1
3 4
5
6 7
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.