Code Golf: Code Golf: Difference between revisions
(J) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 8: | Line 8: | ||
Not counting vector tables, disk/cartridge headers, and/or font graphics data, here is as small as I could get (example is for commodore 64) |
Not counting vector tables, disk/cartridge headers, and/or font graphics data, here is as small as I could get (example is for commodore 64) |
||
===With Quoted Literals=== |
===With Quoted Literals=== |
||
< |
<syntaxhighlight lang="6502asm">m |
||
LDX #0 |
LDX #0 |
||
LDA G,x |
LDA G,x |
||
Line 17: | Line 17: | ||
rts |
rts |
||
G |
G |
||
db "Code Golf",0</ |
db "Code Golf",0</syntaxhighlight> |
||
===Without Quoted Literals=== |
===Without Quoted Literals=== |
||
< |
<syntaxhighlight lang="6502asm">p equ -46 |
||
LDA #67 |
LDA #67 |
||
JSR p |
JSR p |
||
Line 38: | Line 38: | ||
JSR p |
JSR p |
||
LDA #102 |
LDA #102 |
||
JMP p</ |
JMP p</syntaxhighlight> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
===With Quoted Literals=== |
===With Quoted Literals=== |
||
< |
<syntaxhighlight lang="action!">PROC M()Print("Code Golf")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Code Golf</pre> |
<pre>Code Golf</pre> |
||
===Without Quoted Literals=== |
===Without Quoted Literals=== |
||
< |
<syntaxhighlight lang="action!">PROC M()Put(67)Put(111)Put(100)Put(101)Put(32)Put(71)Put(111)Put(108)Put(102)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Code Golf</pre> |
<pre>Code Golf</pre> |
||
Line 55: | Line 55: | ||
===With Quoted Literals=== |
===With Quoted Literals=== |
||
Source size is 18 bytes; as ALGOL 68G is an interpreter, there isn't a compiled object. The interpreter itself is 2780 K. |
Source size is 18 bytes; as ALGOL 68G is an interpreter, there isn't a compiled object. The interpreter itself is 2780 K. |
||
< |
<syntaxhighlight lang="algol68">print("Code Golf")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Code Golf</pre> |
<pre>Code Golf</pre> |
||
Line 62: | Line 62: | ||
Source file size is 67 bytes (says Windows DIR); as noted above, ALGOL 68G is an interpreter so there isn't a compiled object. The interpreter itself is 2780 K.<br> |
Source file size is 67 bytes (says Windows DIR); as noted above, ALGOL 68G is an interpreter so there isn't a compiled object. The interpreter itself is 2780 K.<br> |
||
Declares and uses a unary operator ! which is effectively an abbreviation for REPR (which converts an INT to a CHAR) and then uses this with the builtin + operator which appends CHARs or STRINGS to form another STRING. |
Declares and uses a unary operator ! which is effectively an abbreviation for REPR (which converts an INT to a CHAR) and then uses this with the builtin + operator which appends CHARs or STRINGS to form another STRING. |
||
< |
<syntaxhighlight lang="algol68">OP!=(INTc)CHAR:REPR(c+32);print(!35+!79+!68+!69+!0+!39+!79+!76+!70)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Code Golf</pre> |
<pre>Code Golf</pre> |
||
Line 68: | Line 68: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">print"Code Golf" |
||
print join to[:char][67 111 100 101 32 71 111 108 102]</ |
print join to[:char][67 111 100 101 32 71 111 108 102]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 77: | Line 77: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="AWK"> |
||
# syntax: GAWK -f CODE_GOLF.AWK |
# syntax: GAWK -f CODE_GOLF.AWK |
||
# |
# |
||
Line 93: | Line 93: | ||
BEGIN{print("\x43\x6F\x64\x65\x20\x47\x6F\x6C\x66")} |
BEGIN{print("\x43\x6F\x64\x65\x20\x47\x6F\x6C\x66")} |
||
BEGIN{print("\103\157\144\145\040\107\157\154\146")} |
BEGIN{print("\103\157\144\145\040\107\157\154\146")} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 103: | Line 103: | ||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
With a quoted string, the following weighs in at 12 bytes. |
With a quoted string, the following weighs in at 12 bytes. |
||
< |
<syntaxhighlight lang="freebasic">?"Code Golf"</syntaxhighlight> |
||
For the second task, this is 66 bytes long. |
For the second task, this is 66 bytes long. |
||
< |
<syntaxhighlight lang="freebasic">dim a={37,81,70,71,2,41,81,78,72} |
||
for i=0 to 8 |
for i=0 to 8 |
||
?chr(30+a[i]); |
?chr(30+a[i]); |
||
next</ |
next</syntaxhighlight> |
||
Note: BASIC256 is an interpreter, it does not generate executables. |
Note: BASIC256 is an interpreter, it does not generate executables. |
||
Line 115: | Line 115: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2020-08-14}} |
{{works with|Factor|0.99 2020-08-14}} |
||
< |
<syntaxhighlight lang="factor">[I Code GolfI] |
||
{ 67 111 100 101 32 71 111 108 102 } write</ |
{ 67 111 100 101 32 71 111 108 102 } write</syntaxhighlight> |
||
The executable is 2,265 KB. |
The executable is 2,265 KB. |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
With a quoted string, the following weighs in at 12 bytes. |
With a quoted string, the following weighs in at 12 bytes. |
||
< |
<syntaxhighlight lang="freebasic">?"Code Golf"</syntaxhighlight> |
||
For the second task, this is 80 bytes long. |
For the second task, this is 80 bytes long. |
||
< |
<syntaxhighlight lang="freebasic">dim as byte i,a(8)={37,81,70,71,2,41,81,78,72} |
||
for i=0 to 8 |
for i=0 to 8 |
||
?chr(30+a(i)); |
?chr(30+a(i)); |
||
next</ |
next</syntaxhighlight> |
||
Both compile to a file 27,016 bytes long. |
Both compile to a file 27,016 bytes long. |
||
Line 134: | Line 134: | ||
"Code Golf" as Hex in little Endian ending in 0x00 |
"Code Golf" as Hex in little Endian ending in 0x00 |
||
86 byte. linux executable fpc 3.2.2 : 8x386 183400 Byte | x64 191104 byte |
86 byte. linux executable fpc 3.2.2 : 8x386 183400 Byte | x64 191104 byte |
||
< |
<syntaxhighlight lang="pascal"> |
||
var a:QWord=$006F472065646F43;b:DWord=$0000666C;BEGIN write(pChar(@a),pChar(@b));END. |
var a:QWord=$006F472065646F43;b:DWord=$0000666C;BEGIN write(pChar(@a),pChar(@b));END. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre>Code Golf</pre> |
{{out}}<pre>Code Golf</pre> |
||
Line 143: | Line 143: | ||
The shortest possible program (44 bytes) to print the required string is: |
The shortest possible program (44 bytes) to print the required string is: |
||
< |
<syntaxhighlight lang="go">package main;func main(){print("Code Golf")}</syntaxhighlight> |
||
If the program itself cannot contain string or character literals, then the shortest program I've been able to come up with (81 bytes) is: |
If the program itself cannot contain string or character literals, then the shortest program I've been able to come up with (81 bytes) is: |
||
< |
<syntaxhighlight lang="go">package main;func main(){print(string([]byte{67,111,100,101,32,71,111,108,102}))}</syntaxhighlight> |
||
Output in both cases |
Output in both cases |
||
<pre> |
<pre> |
||
Line 156: | Line 156: | ||
=={{header|Golfscript}}== |
=={{header|Golfscript}}== |
||
With a quoted string, the following weighs in at 11 bytes. |
With a quoted string, the following weighs in at 11 bytes. |
||
< |
<syntaxhighlight lang="golfscript">"Code Golf"</syntaxhighlight> |
||
For the second task, this is 78 bytes long. |
For the second task, this is 78 bytes long. |
||
< |
<syntaxhighlight lang="golfscript">67[]+''+111[]+''+100[]+''+101[]+''+32[]+''+71[]+''+111[]+''+108[]+''+102[]+''+</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
In both cases: |
In both cases: |
||
Line 196: | Line 196: | ||
</pre> |
</pre> |
||
For the second task, the following program clocks in at 41 bytes: |
For the second task, the following program clocks in at 41 bytes: |
||
<lang>[-33,11,0,1,-68,-29,11,8,2|.+100]|implode</ |
<syntaxhighlight lang="text">[-33,11,0,1,-68,-29,11,8,2|.+100]|implode</syntaxhighlight> |
||
'''Extra credit''': |
'''Extra credit''': |
||
Line 203: | Line 203: | ||
=={{header|Openscad}}== |
=={{header|Openscad}}== |
||
With a quoted string, the following weighs in at 18 bytes. |
With a quoted string, the following weighs in at 18 bytes. |
||
< |
<syntaxhighlight lang="openscad">text("Code Golf");</syntaxhighlight> |
||
For the second task, this is 46 bytes long. |
For the second task, this is 46 bytes long. |
||
< |
<syntaxhighlight lang="openscad">text(chr([67,111,100,101,32,71,111,108,102]));</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
The shortest ISO-compliant Pascal program is 46 characters. |
The shortest ISO-compliant Pascal program is 46 characters. |
||
< |
<syntaxhighlight lang="pascal">program p(output);begin write('Code Golf')end.</syntaxhighlight> |
||
Unless you make certain presumptions about the target system, you cannot achieve the second task in Pascal (as defined by the ISO standards). |
Unless you make certain presumptions about the target system, you cannot achieve the second task in Pascal (as defined by the ISO standards). |
||
Therefore, see [[#Free Pascal|Free Pascal]] for one method. |
Therefore, see [[#Free Pascal|Free Pascal]] for one method. |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl"># 20211216 Perl programming solution |
||
# 1 2 |
# 1 2 |
||
Line 225: | Line 225: | ||
#1234567890123456789012345678901234567890 |
#1234567890123456789012345678901234567890 |
||
print pack'H*','436F646520476F6C66' # 35 bytes |
print pack'H*','436F646520476F6C66' # 35 bytes |
||
; print "\n";</ |
; print "\n";</syntaxhighlight> |
||
Actually just for storage purpose it is possible (just not always) to store the bytes string as an UTF string. |
Actually just for storage purpose it is possible (just not always) to store the bytes string as an UTF string. |
||
< |
<syntaxhighlight lang="perl">binmode STDOUT, ":encoding(UTF-8)"; |
||
my $string = "\x{436F}\x{6465} \x{476F}\x{6C66}"; |
my $string = "\x{436F}\x{6465} \x{476F}\x{6C66}"; |
||
Line 242: | Line 242: | ||
} |
} |
||
print "Extract to bytes '$bytes' with length = ".length($bytes)."\n";</ |
print "Extract to bytes '$bytes' with length = ".length($bytes)."\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>'䍯摥 䝯汦' is an UTF string with length = 5 |
<pre>'䍯摥 䝯汦' is an UTF string with length = 5 |
||
Line 249: | Line 249: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="Phix">(phixonline)--> |
||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Code Golf"</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Code Golf"</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
Which is 19 bytes. Note that <code>?"Code Golf"</code>, while only 12 bytes, ''does'' print the quotation marks and therefore does not meet the task specifications.<br> |
Which is 19 bytes. Note that <code>?"Code Golf"</code>, while only 12 bytes, ''does'' print the quotation marks and therefore does not meet the task specifications.<br> |
||
Without using string literals, at 42 bytes we can have |
Without using string literals, at 42 bytes we can have |
||
<!--< |
<!--<syntaxhighlight lang="Phix">(phixonline)--> |
||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">67</span><span style="color: #0000FF;">,</span><span style="color: #000000;">111</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</span><span style="color: #0000FF;">,</span><span style="color: #000000;">71</span><span style="color: #0000FF;">,</span><span style="color: #000000;">111</span><span style="color: #0000FF;">,</span><span style="color: #000000;">108</span><span style="color: #0000FF;">,</span><span style="color: #000000;">102</span><span style="color: #0000FF;">})</span> |
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">67</span><span style="color: #0000FF;">,</span><span style="color: #000000;">111</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</span><span style="color: #0000FF;">,</span><span style="color: #000000;">71</span><span style="color: #0000FF;">,</span><span style="color: #000000;">111</span><span style="color: #0000FF;">,</span><span style="color: #000000;">108</span><span style="color: #0000FF;">,</span><span style="color: #000000;">102</span><span style="color: #0000FF;">})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
Slightly shorter, at 30 bytes, though it could be considered string/char: |
Slightly shorter, at 30 bytes, though it could be considered string/char: |
||
<!--< |
<!--<syntaxhighlight lang="Phix">(phixonline)--> |
||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>x"<span style="color: #0000FF;">436F646520476F6C66</span>"<span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>x"<span style="color: #0000FF;">436F646520476F6C66</span>"<span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
The compiled size of the first is 276,992 bytes. |
The compiled size of the first is 276,992 bytes. |
||
You can actually make a smaller executable as follows: |
You can actually make a smaller executable as follows: |
||
<!--< |
<!--<syntaxhighlight lang="Phix">(phixonline)--> |
||
<span style="color: #008080;">include</span> <span style="color: #000000;">puts1h</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
<span style="color: #008080;">include</span> <span style="color: #000000;">puts1h</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
<span style="color: #000000;">puts1</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Code Golf"</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">puts1</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Code Golf"</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
Then compile it with p -c -nodiag ''test.exw'' (or whatever) to yield an executable of 36,532 bytes - |
Then compile it with p -c -nodiag ''test.exw'' (or whatever) to yield an executable of 36,532 bytes - |
||
no diagnostics, which is itself non-trivial and otherwise pulls in file handling (for the ex.err it writes), |
no diagnostics, which is itself non-trivial and otherwise pulls in file handling (for the ex.err it writes), |
||
Line 278: | Line 278: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
As a string: |
As a string: |
||
< |
<syntaxhighlight lang="Picat">main => "Code Golf".print.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 284: | Line 284: | ||
No quotes: |
No quotes: |
||
< |
<syntaxhighlight lang="Picat">main => [67,111,100,101,32,71,111,108,102].map(chr).print.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 291: | Line 291: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
With a quoted string, the following weighs in at 18 bytes. |
With a quoted string, the following weighs in at 18 bytes. |
||
< |
<syntaxhighlight lang="PureBasic">Print("Code Golf")</syntaxhighlight> |
||
For the second task, this is 69 bytes long. |
For the second task, this is 69 bytes long. |
||
< |
<syntaxhighlight lang="PureBasic">Dim a(8) |
||
a(0)=37:a(1)=81:a(2)=70:a(3)=71 |
a(0)=37:a(1)=81:a(2)=70:a(3)=71 |
||
a(4)=2:a(5)=41:a(6)=81:a(7)=78:a(8)=72 |
a(4)=2:a(5)=41:a(6)=81:a(7)=78:a(8)=72 |
||
For i=0 To 8 |
For i=0 To 8 |
||
Print(Chr(30+a(i))) |
Print(Chr(30+a(i))) |
||
Next</ |
Next</syntaxhighlight> |
||
The size of the executables are 7680 and 10752 bytes respectively though this will obviously depend on PureBasic version, platform and build options being used. |
The size of the executables are 7680 and 10752 bytes respectively though this will obviously depend on PureBasic version, platform and build options being used. |
||
Line 306: | Line 306: | ||
;Python 3.x: |
;Python 3.x: |
||
With a quoted string, the following weighs in at 20 bytes. |
With a quoted string, the following weighs in at 20 bytes. |
||
< |
<syntaxhighlight lang="python">print("Code Golf")</syntaxhighlight> |
||
For the second task, this is 87 bytes long. |
For the second task, this is 87 bytes long. |
||
< |
<syntaxhighlight lang="python">a=[37,81,70,71,2,41,81,78,72] |
||
for i in range(0,9): |
for i in range(0,9): |
||
print(chr(30+a[i]),end = "")</ |
print(chr(30+a[i]),end = "")</syntaxhighlight> |
||
Line 317: | Line 317: | ||
{{works with|QBasic|1.1}} |
{{works with|QBasic|1.1}} |
||
With a quoted string, the following weighs in at 17 bytes. |
With a quoted string, the following weighs in at 17 bytes. |
||
< |
<syntaxhighlight lang="qbasic">PRINT "Code Golf"</syntaxhighlight> |
||
For the second task, this is 91 bytes long. |
For the second task, this is 91 bytes long. |
||
< |
<syntaxhighlight lang="qbasic">DIM a(8) |
||
DATA 37,81,70,71,2,41,81,78,72 |
DATA 37,81,70,71,2,41,81,78,72 |
||
FOR i = 0 TO 8 |
FOR i = 0 TO 8 |
||
READ a(i) |
READ a(i) |
||
PRINT CHR$(30 + a(i));</ |
PRINT CHR$(30 + a(i));</syntaxhighlight> |
||
Note: QBasic is an interpreter, it does not generate executables. |
Note: QBasic is an interpreter, it does not generate executables. |
||
Line 333: | Line 333: | ||
===With Quoted Literals=== |
===With Quoted Literals=== |
||
< |
<syntaxhighlight lang="Quackery">say "Code Golf"</syntaxhighlight> |
||
===Without Quoted Literals=== |
===Without Quoted Literals=== |
||
Line 339: | Line 339: | ||
<code>' [ 67 111 100 101 32 71 111 108 102 ] echo$</code> is marginally shorter but less interesting. For longer strings, encoding the text as a bignum rapidly becomes the more space efficient option. The text "Code Golf" is not quite long enough for the obvious improvement of using hexadecimal rather than decimal, as the digit reduction is less than the four character overhead of putting <code>hex</code> and a space before the number. |
<code>' [ 67 111 100 101 32 71 111 108 102 ] echo$</code> is marginally shorter but less interesting. For longer strings, encoding the text as a bignum rapidly becomes the more space efficient option. The text "Code Golf" is not quite long enough for the obvious improvement of using hexadecimal rather than decimal, as the digit reduction is less than the four character overhead of putting <code>hex</code> and a space before the number. |
||
< |
<syntaxhighlight lang="Quackery">2549578149779768531 9 times [ 112 /mod emit ] drop</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
I suspect there may be shorter methods, but these are my best attempt. |
I suspect there may be shorter methods, but these are my best attempt. |
||
< |
<syntaxhighlight lang="R">## easy way |
||
cat("Code Golf") |
cat("Code Golf") |
||
Line 349: | Line 349: | ||
cat(rlang::string(c(0x43, 0x6F, 0x64, 0x65, 0x20, |
cat(rlang::string(c(0x43, 0x6F, 0x64, 0x65, 0x20, |
||
0x47, 0x6F, 0x6C, 0x66))) |
0x47, 0x6F, 0x6C, 0x66))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Code Golf</pre> |
<pre>Code Golf</pre> |
||
Line 356: | Line 356: | ||
=={{header|True BASIC}}== |
=={{header|True BASIC}}== |
||
With a quoted string, the following weighs in at 22 bytes. |
With a quoted string, the following weighs in at 22 bytes. |
||
< |
<syntaxhighlight lang="qbasic">PRINT "Code Golf" |
||
END</ |
END</syntaxhighlight> |
||
For the second task, this is 100 bytes long. |
For the second task, this is 100 bytes long. |
||
< |
<syntaxhighlight lang="qbasic">DIM a(9) |
||
DATA 37,81,70,71,2,41,81,78,72 |
DATA 37,81,70,71,2,41,81,78,72 |
||
FOR i=1 to 9 |
FOR i=1 to 9 |
||
Line 366: | Line 366: | ||
PRINT CHR$(30+a(i)); |
PRINT CHR$(30+a(i)); |
||
NEXT i |
NEXT i |
||
END</ |
END</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Not very interesting, as it's pretty much just standard, non-obscure Raku. The output string is so short, there isn't any easy way to golf it shorter than just printing it directly. 17 bytes. |
Not very interesting, as it's pretty much just standard, non-obscure Raku. The output string is so short, there isn't any easy way to golf it shorter than just printing it directly. 17 bytes. |
||
<lang |
<syntaxhighlight lang="raku" line>print <Code Golf></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Code Golf</pre> |
<pre>Code Golf</pre> |
||
Line 377: | Line 377: | ||
Assuming we can't use the string literal in the source, the shortest I've come up with is: |
Assuming we can't use the string literal in the source, the shortest I've come up with is: |
||
<lang |
<syntaxhighlight lang="raku" line>print chrs (-32,12,1,2,-67,-28,12,9,3) »+»99 # 45 Chars, 47 bytes</syntaxhighlight> |
||
<lang |
<syntaxhighlight lang="raku" line>print chrs (-3,㊶,㉚,㉛,-㊳,1,㊶,㊳,㉜) »+»㉎ # 37 Chars, 56 bytes</syntaxhighlight> |
||
<lang |
<syntaxhighlight lang="raku" line>print <Dpef!Hpmg>.ords».pred.chrs # 33 Chars, 34 bytes. Somewhat cheaty as it _does_ contain a string literal, but not the same literal as the output</syntaxhighlight> |
||
Same output for each. Of course, to actually run any of that code you need the Raku compiler at 18.0Kb, the nqp vm interpreter at 17.9 Kb and the moar virtual machine at 17.9Kb. (Or the Java virtual machine, which is remarkably difficult to come up with a size for...) |
Same output for each. Of course, to actually run any of that code you need the Raku compiler at 18.0Kb, the nqp vm interpreter at 17.9 Kb and the moar virtual machine at 17.9Kb. (Or the Java virtual machine, which is remarkably difficult to come up with a size for...) |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
With a quoted string, the following weighs in at 17 bytes. |
With a quoted string, the following weighs in at 17 bytes. |
||
< |
<syntaxhighlight lang="freebasic">print "Code Golf"</syntaxhighlight> |
||
For the second task, this is 82 bytes long. |
For the second task, this is 82 bytes long. |
||
< |
<syntaxhighlight lang="freebasic">dim a(8):data 37,81,70,71,2,41,81,78,72:for i=0 to 8:read j:print chr$(30+j);:next</syntaxhighlight> |
||
Note: Run BASIC is an interpreter, it does not generate executables. |
Note: Run BASIC is an interpreter, it does not generate executables. |
||
Line 393: | Line 393: | ||
=={{header|Verilog}}== |
=={{header|Verilog}}== |
||
With a quoted string, the following weighs in at 64 bytes. |
With a quoted string, the following weighs in at 64 bytes. |
||
< |
<syntaxhighlight lang="Verilog">module main; |
||
initial begin $write("Code Golf"); |
initial begin $write("Code Golf"); |
||
end |
end |
||
endmodule</ |
endmodule</syntaxhighlight> |
||
For the second task, this is 102 bytes long. |
For the second task, this is 102 bytes long. |
||
< |
<syntaxhighlight lang="Verilog">module main; |
||
initial begin $write("%c%c%c%c %c%c%c%c",67,111,100,101,71,111,108,102); |
initial begin $write("%c%c%c%c %c%c%c%c",67,111,100,101,71,111,108,102); |
||
end |
end |
||
endmodule</ |
endmodule</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
In both cases: |
In both cases: |
||
Line 412: | Line 412: | ||
The shortest possible program to print the required string is: |
The shortest possible program to print the required string is: |
||
< |
<syntaxhighlight lang="go">print("Code Golf")</syntaxhighlight> |
||
If the program itself cannot contain string or character literals, then use byte list: |
If the program itself cannot contain string or character literals, then use byte list: |
||
< |
<syntaxhighlight lang="go">print([u8(67),111,100,101,32,71,111,108,102].bytestr())</syntaxhighlight> |
||
Output in both cases |
Output in both cases |
||
<pre> |
<pre> |
||
Line 423: | Line 423: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
The shortest possible program (25 bytes) to print the required string is: |
The shortest possible program (25 bytes) to print the required string is: |
||
< |
<syntaxhighlight lang="ecmascript">System.write("Code Golf")</syntaxhighlight> |
||
The size of the executable needed to run this or indeed any other standalone program (Wren-cli on Linux) is 414,760 bytes. However, if Wren were being embedded in a minimal C program, then the size of the executable would be 17,320 bytes. |
The size of the executable needed to run this or indeed any other standalone program (Wren-cli on Linux) is 414,760 bytes. However, if Wren were being embedded in a minimal C program, then the size of the executable would be 17,320 bytes. |
||
If the program itself cannot contain string or character literals, then the shortest program we've been able to come up with (71 bytes) - thanks to Thundergnat! - is: |
If the program itself cannot contain string or character literals, then the shortest program we've been able to come up with (71 bytes) - thanks to Thundergnat! - is: |
||
< |
<syntaxhighlight lang="ecmascript">for(c in[-32,12,1,2,-67,-28,12,9,3])System.write(String.fromByte(c+99))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
In both cases: |
In both cases: |
||
Line 441: | Line 441: | ||
takes advantage of the high byte of register bp being set to 09h when the |
takes advantage of the high byte of register bp being set to 09h when the |
||
program is started by MS-DOS. 09h selects the "display string" function. |
program is started by MS-DOS. 09h selects the "display string" function. |
||
< |
<syntaxhighlight lang="asm">.model tiny |
||
.code |
.code |
||
org 256 |
org 256 |
||
Line 449: | Line 449: | ||
ret |
ret |
||
m db "Code Golf$" |
m db "Code Golf$" |
||
end s</ |
end s</syntaxhighlight> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
Line 455: | Line 455: | ||
it's 54,400 bytes on the Raspberry Pi. Under MS-DOS a version of the |
it's 54,400 bytes on the Raspberry Pi. Under MS-DOS a version of the |
||
compiler produces an executable as small as 6674 bytes. |
compiler produces an executable as small as 6674 bytes. |
||
< |
<syntaxhighlight lang="XPL0">Text(0,"Code Golf")</syntaxhighlight> |
||
This version without a string or character literals is 33 characters |
This version without a string or character literals is 33 characters |
||
long. |
long. |
||
< |
<syntaxhighlight lang="XPL0">Text(0,[$65646f43,$6c6f4720,$e6])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 466: | Line 466: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
With a quoted string, the following weighs in at 12 bytes. |
With a quoted string, the following weighs in at 12 bytes. |
||
< |
<syntaxhighlight lang="yabasic">?"Code Golf"</syntaxhighlight> |
||
For the second task, this is 118 bytes long. |
For the second task, this is 118 bytes long. |
||
< |
<syntaxhighlight lang="yabasic">dim a(8) |
||
a(0)=37:a(1)=81:a(2)=70:a(3)=71 |
a(0)=37:a(1)=81:a(2)=70:a(3)=71 |
||
a(4)=2:a(5)=41:a(6)=81:a(7)=78:a(8)=72 |
a(4)=2:a(5)=41:a(6)=81:a(7)=78:a(8)=72 |
||
for i=0 to 8 |
for i=0 to 8 |
||
?chr$(30+a(i)); |
?chr$(30+a(i)); |
||
next</ |
next</syntaxhighlight> |
||
Note: Yabasic is an interpreter, it does not generate executables. |
Note: Yabasic is an interpreter, it does not generate executables. |
||
Line 482: | Line 482: | ||
===With Quoted Literals=== |
===With Quoted Literals=== |
||
Total: 11 lines. |
Total: 11 lines. |
||
< |
<syntaxhighlight lang="z80">org &200 |
||
ld hl,g |
ld hl,g |
||
o: |
o: |
||
Line 492: | Line 492: | ||
jr o |
jr o |
||
g: |
g: |
||
db "Code Golf",0</ |
db "Code Golf",0</syntaxhighlight> |
||
Hexdump of the executable: |
Hexdump of the executable: |
||
Line 502: | Line 502: | ||
===Without Quoted Literals=== |
===Without Quoted Literals=== |
||
Total: 20 lines |
Total: 20 lines |
||
< |
<syntaxhighlight lang="z80">org &200 |
||
q equ &bb5a |
q equ &bb5a |
||
LD A,67 |
LD A,67 |
||
Line 521: | Line 521: | ||
call q |
call q |
||
LD a,102 |
LD a,102 |
||
jp q</ |
jp q</syntaxhighlight> |
||
Hexdump of the executable: |
Hexdump of the executable: |
Revision as of 19:57, 26 August 2022
First, show the shortest possible program that will emit the nine-character string “Code Golf”, without the quotation marks and without anything after the final “f”. Then show the shortest possible program that does the same thing but without itself containing any string or character literals, and without requiring any input or any environment variables or command-line arguments, though the name of the running program can be used.
Extra credit: how big is the executable required to perform the first task? Skip details about any prior compilation steps that might be involved.
6502 Assembly
Not counting vector tables, disk/cartridge headers, and/or font graphics data, here is as small as I could get (example is for commodore 64)
With Quoted Literals
m
LDX #0
LDA G,x
BEQ d
jsr -46
jmp m+2
d
rts
G
db "Code Golf",0
Without Quoted Literals
p equ -46
LDA #67
JSR p
LDA #111
JSR p
LDA #100
JSR p
LDA #101
JSR p
LDA #32
JSR p
LDA #71
JSR p
LDA #111
JSR p
LDA #108
JSR p
LDA #102
JMP p
Action!
With Quoted Literals
PROC M()Print("Code Golf")
- Output:
Code Golf
Without Quoted Literals
PROC M()Put(67)Put(111)Put(100)Put(101)Put(32)Put(71)Put(111)Put(108)Put(102)
- Output:
Code Golf
ALGOL 68
With Quoted Literals
Source size is 18 bytes; as ALGOL 68G is an interpreter, there isn't a compiled object. The interpreter itself is 2780 K.
print("Code Golf")
- Output:
Code Golf
Without Quoted Literals
Source file size is 67 bytes (says Windows DIR); as noted above, ALGOL 68G is an interpreter so there isn't a compiled object. The interpreter itself is 2780 K.
Declares and uses a unary operator ! which is effectively an abbreviation for REPR (which converts an INT to a CHAR) and then uses this with the builtin + operator which appends CHARs or STRINGS to form another STRING.
OP!=(INTc)CHAR:REPR(c+32);print(!35+!79+!68+!69+!0+!39+!79+!76+!70)
- Output:
Code Golf
Arturo
print"Code Golf"
print join to[:char][67 111 100 101 32 71 111 108 102]
- Output:
Code Golf Code Golf
AWK
# syntax: GAWK -f CODE_GOLF.AWK
#
# Under MS-Windows 10 using Thompson Automation's TAWK 5.0c AWKW -xm
# the compiled length of each program is 34,936 bytes and all three is 35,140 bytes.
# Each requires the Awkr50w.EXE runtime of 231,936 bytes.
#
# Under MS-Windows 10 using Thompson Automation's TAWK 5.0c AWKW -xe
# the compiled length of each program is 244,856 bytes and all three is 245,060 bytes.
# This is a completely stand-alone executable.
#
# 25 bytes
BEGIN{print("Code Golf")}
# 52 bytes
BEGIN{print("\x43\x6F\x64\x65\x20\x47\x6F\x6C\x66")}
BEGIN{print("\103\157\144\145\040\107\157\154\146")}
- Output:
Code Golf Code Golf Code Golf
BASIC256
With a quoted string, the following weighs in at 12 bytes.
?"Code Golf"
For the second task, this is 66 bytes long.
dim a={37,81,70,71,2,41,81,78,72}
for i=0 to 8
?chr(30+a[i]);
next
Note: BASIC256 is an interpreter, it does not generate executables.
Factor
[I Code GolfI]
{ 67 111 100 101 32 71 111 108 102 } write
The executable is 2,265 KB.
FreeBASIC
With a quoted string, the following weighs in at 12 bytes.
?"Code Golf"
For the second task, this is 80 bytes long.
dim as byte i,a(8)={37,81,70,71,2,41,81,78,72}
for i=0 to 8
?chr(30+a(i));
next
Both compile to a file 27,016 bytes long.
Free Pascal
"Code Golf" as Hex in little Endian ending in 0x00 86 byte. linux executable fpc 3.2.2 : 8x386 183400 Byte | x64 191104 byte
var a:QWord=$006F472065646F43;b:DWord=$0000666C;BEGIN write(pChar(@a),pChar(@b));END.
- Output:
Code Golf
Go
Go isn't well equipped for Code Golf as a certain amount of ceremony (package main and func main()) are needed for any executable.
The shortest possible program (44 bytes) to print the required string is:
package main;func main(){print("Code Golf")}
If the program itself cannot contain string or character literals, then the shortest program I've been able to come up with (81 bytes) is:
package main;func main(){print(string([]byte{67,111,100,101,32,71,111,108,102}))}
Output in both cases
Code Golf
The size of the executables are 1,158,158 and 1,158,174 bytes respectively though this will obviously depend on Go version, platform and build options being used.
Golfscript
With a quoted string, the following weighs in at 11 bytes.
"Code Golf"
For the second task, this is 78 bytes long.
67[]+''+111[]+''+100[]+''+101[]+''+32[]+''+71[]+''+111[]+''+108[]+''+102[]+''+
- Output:
In both cases:
Code Golf
J
For this bit of silliness, eliminating a trailing newline on stdout is probably the most difficult issue. So, we limit our implementation to linux and use /proc/self/fd/1
Sadly, we need to use a character literal to reference /proc/self/fd/1
But we do not need that reference to live in the implementation -- this task explicitly allows us to use the name of the running program.
So, our program looks like this:
#!/usr/bin/env jconsole exit".(a.C.~<45 47){~a.i.;}.ARGV
And we name our program "exit'Code Golf'fwrite'-proc-self-fd-1'"
Here's an example bash session, illustrating this incredibly useful program:
$ "exit'Code Golf'fwrite'-proc-self-fd-1'" | wc 0 2 9 $ "exit'Code Golf'fwrite'-proc-self-fd-1'" Code Golf$
jq
Works with gojq, the Go implementation of jq
To skip the newline, the interpreter must be invoked with the -j option:
$ jq -nj '"Code Golf"' | wc -c 9
For the second task, the following program clocks in at 41 bytes:
[-33,11,0,1,-68,-29,11,8,2|.+100]|implode
Extra credit: The jq executable on my Mac is 461,864 bytes; gojq's is over 8 times larger.
Openscad
With a quoted string, the following weighs in at 18 bytes.
text("Code Golf");
For the second task, this is 46 bytes long.
text(chr([67,111,100,101,32,71,111,108,102]));
Pascal
The shortest ISO-compliant Pascal program is 46 characters.
program p(output);begin write('Code Golf')end.
Unless you make certain presumptions about the target system, you cannot achieve the second task in Pascal (as defined by the ISO standards). Therefore, see Free Pascal for one method.
Perl
# 20211216 Perl programming solution
# 1 2
#12345678901234567890
print 'Code Golf' # 17 bytes
; print "\n";
# 1 2 3 4
#1234567890123456789012345678901234567890
print pack'H*','436F646520476F6C66' # 35 bytes
; print "\n";
Actually just for storage purpose it is possible (just not always) to store the bytes string as an UTF string.
binmode STDOUT, ":encoding(UTF-8)";
my $string = "\x{436F}\x{6465} \x{476F}\x{6C66}";
print "\n";
print "\n'$string' is an UTF string with length = ".length($string)."\n";
print "\n";
my ( $bytes, $offset ) = '', 0 ;
for ( map { ord $_ } split //, $string ) {
my @ar = ();
while ( $_ > 0 ) { unshift @ar, $_ & 0xff and $_ >>= 8 }
for ( @ar ) { vec( $bytes, $offset++, 8 ) = $_ }
}
print "Extract to bytes '$bytes' with length = ".length($bytes)."\n";
- Output:
'䍯摥 䝯汦' is an UTF string with length = 5 Extract to bytes 'Code Golf' with length = 9
Phix
puts(1,"Code Golf")
Which is 19 bytes. Note that ?"Code Golf"
, while only 12 bytes, does print the quotation marks and therefore does not meet the task specifications.
Without using string literals, at 42 bytes we can have
puts(1,{67,111,100,101,32,71,111,108,102})
Slightly shorter, at 30 bytes, though it could be considered string/char:
puts(1,x"436F646520476F6C66")
The compiled size of the first is 276,992 bytes. You can actually make a smaller executable as follows:
include puts1h.e puts1("Code Golf")
Then compile it with p -c -nodiag test.exw (or whatever) to yield an executable of 36,532 bytes - no diagnostics, which is itself non-trivial and otherwise pulls in file handling (for the ex.err it writes), printf, ffi, and they in turn pull in almost every builtin in existence between them. However even without all that lot it still needs stack, unassigned, and heap handlers, and unfortunately the latter also drags in delete() and therefore callfunc and therefore a whole bunch of subscript stuff we don't rightly need... still I suppose 36K ain't really all that bad. Oh, I should also say the compiler/interpreter/linker/debugger is itself (currently) 2,789,376 bytes, plus you'll still need most of builtins/ which is around the 9MB mark.
Picat
As a string:
main => "Code Golf".print.
- Output:
Code Golf
No quotes:
main => [67,111,100,101,32,71,111,108,102].map(chr).print.
- Output:
Code Golf
PureBasic
With a quoted string, the following weighs in at 18 bytes.
Print("Code Golf")
For the second task, this is 69 bytes long.
Dim a(8)
a(0)=37:a(1)=81:a(2)=70:a(3)=71
a(4)=2:a(5)=41:a(6)=81:a(7)=78:a(8)=72
For i=0 To 8
Print(Chr(30+a(i)))
Next
The size of the executables are 7680 and 10752 bytes respectively though this will obviously depend on PureBasic version, platform and build options being used.
Python
- Python 3.x
With a quoted string, the following weighs in at 20 bytes.
print("Code Golf")
For the second task, this is 87 bytes long.
a=[37,81,70,71,2,41,81,78,72]
for i in range(0,9):
print(chr(30+a[i]),end = "")
QBasic
With a quoted string, the following weighs in at 17 bytes.
PRINT "Code Golf"
For the second task, this is 91 bytes long.
DIM a(8)
DATA 37,81,70,71,2,41,81,78,72
FOR i = 0 TO 8
READ a(i)
PRINT CHR$(30 + a(i));
Note: QBasic is an interpreter, it does not generate executables.
Quackery
With Quoted Literals
say "Code Golf"
Without Quoted Literals
' [ 67 111 100 101 32 71 111 108 102 ] echo$
is marginally shorter but less interesting. For longer strings, encoding the text as a bignum rapidly becomes the more space efficient option. The text "Code Golf" is not quite long enough for the obvious improvement of using hexadecimal rather than decimal, as the digit reduction is less than the four character overhead of putting hex
and a space before the number.
2549578149779768531 9 times [ 112 /mod emit ] drop
R
I suspect there may be shorter methods, but these are my best attempt.
## easy way
cat("Code Golf")
## no quotes or string literals
cat(rlang::string(c(0x43, 0x6F, 0x64, 0x65, 0x20,
0x47, 0x6F, 0x6C, 0x66)))
- Output:
Code Golf
True BASIC
With a quoted string, the following weighs in at 22 bytes.
PRINT "Code Golf"
END
For the second task, this is 100 bytes long.
DIM a(9)
DATA 37,81,70,71,2,41,81,78,72
FOR i=1 to 9
READ a(i)
PRINT CHR$(30+a(i));
NEXT i
END
Raku
Not very interesting, as it's pretty much just standard, non-obscure Raku. The output string is so short, there isn't any easy way to golf it shorter than just printing it directly. 17 bytes.
print <Code Golf>
- Output:
Code Golf
Assuming we can't use the string literal in the source, the shortest I've come up with is:
print chrs (-32,12,1,2,-67,-28,12,9,3) »+»99 # 45 Chars, 47 bytes
print chrs (-3,㊶,㉚,㉛,-㊳,1,㊶,㊳,㉜) »+»㉎ # 37 Chars, 56 bytes
print <Dpef!Hpmg>.ords».pred.chrs # 33 Chars, 34 bytes. Somewhat cheaty as it _does_ contain a string literal, but not the same literal as the output
Same output for each. Of course, to actually run any of that code you need the Raku compiler at 18.0Kb, the nqp vm interpreter at 17.9 Kb and the moar virtual machine at 17.9Kb. (Or the Java virtual machine, which is remarkably difficult to come up with a size for...)
Run BASIC
With a quoted string, the following weighs in at 17 bytes.
print "Code Golf"
For the second task, this is 82 bytes long.
dim a(8):data 37,81,70,71,2,41,81,78,72:for i=0 to 8:read j:print chr$(30+j);:next
Note: Run BASIC is an interpreter, it does not generate executables.
Verilog
With a quoted string, the following weighs in at 64 bytes.
module main;
initial begin $write("Code Golf");
end
endmodule
For the second task, this is 102 bytes long.
module main;
initial begin $write("%c%c%c%c %c%c%c%c",67,111,100,101,71,111,108,102);
end
endmodule
- Output:
In both cases:
Code Golf
Vlang
The shortest possible program to print the required string is:
print("Code Golf")
If the program itself cannot contain string or character literals, then use byte list:
print([u8(67),111,100,101,32,71,111,108,102].bytestr())
Output in both cases
Code Golf
Wren
The shortest possible program (25 bytes) to print the required string is:
System.write("Code Golf")
The size of the executable needed to run this or indeed any other standalone program (Wren-cli on Linux) is 414,760 bytes. However, if Wren were being embedded in a minimal C program, then the size of the executable would be 17,320 bytes.
If the program itself cannot contain string or character literals, then the shortest program we've been able to come up with (71 bytes) - thanks to Thundergnat! - is:
for(c in[-32,12,1,2,-67,-28,12,9,3])System.write(String.fromByte(c+99))
- Output:
In both cases:
Code Golf
X86 Assembly
This is 100 bytes long (with CR+LF line endings). More useful than small, obfuscated source is small executable. This makes a 17-byte .COM file under MS-DOS. Assemble with: tasm and tlink /t. The xchg instruction is a single byte (as opposed to a straightforward 2-byte mov ah,9), and it takes advantage of the high byte of register bp being set to 09h when the program is started by MS-DOS. 09h selects the "display string" function.
.model tiny
.code
org 256
s:xchg ax,bp
mov dx,offset m
int 33
ret
m db "Code Golf$"
end s
XPL0
This is 19 characters long. I hate to say how big the executable is, but it's 54,400 bytes on the Raspberry Pi. Under MS-DOS a version of the compiler produces an executable as small as 6674 bytes.
Text(0,"Code Golf")
This version without a string or character literals is 33 characters long.
Text(0,[$65646f43,$6c6f4720,$e6])
- Output:
Code Golf
Yabasic
With a quoted string, the following weighs in at 12 bytes.
?"Code Golf"
For the second task, this is 118 bytes long.
dim a(8)
a(0)=37:a(1)=81:a(2)=70:a(3)=71
a(4)=2:a(5)=41:a(6)=81:a(7)=78:a(8)=72
for i=0 to 8
?chr$(30+a(i));
next
Note: Yabasic is an interpreter, it does not generate executables.
Z80 Assembly
Thanks to the Amstrad CPC's kernel, we can reduce our line count greatly by abstracting print routines to a single CALL
statement. In addition, WinAPE lets us load our executable directly into memory without the need for a disk by simply using an ORG
directive to define the starting address.
With Quoted Literals
Total: 11 lines.
org &200
ld hl,g
o:
ld a,(hl)
or a
ret z
call &bb5a
inc hl
jr o
g:
db "Code Golf",0
Hexdump of the executable:
7E B7 C8 CD 5A BB 23 18 F7 43 6F 64 65 20 47 6F 6C 66 00
Total: 19 bytes.
Without Quoted Literals
Total: 20 lines
org &200
q equ &bb5a
LD A,67
call q
LD A,111
call q
LD a,100
call q
LD a,101
call q
LD a,32
call q
LD a,71
call q
LD a,111
call q
LD a, 108
call q
LD a,102
jp q
Hexdump of the executable:
3E 43 CD 5A BB 3E 6F CD 5A BB 3E 64 CD 5A BB 3E 65 CD 5A BB 3E 20 CD 5A BB 3E 47 CD 5A BB 3E 6F CD 5A BB 3E 6C CD 5A BB 3E 66 C3 5A BB
Total: 45 bytes.