91
\$\begingroup\$

Dilbert is awesome
source: Dilbert, September 8, 1992

I'm hoping to add a new twist on the classic "Hello World!" program.

Code a program that outputs Hello World! without:

  • String/Character literals
  • Numbers (any base)
  • Pre-built functions that return "Hello World!"
  • RegEx literals

With the exceptions of "O" and 0.

†"O" is capitalized, "o" is not acceptable.

\$\endgroup\$
24
  • 1
    \$\begingroup\$ I hope by "numbers" you mean "numeric constants", because there probably wouldn't be much programming left without. \$\endgroup\$
    – J B
    Mar 11, 2011 at 21:42
  • 5
    \$\begingroup\$ One of [code-golf] and [code-challenge] please, not both. The point of these tags to to help people find questions with the rules they want to use. Essentially every question on this site should be a game of some kind or another. \$\endgroup\$ Mar 11, 2011 at 22:29
  • 4
    \$\begingroup\$ -1 We've already had Obfuscated Hello World, and I think this challenge is too similar. I'd have cast a "close as duplicate" vote, if I weren't a mod. \$\endgroup\$ Mar 11, 2011 at 22:35
  • 2
    \$\begingroup\$ @zzzzBov: I don't think it's different enough to warrant another question in the "hello world" theme; a different theme would have been better. But, that's just my opinion. \$\endgroup\$ Mar 11, 2011 at 23:39
  • 2
    \$\begingroup\$ Some people seem to assume that "O"* means they can have a string literal with any number of O’s, including zero. I don’t think that was the intention. Please clarify. \$\endgroup\$
    – Timwi
    Mar 12, 2011 at 21:12

107 Answers 107

92
\$\begingroup\$

C, 327 chars

#define O(O)-~O
#define OO(o)O(O(o))
#define Oo(o)OO(OO(o))
#define oO(o)Oo(Oo(o))
#define oo(o)oO(oO(o))
#define O0 putchar
main() {
    O0(OO(oO(!O0(~O(Oo(OO(-O0(~O(Oo(-O0(O(OO(O0(oo(oO(O0(O(oo(oO(OO(Oo(oo(oO(
    O0(oo(oo(!O0(O(OO(O0(O0(O(OO(Oo(O0(O(Oo(oo(oO(O0(oo(oo(oO(oo(oo(0))))))))
    ))))))))))))))))))))))))))))))))))))));
}

Strangely, it does't lose its beauty after preprocessing:

main() {
putchar(-~-~-~-~-~-~-~-~-~-~!putchar(~-~-~-~-~-~-~-~-putchar(~-~-~-~-~-~-
putchar(-~-~-~putchar(-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~putchar(
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~putchar(-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
-~-~-~-~-~-~-~-~-~-~-~-~-~!putchar(-~-~-~putchar(putchar(-~-~-~-~-~-~-~putchar
(-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~putchar(-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~0))))))))))));
}
\$\endgroup\$
5
  • 2
    \$\begingroup\$ Awesome. Missing the !, though. \$\endgroup\$
    – Mark Reed
    Nov 21, 2013 at 15:07
  • 1
    \$\begingroup\$ This is real art! \$\endgroup\$
    – Askaga
    Feb 7, 2014 at 19:42
  • \$\begingroup\$ I feel like the body could be shortened with even more preprocessors. Not sure if the code as a whole would benefeit though. \$\endgroup\$
    – Zwei
    Oct 27, 2016 at 3:37
  • \$\begingroup\$ @Zwei, It's certainly possible to shorten it. I made no effort to find the optimal set of macros. \$\endgroup\$
    – ugoren
    Oct 27, 2016 at 7:59
  • \$\begingroup\$ Why don't you include the parens in the CLANG #define? would save a few bytes, no? Edit: nevermind, just tried and now I see why that won't work ahaha \$\endgroup\$ Jan 24, 2017 at 18:15
85
\$\begingroup\$

Windows PowerShell, way too much

Yes, indeed, back in the day we had to write a »Hello world« using (almost exclusively) zeroes ...

&{-join[char[]]($args|% Length)} `
O00000000000000000000000000000000000000000000000000000000000000000000000 `
O0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O0000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000

On a more serious note:

Windows PowerShell, 25

Write-Host Hello World!

No string literal. The Hello World! in there just happens to be parsed as a string since PowerShell is in argument parsing mode there.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ How is that real?? O_o \$\endgroup\$
    – Josh
    Mar 11, 2011 at 22:06
  • 1
    \$\begingroup\$ I searched the internet for some examples or history of this. This is so weird. Could you provide any links? \$\endgroup\$ Dec 5, 2011 at 5:44
  • 3
    \$\begingroup\$ "Back in the day". :) You millennials crack me up 👍 \$\endgroup\$
    – user63571
    Jan 26, 2017 at 23:29
  • \$\begingroup\$ Oh. Unary. I get it. \$\endgroup\$ Jul 26, 2017 at 16:22
56
\$\begingroup\$

BrainFuck, 102 111 characters

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

Meets all of the rules.

Credit goes to Daniel Cristofani.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I can't decide if the rules should also state 0 and/or "O" must be used. It's a little mean to BrainFuck and golfscript, but they don't exactly have a hard time with this challenge. \$\endgroup\$
    – zzzzBov
    Mar 11, 2011 at 21:32
  • 46
    \$\begingroup\$ @zzzzBov: If they must be used, then Brainfuck code will just include them. They don't affect the program at all, though. \$\endgroup\$
    – Joey
    Mar 11, 2011 at 21:47
38
\$\begingroup\$

C, 182 bytes

#define decode(c,o,d,e,g,O,l,f) e##c##d##o
#define HelloWorld decode(a,n,i,m,a,t,e,d)
#define Puzzles(flog) #flog
#define CodeGolf Puzzles(Hello World!)
HelloWorld(){puts(CodeGolf);}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1,Lol,You still using the token pasting operator trick! Nice mate :-) \$\endgroup\$
    – Quixotic
    Mar 12, 2011 at 7:52
37
\$\begingroup\$

C program - 45

(cheating)

Lexically, this doesn't use any string literals or regex literals. It takes advantage of the stringification feature of the C preprocessor. s(x) is a macro that turns its argument into a string.

#define s(x)#x
main(){puts(s(Hello World!));}
\$\endgroup\$
1
  • 6
    \$\begingroup\$ For a very convenient notion of "lexically" :) But cheekiness FTW! \$\endgroup\$
    – J B
    Mar 12, 2011 at 22:39
25
\$\begingroup\$

Haskell - 143 characters

o%y=o.o.o$y;o&y=(o%)%y;o!y=o$o$(o%)&y
r=succ;w=pred;a=r%y;e=r$w&l;l=r!'O';o=r e;u=(w&)&a;y=r%l
main=putStrLn[w!o,o,l,l,y,w u,w$r&'O',y,a,l,e,u]

oy, that was woolly!

No numbers, no numeric operations, variables renamed for amusement.

Some exposition might be nice:

  • o%y, o&y, and o!y each applies the function o to y multiple times: 3, 9, and 29 times respectively. 29?!?! Yes, 29!
  • r and w are next and previous character, which when applied using the above higher- order functions can be made to get all the characters needed from 'O'.

The sequence of jumps needed is:

'O' +29    -> 'l'
'O'  +9 -1 -> 'W'
'l'  -9 +1 -> 'd'
'l'  +3    -> 'o'
'd'  +1    -> 'e'
'o'  +3    -> 'r'
'e' -29    -> 'H'
'r' -81    -> '!'
'!'  -1    -> ' '

  • Edit: (134 -> 144) Forgot to output an exclamation point, sigh....
  • Edit: (144 -> 143) Removed a unnecessary $, renamed # to ! for Hugs.
\$\endgroup\$
7
  • 1
    \$\begingroup\$ o rly? o.o .... \$\endgroup\$
    – Joey Adams
    Mar 12, 2011 at 7:12
  • \$\begingroup\$ It doesn't work. codepad.org/lyKyj1Ox \$\endgroup\$
    – alexia
    Mar 13, 2011 at 15:31
  • \$\begingroup\$ @Nyuszika7H That's because this service turns on some compiler options by default. It should compile with plain GHC. \$\endgroup\$
    – FUZxxl
    Mar 13, 2011 at 16:10
  • \$\begingroup\$ @Nyuszika7H It works just fine with GHC. The problem is that that service is using Hugs from 2006. There appear to be two things that Hugs can't deal with: 1) Using '#' as an operator. Changing to '!' makes it work. 2) The definitions of r=succ and w=pred run afoul of how Hugs implements the monomorphism restriction. Changing to r x=succ x and w x=pred x makes it work (at the cost of 4 characters). These seem to be problems in Hugs. \$\endgroup\$ Mar 13, 2011 at 18:09
  • \$\begingroup\$ Is it still valid after the @Community edit? \$\endgroup\$
    – user10766
    Feb 21, 2014 at 23:58
25
\$\begingroup\$

Unary, 10197 1137672766964589547169964037018563746793726105983919528073581559828 bytes

I'm surprised that no one's done this yet...

It's too long to post here, but it's 1137672766964589547169964037018563746793726105983919528073581559828 zeroes.

Or, more easily read: ~1067 zeroes.

Thanks to @dzaima for saving 10197 bytes

\$\endgroup\$
1
  • 2
    \$\begingroup\$ When I read "I once wrote an entire database using only zeroes", my first thought was "he must have done it in unary". +1 for being the only answer to work exactly how the comic did \$\endgroup\$
    – Cyoce
    Oct 17, 2016 at 23:45
23
\$\begingroup\$

Mathematica 12 chars

Only symbols, no strings.

Hello World!   

The ! is a factorial operator, but as the symbols Hello and World are undefined, returns the input unchanged.

If we modify the program a bit:

Hello=2;
World=3;
Hello World!  

Then it prints 12 (2 * 3!)

\$\endgroup\$
17
\$\begingroup\$

i386 assembly (Linux, gcc syntax), 440 442 435

Today's my assembly day, and after that I'll have had enough for a while. I allowed myself number 128, see below program for discussion of why. Nothing extraordinary: I'm just encoding "Hello World!" as assembly opcodes where that made sense without numeric constants, and filled in the rest with arithmetic.

#define M mov
M $0,%ecx;inc %cx;M %ecx,%ebx;inc %cx;M %ecx,%eax;add %ax,%ax
M %ecx,%edx;shl %cl,%dx;M (e),%ch;add %dl,%ch;dec %ch;M %ch,(l)
M %ch,(j);M %ch,(z);M $0,%ch;shl %cl,%edx;M %dl,(s);inc %dl
M %dl,(b);M (o),%dl;M %dl,(u);add %al,%dl;dec %dl;M %dl,(r)
M $m,%ecx;M $n,%edx;int $c;M %ebx,%eax;M $0,%ebx;int $c
.data
m:dec %eax;e:gs;l:es;j:es;o:outsl (%esi),(%dx)
s:es;push %edi;u:es;r:es;z:es;fs;b:es;n=.-m
t=(n+n)/n;c=t<<(t*t+t)

(assemble with gcc -nostartfiles hello.S -o hello, possibly -m32 depending on your arch)

Why the tolerance for 128? I need syscalls to actually show anything; Linux syscalls are on INT 80h (128 decimal); the only operand format for INT is immediate, so it's not possible to have anything else than a constant (to the code) there. I could (after I get sober) attempt to express it as a function of other symbolic constants in the code, likely n, but that's getting very boring for not much gain. I read the constraint on numbers as a way to prevent ASCII coding, and that's definitely not what I'm doing here, so I feel innocent enough to submit this. (FWIW, I also tried self-modifying code, but that segfaulted) There's now no 128 left either. The code's pure!

  • Edit1 reformatted to save lines; removed a numeric 1 (nobody noticed?!)
  • Edit2 compressed mov with CPP macros; eliminated the remaining 128.
\$\endgroup\$
1
  • 8
    \$\begingroup\$ This is how real men program. \$\endgroup\$
    – Etheryte
    Mar 4, 2014 at 17:30
16
\$\begingroup\$

Javascript - 305

A bit long but I like the method used.

O=0;O++;O=O.toString();alert([O+0+0+O+0+0+0,0+O+O+0+0+O+0+O,O+O+0+O+O+0+0,O+O+0+O+O+0+0,O+O+0+O+O+O+O,O+0+0+0+0+0,O+0+O+0+O+O+O,O+O+0+O+O+O+O, O+O+O+0+0+O+0,O+O+0+O+O+0+0,O+O+0+0+O+0+0,O+0+0+0+0+O].map(function(e){O=0;O++;O++;return String.fromCharCode(parseInt(e,O))}).reduce(function (a,b){return a+b}))
\$\endgroup\$
4
  • \$\begingroup\$ I like it. It would be better with function parameters as OO, OOO, but of course that would make it longer. \$\endgroup\$
    – zzzzBov
    Mar 12, 2011 at 19:31
  • \$\begingroup\$ Really nice. Does the ',new String()' need to be there at the end? It seems to work without it. \$\endgroup\$ Mar 12, 2011 at 23:32
  • 4
    \$\begingroup\$ Ah, no it doesn't. Sometimes I forget just how weakly typed Javascript is. \$\endgroup\$
    – david4dev
    Mar 12, 2011 at 23:38
  • \$\begingroup\$ You don't need left 0 bits ('0+') either, and the space and the exclamation have two left 0 bits. Total reduction of 28 characters. \$\endgroup\$ Mar 12, 2011 at 23:50
12
\$\begingroup\$

C# (131 chars)

141 chars 142 chars

enum X{Hello,World,A,B=A<<A<<A}class Y{static void Main(){var c=(char)X.B;System.Console.Write(X.Hello.ToString()+c+++X.World+c);}}

Readable:

// Define some constants (B = 32)
enum X { Hello, World, A, B = A << A << A }
class Y
{
    static void Main()
    {
        // Generate the space (character #32)
        var c = (char) X.B;

        // Remember that “!” is character #33
        System.Console.Write(X.Hello.ToString() + c++ + X.World + c);
    }
}
\$\endgroup\$
6
  • 2
    \$\begingroup\$ this is twisted and nice. I love it. \$\endgroup\$
    – jcolebrand
    Mar 14, 2011 at 16:33
  • \$\begingroup\$ +1 nice way to generate certain numbers. Gotta remember that! \$\endgroup\$ Sep 19, 2013 at 6:03
  • \$\begingroup\$ LINQPad program, 102 chars: enum X{Hello,World,A,B=A<<A<<A}void Main(){var c=(char)X.B;(X.Hello.ToString()+c+++X.World+c).Dump();} \$\endgroup\$
    – Cœur
    Jul 8, 2014 at 16:10
  • \$\begingroup\$ .ToString() -> +"" saves some cahrs \$\endgroup\$
    – Firo
    Jul 10, 2014 at 10:18
  • 3
    \$\begingroup\$ @Firo: ... and breaks the rules. (No string literals allowed.) \$\endgroup\$
    – Timwi
    Jul 10, 2014 at 13:55
8
\$\begingroup\$

JavaScript, 88

t=!0<<-~-~-~-~!0
r=[]
for(i in{Hello:"O",World:0})r+=i+String.fromCharCode(t++)
alert(r)

99

Many thanks to @Timwi for the suggestions

removed ternary operator:

o={Hello:"O",World:0}
t=!0<<-~-~-~-~!0
c=String.fromCharCode
r=c(0)
for(i in o)r+=i+c(t++)
alert(r)

103
aliased String.fromCharCode

o={Hello:"O",World:0}
t=!0<<-~-~-~-~!0
c=String.fromCharCode
for(i in o)o[i]?r=i+c(t):alert(r+i+c(++t))

117
Switched if-else to ternary operator

o={Hello:"O",World:0},t=!0<<-~-~-~-~!0
for(i in o)o[i]?r=i+String.fromCharCode(t):alert(r+i+String.fromCharCode(++t))

125
I'm keeping the "O" just to have an "O" in the program.

o={Hello:"O",World:0},t=!0<<-~-~-~-~!0
for(i in o)if(o[i])r=i+String.fromCharCode(t)
else alert(r+i+String.fromCharCode(++t))

133

o={Hello:"O",World:0},t=!0<<(!0+!0<<!0)+!0
for(i in o)if(o[i])r=i+String.fromCharCode(t)
else r+=i+String.fromCharCode(t+!0)
alert(r)
\$\endgroup\$
12
  • \$\begingroup\$ @Joey, that's largely the point. \$\endgroup\$
    – zzzzBov
    Mar 11, 2011 at 23:35
  • \$\begingroup\$ @Joey, i read on the codegolf meta that one should avoid answering their own question for some time to encourage others to try various different approaches. My plan was to uncomment it in a day or two. \$\endgroup\$
    – zzzzBov
    Mar 11, 2011 at 23:43
  • \$\begingroup\$ @Joey, wasn't working on my own machine at the time, and I didn't feel like e-mailing myself the answer when I could just post it in a comment. \$\endgroup\$
    – zzzzBov
    Mar 11, 2011 at 23:57
  • \$\begingroup\$ @Joey: "There are only five of those" is wrong. Anyone can suggest an edit. Just click on "edit" between "link" and "flag" and you can see the code. \$\endgroup\$
    – John
    Mar 12, 2011 at 3:57
  • \$\begingroup\$ @zzz: "that's largely the point" Anyone can still see it. Anyone can suggest an edit by clicking on "edit" in between "link" and "flag", which will bring up the edit dialog, revealing your code. \$\endgroup\$
    – John
    Mar 12, 2011 at 3:59
8
\$\begingroup\$

Brainfuck, 111 bytes

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

Algorithm explained

Increment cell 0 to 10 (it will be loop counter)
Repeat 10 times ; will stop at cell 0
  Increment cell 1 to 7
  Increment cell 2 to 10
  Increment cell 3 to 3
  Increment cell 4 to 1
Increment cell 1 by 2 and output it ; Thus, output ASCII 72 'H'
etc. for all symbols in 'Hello World!'

Longer version without loop, 389 bytes:

+++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++.+++++++++++++++++
++++++++++++.+++++++..+++.-------------------
---------------------------------------------
---------------.+++++++++++++++++++++++++++++
++++++++++++++++++++++++++.++++++++++++++++++
++++++.+++.------.--------.------------------
---------------------------------------------
----.-----------------------.
\$\endgroup\$
0
7
\$\begingroup\$

GolfScript, 63 chars

[0))):O.)?O.*-.O.?+)).O.*+((..O+.O(.O+?.O-O*@.O+O.+$.O.*-).O/]+

What, no GolfScript entry yet?

This one uses a single numeric literal 0 and a variable named O (which is used to store the number 3). Everything else is arithmetic and stack manipulation. The string Hello World! is built up from its ASCII codes, character by character.

Here's how it works:

[             # insert start-of-array marker
  0))):O      # increment 0 thrice to get 3, and save it in the variable O
  .)?O.*-     # calculate 3^(3+1) - 3*3 = 81 - 9 = 72 = "H" 
  .O.?+))     # calculate 72 + 3^3 + 1 + 1 = 72 + 27 + 2 = 101 = "e"
  .O.*+((     # calculate 101 + 3*3 - 1 - 1 = 101 + 9 - 2 = 108 = "l"
  .           # ...and duplicate it for another "l"
  .O+         # calculate 108 + 3 = 111 = "o"
  .           # ...and duplicate it for later use
  O(.O+?      # calculate (3-1)^(3-1+3) = 2^5 = 32 = " "
  .O-O*       # calculate (32 - 3) * 3 = 29 * 3 = 87 = "W"
  @           # pull the second 111 = "o" to the top of the stack
  .O+         # calculate 111 + 3 = 114 = "r"
  O.+$        # copy the (3+3 = 6)th last element on the stack, 108 = "l", to top
  .O.*-)      # calculate 108 - (3*3) + 1 = 108 - 9 + 1 = 100 = "d"
  .O/         # calculate int(100 / 3) = 33 = "!"
]             # collect everything after the [ into an array
+             # stringify the array by appending it to the input string
\$\endgroup\$
6
\$\begingroup\$

Python (126 130)

O=ord("O")
N=O/O
T=N+N
R=N+T
E=T**R
E<<T
print'O'[0].join(chr(c+O)for c in[N-E,E*R-T,_-R,_-R,_,N-_-E-E,E,_,_+R,_-R,E*R-R,T-_-E-E])
\$\endgroup\$
3
  • \$\begingroup\$ The literal '' is not allowed. \$\endgroup\$
    – RomanSt
    Mar 12, 2011 at 21:12
  • 1
    \$\begingroup\$ @romkyns: Very true, fixed. \$\endgroup\$ Mar 13, 2011 at 0:40
  • 5
    \$\begingroup\$ Not fixed (wrong output). You are probably thinking of [:0]. \$\endgroup\$ Nov 22, 2013 at 15:39
6
\$\begingroup\$

QR with halfblocks (169) 121 characters

With QR-Code by using UTF-8 Half-blocks characters:

▗▄▄▄▗▄▖▗▄▄▄  
▐▗▄▐▝█▙▐▗▄▐  
▐▐█▐▝▄ ▐▐█▐  
▐▄▄▟▗▗▗▐▄▄▟  
▗▗▖▄▞▝ ▗ ▖▄  
 ▟▜ Code ▀▟  
 ▙▀ Golf ▘▚  
▗▄▄▄▐▗▘▟▙▝▝  
▐▗▄▐▝▀▛▘▘█▖  
▐▐█▐▐▖▐▝▖▜▘  
▐▄▄▟▗ ▌█▛▗▝  3

Unfortunely, this won't render nicely there. There is a little snippet with appropriate style sheet, but.. No! The language presented here is not HTML! Language presented here is QR Code! (HTML and CSS is used here only to work around presentation bug!)

 body {
    font-family: monospace;
    line-height:97%;
 }
▗▄▄▄▗▄▖▗▄▄▄  <br>
▐▗▄▐▝█▙▐▗▄▐  <br>
▐▐█▐▝▄ ▐▐█▐  <br>
▐▄▄▟▗▗▗▐▄▄▟  <br>
▗▗▖▄▞▝ ▗ ▖▄  <br>
 ▟▜ Code ▀▟  <br>
 ▙▀ Golf ▘▚  <br>
▗▄▄▄▐▗▘▟▙▝▝  <br>
▐▗▄▐▝▀▛▘▘█▖  <br>
▐▐█▐▐▖▐▝▖▜▘  <br>
▐▄▄▟▗ ▌█▛▗▝  <br>
             <br>

QR with halfblocks (169)

  ▛▀▀▌▚▝▐▀▌▛▀▀▌
  ▌█▌▌▖▞▚▚▘▌█▌▌
  ▌▀▘▌ ▚▛▞ ▌▀▘▌
  ▀▀▀▘▚▌▙▘▌▀▀▀▘
  ▄▙▛▚▜▀▄▀█▖▝▄▌
  ▖▄▄▘▖▄▄▄▟▗ ▀▘
  ▜Code  golf!▌
  ▚▟▘▘▝▙▛▚▐▀▖▜▘
  ▘▘ ▀▛▗▚▗▛▀▌▄ 
  ▛▀▀▌▟▌▜▖▌▘█▐▘
  ▌█▌▌▘█▌▟█▜▙▐ 
  ▌▀▘▌▚▌▌█▗▝▌▚▘
  ▀▀▀▘ ▝▘▘▀▀▀▀▘

Idealy, this could look like:

QR:Hello World!

\$\endgroup\$
8
  • \$\begingroup\$ Use your smart phone to see this or zbar under linux. (sample: xwd | xwdtopnm | zbarimg /dev/stdin ) \$\endgroup\$ Jan 28, 2014 at 21:40
  • \$\begingroup\$ Isn't Code golf! a character literal? \$\endgroup\$
    – ugoren
    Feb 5, 2014 at 8:10
  • 2
    \$\begingroup\$ I actually think this answer, though creative, doesn't qualify because it isn't a program. QR is a way to encode data, not a programming language. \$\endgroup\$
    – ugoren
    Feb 5, 2014 at 9:20
  • 2
    \$\begingroup\$ Also what about languages like postscript, svg or other presentation languages already used there!? A way to encode is a language anyway... I think! \$\endgroup\$ Feb 5, 2014 at 10:07
  • 1
    \$\begingroup\$ I used HTML (minus JS) as an example for what's not a language. Same goes for QR. See these criteria. \$\endgroup\$
    – ugoren
    Nov 20, 2015 at 15:25
5
\$\begingroup\$

J, 250

oo=:#a.
o0=:<.o.^0
o0o=:%:%:%:oo
ooo=:p:^:(-*oo)
o=:<.(^^^0)*(^^0)*(^^0)
o00=:o,~(,[)o0(**(**[))o0o
oo0=:*/p:(!0),>:p:!0
echo u:(o0(**(**]))o0o),(ooo ooo ooo(o.o.^^^0)*oo),o00,(+:%:oo),(oo0-~!p:>:>:0),o,(<.-:o.o.^o.^0),(>:p:(]^])#o00),(*:#0,#:oo),oo0

I had entirely too much fun with this one, and I learned a little bit more J to boot. Also, ooo ooo ooo may be the stupidest code I've ever written.

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

ForWhile 22 bytes

!dlroW olleH{}+~(~~@#)

uses ForWhiles ability to read its own source code

online Interpreter

Explanation

! invert empty stack, will push one

dlroW olleH no-ops (all letters and white spaces are interpreted as comments)

{} empty procedure, push current instruction pointer (-14)

+~ add one and flip bits (12 length of the string)

(~~@#) print the first 12 characters of the program (stored at address -1 to -12)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Jul 12 at 17:37
4
\$\begingroup\$

Lua 144 97 86 chars

A different approach, based on the fact that table keys are also strings, and the fact that #Hello == #World == 32 == string.byte'\n'

e=#"O"t=e+e for k,v in pairs{Hello=0,World=e}do T=t^#k io.write(k,string.char(T+v))end

145 char solution

  • no strings except "O" or 0
  • no Regexes
  • no pre-built functions

Did delta encoding of the bytes, then some primenumbers etc etc :)

Golfed version:

e=#"O"d=e+e t=d+e v=d+t z=v+t T=t^d*d^t n=0 for l,m in pairs{T,T/t-z,z,0,d,-T-z,z*z+t*d,T/d,d,-d*t,-t^d,-T+v}do n=n+m io.write(string.char(n))end

Commented:

-- without numbers, strings, regex
-- except "O" and 0
e=#"0"
t=e+e --2
d=t+e -- 3
v=d+t -- 5
z=v+t -- 7
n=0
T=t^d*d^t -- 72 = 2^3+3^2
for l,m in pairs{T, --72
T/t-z, -- 29 = 72/2-7
z, --7
0, -- 0
d, -- 3
-T-z, -- -79 = -72 - 7
z*z+t*d, -- 55 = 7*7 + 2*3
T/d, -- 24 = 72/3
d, -- 3
-d*t, -- -6
-t^d, -- -8
-T+v -- -67 = -72+5
} do
    n=n+q[k]
    io.write(string.char(n))
end

Edit: Changed multiple O strings, and found some more optimalisations.

\$\endgroup\$
4
  • \$\begingroup\$ "O"* - I think the * was for the footnote, not "any number of O's \$\endgroup\$
    – RomanSt
    Mar 12, 2011 at 21:13
  • \$\begingroup\$ Well, regex'ing is what I naturally do :p. Could replace it at the cost of 5 or 3 extra characters for respectively the first and second solution \$\endgroup\$
    – jpjacobs
    Mar 12, 2011 at 21:33
  • \$\begingroup\$ But anyway, I'll edit them out, thanks for the heads up \$\endgroup\$
    – jpjacobs
    Mar 12, 2011 at 21:45
  • \$\begingroup\$ Hello and World in table constructor are still string literals - that's just a syntatic sugar. \$\endgroup\$ Dec 21, 2015 at 10:00
4
\$\begingroup\$

Scala (357 423 361 characters)

Not the shortest answer, unfortunately, but hoping to get bonus marks for the most use of 'O' and '0'

def h(){type t=scala.Char;def OO(c:t)={(c-('O'/'O')).toChar};def O(c:t)={OO(OO(OO(c)))};def O00(c:t)={(c+('O'/'O')).toChar};def O0(c:t)={O00(O00(O00(c)))};val l=O('O');val z=O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(0)))))))))));print(OO(O(O('O'))).toString+(OO(O(O(O('O')))).toString+l+l+'O'+OO(z)+O0(O0(O0(OO('O'))))+'O'+O0('O')+l+OO(OO(O(O(O('O')))))+z).toLowerCase)}

Previously:

def h(){type t=scala.Char;print(OO(O(O('O'))).toString+(OO(O(O(O('O')))).toString+O('O')+O('O')+'O'+OO(O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(0))))))))))))+O0(O0(O0(OO('O'))))+'O'+O0('O')+O('O')+OO(OO(O(O(O('O')))))+O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(0)))))))))))).toLowerCase);def OO[Char](c:t)={(c-('O'/'O')).toChar};def O[Char](c:t)={OO(OO(OO(c)))};def O00[Char](c:t)={(c+('O'/'O')).toChar};def O0[Char](c:t)={O00(O00(O00(c)))}}

Old (illegal) version:

def h(){type t=scala.Char;print(""+OO(O(O('O')))+(""+OO(O(O(O('O'))))+O('O')+O('O')+'O'+OO(O(O(O(O(O('0'))))))+O0(O0(O0(OO('O'))))+'O'+O0('O')+O('O')+OO(OO(O(O(O('O')))))+O(O(O(O(O('0')))))).toLowerCase);def O0[Char](c:t)={O00(O00(O00(c)))};def O[Char](c:t)={OO(OO(OO(c)))};def OO[Char](c:t)={(c-('O'/'O')).toChar};def O00[Char](c:t)={(c+('O'/'O')).toChar}}
\$\endgroup\$
2
  • \$\begingroup\$ I believe the empty string ("") and strings/character literals containing the character zero ('0') are not allowed. Only the string "O" (capital O) and the number 0 are. \$\endgroup\$
    – Timwi
    Mar 12, 2011 at 22:59
  • \$\begingroup\$ Bugger. Some editing required then... \$\endgroup\$
    – Gareth
    Mar 12, 2011 at 23:08
4
\$\begingroup\$

C (or C++) (body segment: 49) (cheating)

when compiling, compile to a binary called Hello\ World\!, the code is:

#include <stdio.h>
#include <string.h>
int main(int i,char**a)
{
  int j=i+i,k=j<<j;puts(strrchr(*a,'O'-(k<<j))+i);
}

The strrchr segment is required to remove the full path in the event the program name passed in contains the full path, also no arguments must be passed in..

Typical compile line could be: gcc -o Hello\ World\! foo.c

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

Java 389 characters

spotted a unnecessary declaration

class A{static int a=0,b=a++,f=a,e=a++;static char p(String s){return(char)Byte.parseByte(s,a);}public static void main(String[]z){long x=e,y=b;String c=((Long)x).toString(),d=((Long)y).toString();char l=p(c+c+d+c+c+d+d),m=p(c+c+d+d+c+d+c),o=(char)(l+a+f),_=p(c+d+d+d+d+d),$=_++;System.out.print(new char[]{p(c+d+d+c+d+d+d),m,l,l,o,$,p(c+d+c+d+c+c+c),o,(char)(o+a+f),l,(char)(m-f),_});}}

History is in edit history now the original ungolfed version readable:

// H  e  l    l   o      W  o  r   l    d  !
//72,101,108,108,111,32,87,111,114,108,100 33
import static java.lang.Integer.*;
class A
{
    
    
    public static void main(String[] args)
    {
        Integer a=0,b=a++,e=a++;  // making a 0 a 1 and a 2 which is required later;
        String c=e.toString(),d=b.toString(),z=c.substring(0,0);  //
        
        String H = ((char)parseInt(d+c+d+d+c+d+d+d,a))+z+  // make binary values and then to char
                (char)parseInt(d+c+c+d+d+c+d+c,a)+
                (char)parseInt(d+c+c+d+c+c+d+d,a)+
                (char)parseInt(d+c+c+d+c+c+d+d,a)+
                (char)parseInt(d+c+c+d+c+c+c+c,a)+
                (char)parseInt(d+d+c+d+d+d+d+d,a)+
                (char)parseInt(d+c+d+c+d+c+c+c,a)+
                (char)parseInt(d+c+c+d+c+c+c+c,a)+
                (char)parseInt(d+c+c+c+d+d+c+d,a)+
                (char)parseInt(d+c+c+d+c+c+d+d,a)+
                (char)parseInt(d+c+c+d+d+c+d+d,a)+
                (char)parseInt(d+d+c+d+d+d+d+c,a)
                ;
        System.out.println(H);  //obvious
    }
\$\endgroup\$
3
\$\begingroup\$

C++, 141, 146

First time trying one of these, can probably be improved quite a bit yet:

char o='O'/'O',T=o+o,X=T+o,f=T+X,t=f+f,F=t*f,h=F+F,l=h+t-T,O=l+X;
char c[]={F+t+t+T,h+o,l,l,O,o<<f,h-t-X,O,l+f+o,l,h,0};
cout<<c;

EDIT:

Stole the divide trick from another post, can't believe I didn't think of that :(

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You're technically supposed to include the entire working program in the character count, including things like main, any included libraries, std::, etc etc \$\endgroup\$
    – Wug
    Nov 9, 2012 at 22:01
3
\$\begingroup\$

Haskell - 146

a:b:c:f:g:s:j:z=iterate(\x->x+x)$succ 0
y=[f+j,d+a,c+s+h,l,a+b+l,s,f-s+o,o,a+b+o,l,l-f,a+s]
[h,e,l,_,o,_,w,_,r,_,d,x]=y
main=putStrLn$map toEnum y

I figured pattern matching would give Haskell a huge leg up, in particular because you can initialize powers of two like so:

one:two:four:eight:sixteen:thirty_two:sixty_four:the_rest = iterate (*2) 1

However, as seen in MtnViewMark's Haskell answer (which deserves many many upvotes, by the way) and other answers, better compression can be achieved by using more than just + and -.

\$\endgroup\$
0
3
\$\begingroup\$

Clojure - 46 chars

(map print(butlast(rest(str'(Hello World!)))))

Note that Hello and World! are symbols, not literals of any kind.

\$\endgroup\$
3
\$\begingroup\$

C++

/*
Hello World!
*/
#define CodeGolf(r) #r
#include<iostream>
using namespace std;
int main()
{
    char str[*"O"];
    freopen(__FILE__,CodeGolf(r),stdin);
    gets(str);gets(str);puts(str);
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ The 100 violates the rule about numbers. Please replace it with something more amusing. \$\endgroup\$
    – Joey Adams
    Mar 12, 2011 at 23:03
  • \$\begingroup\$ @joey-adams Thanks for pointing out. Changed that. \$\endgroup\$
    – fR0DDY
    Mar 13, 2011 at 5:15
  • \$\begingroup\$ Line 7: error: ISO C++ forbids declaration of 'main' with no type \$\endgroup\$ Mar 15, 2011 at 2:31
  • \$\begingroup\$ @george-edison Corrected. Though it worked without int on g++ \$\endgroup\$
    – fR0DDY
    Mar 15, 2011 at 3:07
3
\$\begingroup\$

PHP – 49 chars

<?=Hello.chr(($a=-~-~-~0).~-$a).World.chr($a.$a);

Changelog:

  • (73 -> 86) Forgot to output an exclamation point... sigh
  • (86 -> 57) Uses a single variable with incrementing
  • (57 -> 51) Changed to use bitwise operators on 0
  • (51 -> 49) More bitwise operators
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Pretty sure that the constants would count as string literals because of the conversions. \$\endgroup\$ Mar 13, 2011 at 18:23
  • \$\begingroup\$ @Bass5098 Thanks for your edit! I approved it. \$\endgroup\$
    – alexia
    Mar 13, 2011 at 18:34
  • \$\begingroup\$ @Bass5098 that doesn't work, you need to have chars 32 and 33, not 21 and 22. <?$a=-~-~-~0;echo Hello.chr($a.$a-1).World.chr($a.$a); works, but it's 54 chars. \$\endgroup\$
    – zzzzBov
    Mar 16, 2011 at 18:07
  • \$\begingroup\$ @zzzzBov I could not figure out how to clear the edit initially, and forgot to roll it back once it was approved. \$\endgroup\$ Mar 16, 2011 at 22:52
  • \$\begingroup\$ whoops, i left the 1 in there. There I go breaking my own rules...<?$a=-~-~-~0;echo Hello,chr($a.~-$a),World,chr($a.$a); is what I should have used, still 54 chars. \$\endgroup\$
    – zzzzBov
    Mar 16, 2011 at 23:34
3
\$\begingroup\$

Python, 106

o=-~-~-~0
L=o+o
d,W=-~L,o**o
_=o*W
print str().join(chr(L+W+_-i)for i in[d*L,L+d,L,L,o,-~_,W,o,0,L,d+d,_])
\$\endgroup\$
4
  • \$\begingroup\$ Use '' (with no preceding space) instead of str() \$\endgroup\$ Mar 19, 2013 at 11:29
  • 2
    \$\begingroup\$ Also, VERY nice! :) Btw, the multiple assignment is not saving any characters \$\endgroup\$ Mar 19, 2013 at 11:39
  • \$\begingroup\$ @aditsu: I can't, that would be a string literal. Thanks for your comments. :) \$\endgroup\$
    – flornquake
    Mar 20, 2013 at 14:49
  • \$\begingroup\$ Oh oops, somehow the empty string didn't register as a literal in my mind \$\endgroup\$ Mar 20, 2013 at 15:23
3
\$\begingroup\$

Perl, 186 bytes

@A=(0,0,0,0);@B=(@A,@A);@C=(@B,@B);@D=(@C,@C);@E=(@D,@D);@d=(@E,@D,@A);$l=[@d,@B];$o=[@$l,0,0,0];print(chr@$_)for[@E,@B],[@d,0],$l,$l,$o,\@D,[@E,@C,@A,0,0,0],$o,[@$o,0,0,0],$l,\@d,[@D,0]

Each chararcter is printed via its ordinal number, which is the length of an array. The construction of the arrays are optimized via the binary representation of the character numbers.

Ungolfed:

@A = (0, 0, 0, 0);          # A = 2^2
@B = (@A, @A);              # B = 2^3
@C = (@B, @B);              # C = 2^4
@D = (@C, @C);              # D = 2^5
@E = (@D, @D);              # E = 2^6

# d = 100 = 0x64 = 1100100b
@d = (@E, @D, @A);          # d = 2^6 + 2^5 + 2^2 

# l = 108 = 0x6C = 1101100b
$l = [@d, @B];              # l = d + 2^3

# o = 111 = 0x6F = 1101111b
$o = [@$l, 0, 0, 0];        # o = l + 3

print (chr @$_) for
    [@E, @B],              # "H"    H  =  72 = 0x48 = 1001000b = 2^6 + 2^3
    [@d, 0],               # "e"    e  = 101 = 0x65 = 1100101b = d + 1  
    $l, $l, $o,            # "llo"
    \@D,                   # " "   ' ' =  32 = 0x20 = 0100000b = 2^5
    [@E, @C, @A, 0, 0, 0], # "W"    W  =  87 = 0x57 = 1010111b = 2^6 + 2^4 + 2^2 + 3
    $o,                    # "o"
    [@$o, 0, 0, 0],        # "r"    r  = 114 = 0x72 = 1110010b = o + 3
    $l, \@d,               # "ld"
    [@D,0]                 # "!"    !  =  33 = 0x21 = 0100001b = 2^5 + 1
\$\endgroup\$
1
  • \$\begingroup\$ seems that my binary vodoo inspired you :) +1 \$\endgroup\$
    – masterX244
    Apr 13, 2014 at 18:18
3
\$\begingroup\$

Perl 6, 199 bytes

my \O=+[0];my \t=O+O;my \T=t+O;my \f=(t+t,O);my \F=t+T;my \S=T+T;my \L=(S,F);
say [~] ((S,T),(|L,t,0),(my \l=(|L,T,t)),l,(my \o=(|l,O,0)),F,(S,|f,t,0),o,(|L,|f),l,(|L,t),(F,0),)
.map({chr [+] t X**@_})

(newlines are added to reduce width, but are unnecessary so are not counted)


Hello World! is encoded as a list of lists of the powers of 2 of each letter.

There is exactly one place where I have a literal 0 that is used for anything other than a 0. It is used to create a one-element list, which is immediately turned into the number 1 with the numeric prefix operator (+[0]).

my \O=+[0];    # one          # (List.elems)
my \t=O+O;     # two
my \T=t+O;     # Three
my \f=(t+t,O); # (four, one)  # <W r>
my \F=t+T;     # five
my \S=T+T;     # six

my \L=(S,F);   # lowercase letter  # (6,5)

say [~] (
  (S,T),            # H
  (|L,t,0),         # e
  (my \l=(|L,T,t)), # l
  l,                # l  reuse <l>
  (my \o=(|l,O,0)), # o  reuse <l>, add 0,1
  F,                # ␠
  (S,|f,t,0),       # W
  o,                # o  reuse <o>
  (|L,|f),          # r
  l,                # l  reuse <l>
  (|L,t),           # d
  (F,0),            # !
).map({chr [+] t X**@_})
\$\endgroup\$

Your Answer

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

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