145
\$\begingroup\$

Write a program that prints "Hello, World!". But also, if you take only the first, third, fifth, etc. characters of your program, the resulting program should still print "Hello, World!".

If your program is:

abc
def

It should output "Hello, World!", but so should

acdf

No solutions with fewer than 2 characters.

\$\endgroup\$
15
  • 3
    \$\begingroup\$ Can there be whitespace around the output? \$\endgroup\$ Jun 26, 2017 at 20:20
  • 24
    \$\begingroup\$ Amazing first post! \$\endgroup\$
    – Adám
    Jun 26, 2017 at 22:49
  • 13
    \$\begingroup\$ Seeing all the answers with "HHeelllloo" reminded me of speaking Whale. \$\endgroup\$ Jun 27, 2017 at 4:19
  • 36
    \$\begingroup\$ Pro tip for avoiding "Hello, World!" built-ins in challenges like this: use a slightly different string of similar complexity like "Greetings, Earthlings!" \$\endgroup\$ Jun 27, 2017 at 7:15
  • 7
    \$\begingroup\$ "No solutions with fewer than 2 characters." Amazing. \$\endgroup\$
    – Rob Grant
    Jun 28, 2017 at 9:10

90 Answers 90

1 2
3
2
\$\begingroup\$

Cubically, 155 bytes

R U + 4 3 2 @ 6 + 5 0 - 4 @ 6 + 3 - 4 @ 6 @ 6 + 1 - 0 0 @ 6 - 3 3 1 @ 6 - 0 0 @ 6 + 4 1 1 0 @ 6 + 0 0 0 0 @ 6 + 1 - 0 0 @ 6 - 0 @ 6 - 2 + 4 @ 6 - 3 3 1 @ 6

The new parser strips out all spaces ¯\_(ツ)_/¯ so either way the code is interpreted like this:

RU+432@6+50-4@6+3-4@6@6+1-00@6-331@6-00@6+4110@6+0000@6+1-00@6-0@6-2+4@6-331@6

Try it online! (spaced)

Try it online! (every other character)

\$\endgroup\$
2
\$\begingroup\$

Stax, 33 bytes

"zHzezlzlzoz,z zWzozrzlzdz!z"'z -

Run and debug it

Every other character, 17 bytes

"Hello, World!"z-

Run and debug it

In the first version, 'z - remove all zs from the string. In the second version, z by itself is empty set, and z- is noop.

\$\endgroup\$
2
\$\begingroup\$

Lost, 113 bytes

v<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>.%?"Hello, worldvU"-+@
v>v%v?v"vHvevlvlvov,v 
wvovrvlvdvvvUv"v-v+v@

Try it online!

With removal, 57 bytes

v<<<<<<<<<<<<<<<<<<<<<
.?Hlo olv"+
>%?"Hello, worldvU"-+@

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Vyxal, 7 bytes

ka`a`kH

Try it Online!

Pretty straightforward: The kH at the end is a builtin for Hello, World!, and that's the only thing that gets implicitly printed.

With characters removed:

k``H

Try it Online!

This makes use of a quirk in Vyxal where you can put a string in the middle of a constant diagraph, and the constant will be pushed after the string, making this equivalent to ``kH, which pushes the same builtin as before and prints it.

\$\endgroup\$
2
2
\$\begingroup\$

Vyxal, 29 bytes

`HHeelllloo,,  WWoorrlldd!!`√

Try it Online!

`Hello, World!√

Try it Online!

Without using the kH builtin, which returns "Hello, World!"

`HHeelllloo,,  WWoorrlldd!!`  # String
                            √ # Get every second character
`Hello, World!  # String (unterminated)
              √ # Ignored due to parsing bug
\$\endgroup\$
1
\$\begingroup\$

Pyth, 31 bytes

%y1t"pHEeGlGlhoz,e QwjolrDlBdJ!

eh

\$\endgroup\$
1
\$\begingroup\$

Forth, 61 bytes

This uses the fact that \ makes the rest of the line a comment.

 \  . (   H e l l o ,   W o r l d ! )   \
 .( Hello, World! )

Full program

Remove every other byte to get:

  .( Hello, World!) \ (Hlo ol!)

Every other

\$\endgroup\$
1
\$\begingroup\$

Japt, 32 28 bytes

Wrote this up and then noticed Oliver beat me to it with the same method in 05AB1E.

Note: The empty line after the comma contains a space character and the empty line after the "W" contains an unprintable.

`
H
e
¥
o
,
 
W

l
d
!
` rR

Test it

With every second character removed, it becomes (with the unprintable still present between the "W" and the "l"):

`He¥o, Wld!`r

Test it

\$\endgroup\$
1
\$\begingroup\$

Swift, 77 bytes

/**/print("Hello, World!")//  * / p r i n t ( " H e l l o ,   W o r l d ! " )

Every other character:

/*pit"el,Wrd"/ */print("Hello, World!")
\$\endgroup\$
1
\$\begingroup\$

Java 8, 123 122 bytes

Here is the full code

/**/()->System.out.print("Hello, World!")//_*_/_(_)_-_>_S_y_s_t_e_m_._o_u_t_._p_r_i_n_t_(_"_H_e_l_l_o_,_ _W_o_r_l_d_!_"_)_

Try it online here


Here is the 'every other' code

/*(-Sse.u.rn(Hlo ol!)/*/()->System.out.print("Hello, World!")

Try it online here

\$\endgroup\$
1
\$\begingroup\$

LOGO, 63 bytes

 ;p r [ H e l l o ,   W o r l d ! ] e r [
 pr[Hello, World!] ;]

With characters at even index removed

 pr[Hello, World!]er[ rHlo ol! ]

Explanation:

; starts an inline comment in Logo. Therefore the first program is equivalent to

 pr[Hello, World!] 

which obviously print "Hello, World!".

In the second program, a function er is invoked. er stands for erase, receive a list in a suitable format as input, and erase some contents specified in the list (the details does not matter). The key point is that er does not return anything, so it is a shorter alternative to ignore, which ignores its contents.

\$\endgroup\$
1
\$\begingroup\$

x86 machine code, 73 bytes

Inspired by Евгений Новиков's solution, I thought it should be doable with less tricks, i.e., just jumping around to otherwise "disjoint" codes for all three variants. I'm still trying with a smart variant that uses lodsb; lodsb as central point (so only one string constant is needed for all variants)

EB 14 00 00 8A 8A 17 16 01 01 B4 B4 09 09 CD CD
21 21 CD CD 20 20 8A 1f 01 B4 09 CD 21 CD 20 48
65 6c 6c 6f 2c 20 57 6f 72 6c 64 21 00 48 48 65
65 6c 6c 6c 6c 6f 6f 2c 2c 20 20 57 57 6f 6f 72
72 6c 6c 64 64 21 21 00 00

If I recall correctly from my childhood days, COM's tiny model starts with DS=CS=SS and the code is loaded beginning from CS:0100h. I do not assume it is guaranteed that the code is loaded into a zeroed memory block (if it were guaranteed, I could drop two bytes).

Disassembly of the long code should be

  JMP *+14h
  ; 20 irrelevant bytes
  MOV DX,message
  MOV AH,09h
  INT 21h;  print string pointed to by DS:DX
  INT 20h;  exit program
message:
  DB "Hello, World!\0"
  DB "HHeelllloo,,  WWoorrlldd!!\0\0"

Disassembly of odd code

  JMP *+00h
  MOV DX,message
  MOV AH,09h
  INT 21h;  print string pointed to by DS:DX
  INT 20h;  exit program
  ; some irrelevant bytes
message:
  DB "Hello, World!\0"

Disassembly of even code:

  ADC AL,00h
  MOV DX,message
  MOV AH,09h
  INT 21h;  print string pointed to by DS:DX
  INT 20h;  exit program
  ; some irrelevant bytes
message:
  DB "Hello, World!\0"
\$\endgroup\$
1
\$\begingroup\$

dc, 52 bytes

0#[#H#e#l#l#o#,# #W#o#r#l#d#!#]#P#q
[Hello, World!]P

Try it online!

The first line puts a zero on the stack, and then the rest of the line is commented out. The second line prints 'Hello, World!'

With every even character removed, all the #s in the first line disappear, and the line is no longer commented out. It then prints 'Hello, World!' and quits before hitting the second line.

\$\endgroup\$
1
\$\begingroup\$

Brainfuck 156 bytes

-
-
<
-
<
<
+
[
+
[
<
+
>
-
-
-
>
-
>
-
>
-
<
<
<
]
>
]
<
<
-
-
.
<
+
+
+
+
+
+
.
<
<
-
.
.
<
<
.
<
+
.
>
>
.
>
>
.
<
<
<
.
+
+
+
.
>
>
.
>
>
-
.
<
<
<
+
.
\$\endgroup\$
1
\$\begingroup\$

Powershell, 46 bytes

Full version:

#
"Hello, World"
#" H e l l o ,   W o r l d " 

Try it online here

Every other character

#"el,Wrd
"Hello, World"

Try it online here

\$\endgroup\$
1
\$\begingroup\$

MAWP, 169 bytes

9/8/W/;/5/5/W/4/W/1/M/;/9/3/W/4/W/;/9/3/W/4/W/;/9/4/W/1/M/3/W/;/5/8/W/4/M/;/8/4/W/;/9/8/M/5/W/2/M/;/9/4/W/1/M/3/W/;/9/9/M/1/M/6/W/;/9/3/W/4/W/;/5/5/W/4/W/;/9/2/M/3/W/;/.

Try it!

Code with every other character removed:

98W;55W4W1M;93W4W;93W4W;94W1M3W;58W4M;84W;98M5W2M;94W1M3W;99M1M6W;93W4W;55W4W;92M3W;.

Try It!

\$\endgroup\$
0
1
\$\begingroup\$

Java (JDK), 123 bytes

/*/ */System.out.print("Hello, world!"); ///\ u 0 0 0 d S y s t e m . o u t . p r i n t ( " H e l l o_,_ _w o r l d ! " ) ;

Try it online!

This isn't a full program - you'll have to paste it into a method somewhere.


Every other character, 62 bytes

//*Sse.u.rn(Hlo ol!) /\u000dSystem.out.print("Hello, world!");

Try it online!

It takes advantage of the way the compiler turns unicode escapes into their corresponding characters.

\$\endgroup\$
1
\$\begingroup\$

Julia, 59 bytes

".H.e.l.l.o. .W.o.r.l.d.!\" | > p r i n t #"[2:2:24]|>print

Shortened version :

"Hello World!"|>print#[::4|pit

Try it online!

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 25 bytes

”Ÿ™,‚ï!”,””q”
Ÿ
™
,
‚
ï
!

Try it online!

”Ÿ™,‚ï!”,””q”Ÿ™,‚ï!  # full program (newlines aren't important here)
        ,            # output...
”Ÿ™,‚ï!”             # "Hello, World!"
         ””          # push empty string
           q         # exit program
            ”Ÿ™,‚ï!  # push "\nŸ\n™\n,\n‚\nï\n!" (never executed)

Every other character, 13 bytes

”™‚!,””Ÿ™,‚ï!

Try it online!

”™‚!,””Ÿ™,‚ï!  # full program
”™‚!,”         # push "Situation!,"
      ”Ÿ™,‚ï!  # push "Hello, World!"
               # implicit output
\$\endgroup\$
1
\$\begingroup\$

AWK, 87 bytes

BEGIN {print"Hello, World!" }
B#E#G#I#N# #{#p#r#i#n#t#"#H#e#l#l#o#,# #W#o#r#l#d#!#"# #}

Try it online!

The resulting code is:

BGN{rn"el,Wrd"}BEGIN {print"Hello, World!" }

Because the BGN variable is false (not assigned), the first part of the code has no effect.

dc, 49 bytes

[Hello, World!]p #[[HHeelllloo,,  WWoorrlldd!!]]p

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Ly, 39 [40] bytes

"x!xdxlxrxoxWx x,xoxlxlxexHx"'_p_p[xop]

Try it online!

Thanks to Aaroneous Miller for the -1 improvement hint in the comments.

The alternate code, with all the odd number characters deleted is,

"!dlroW ,olleH"__[o]

Which is straightforward. The only trick used is relying on Ly's willingness to ignore any character is doesn't recognize. So the _ characters (and the x characters in the longer code) can be replaced by any character that's undefined in the language.

The longer code works because the loop running through the stack and printing things, meaning

 [xop]

deleted one character for every one it prints.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The challenge says that the modified program should keep the odd-numbered characters, but you removed those ones. This means that you can just remove the first x to make it valid, which also gives you 39 bytes. \$\endgroup\$ Nov 16, 2021 at 18:33
  • \$\begingroup\$ ha! thanks for clarifying... \$\endgroup\$
    – cnamejj
    Nov 17, 2021 at 7:34
1
\$\begingroup\$

Backhand, 33 bytes

>'l"o'"'l','!'e' 'd'H'WHl'"'o.r'H

Try it online!

Only works in the latest interpreter, so the entire interpreter is copied into the TIO link (TIO's Backhand seems to not have ' implemented).

This one has only one copy of "Hello, World!" in the entire code.

>'l"o'"'l','!'e' 'd'H'WHl'"'o.r'H   Full program
>  "  "                             Push empty string (no-op)
         '  !  '  d  '  l  '  r     Push "!dlr" (`'` pushes next char)
    o  '  ,  '     '  W  '  o  '    Bounce off and push "oW ,o"
 'l  '  l  '  e  '  H               Bounce off and push "lleH"
                       H            Halt and print entire stack

>lo"l,!e dHWl"orH   Halved program
>  "                Start string mode
      !  d  l  r    Push "!dlr"
  o  ,     W  o     Bounce off and push "oW ,o"
 l  l  e  H  "      Bounce off and push "lleH" and end string mode
                H   Halt and print entire stack

Backhand, 34 bytes

>>lvo""!ld,l!reo Wd H,Woll"loerHH"

Try it online!

Halved:

>lo"l,!e dHWl"orH

Try it online!

This one works in the TIO version, and has two disjoint program paths. The full program has its step size reduced at an odd position, which makes the rest of its path totally invisible to the halved program. (Bonus points for looking totally jumbled, I guess.)

>>lvo""!ld,l!reo Wd H,Woll"loerHH"   Full program
>  v                                 Reduce step size to 2
     " ! d l r o W   , o l l e H "   Push the string
                               H     Halt and print

>lo"l,!e dHWl"orH   Halved program
>  "  !  d  l  r    →
  o  ,     W  o     ←
 l  l  e  H  "      →  Push the string
                H   Halt and print
\$\endgroup\$
1
\$\begingroup\$

Zsh, 35 bytes

e\c\h\o  \H\e\l\l\o\,\ \W\o\r\l\d\!

Attempt This Online!

AtmtTi nie

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

Stacked, 60 bytes

'Hello, World!'  '''H e l l o ,   W o r l d !''' @oouutt out

Try it online!

I generated the second code with this snippet. Second code is:

'el,Wrd' 'Hello, World!' out u

The first one generates two strings, Hello, World! and 'H e l l o , W o r l d !'. The latter is saved to variable oouutt, and the former is printed.

The second one generates two strings, outputs the second, then errors after encountering u.

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

BotEngine, 91 bytes

 vv 
 v < 
 eHH 
 eee 
 ell 
 ell 
 eoo 
 e,, 
 e   
 eWW 
 eoo 
 err 
 ell 
 edd 
 e!! 
 P
\$\endgroup\$
0
\$\begingroup\$

Keg, 47 39 35 bytes

`1H1%1;1,1 1c1¡1;1!1`_`H%;, c¡;!` $

Try it online!

And, unless I've somehow misread the challenge, the program with alternating characters:

-hd, 18 bytes

`H%;, c¡;!``%,c;`$

Try it online!

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

Pari/GP, 47 bytes

p r i n t ( H e l l o "\,\ """W o r l d "\!""")

Try it online!

print(Hello", "World"!")

Try it online!

A boring answer. In PARI/GP, all spaces not contained in a string are simply ignored. Hello and World are variables that can be used in polynomials.

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

Python 3, 60 bytes

#
exec(#
'p_r_i_n_t_(_"_H_e_l_l_o_,_ _W_o_r_l_d_!_"_)'[::2])

Try it online!

The [::2] slice the string to have 'print("Hello, World!")'. Then we execute it with exec

Every other char :

#ee(
print("Hello, World!")[:]

This prints Hello, World! and then error due to the slice on a None object

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

x86 machine code (DOS), 53 bytes

Full:

00000000: ba 00 0d b0 01 b0 b4 b0 09 b0 cd b0 21 b0 c3 be  ............!...
00000010: 1a 01 ad cd 29 3c 21 75 f9 c3 48 00 65 00 6c 00  ....)<!u..H.e.l.
00000020: 6c 00 6f 00 2c 00 20 00 57 00 6f 00 72 00 6c 00  l.o.,. .W.o.r.l.
00000030: 64 00 21 00 24                                   d.!.$

Even bytes only, 27 bytes

00000000: ba 0d 01 b4 09 cd 21 c3 1a ad 29 21 f9 48 65 6c  ......!...)!.Hel
00000010: 6c 6f 2c 20 57 6f 72 6c 64 21 24                 lo, World!$

Assembly (NASM syntax)

    [org 0x100]
start:
    ; Encode the half version using the defacto hello world with some
    ; harmless mov instructions. I haven't found a good use for these bytes
    ; yet (point of interest: get 13 in CX or (hello - start) in AX.
    ; mov dx, hello
    ; The offset is halved, then shifted left 8 to get it to encode in the
    ; upper byte.
    ;
    ; As a 3 byte opcode, this jumps the full variant to an odd address,
    ; causing a desync.
    mov dx, ((hello - start) / 2) << 8
    mov al, 0x01
    ; mov ah, 0x09
    mov al, 0xB4
    mov al, 0x09
    ; int 0x21
    mov al, 0xCD
    mov al, 0x21
    ; ret
    mov al, 0xC3

    ; Now for the thicc code. Instead of INT 21:09H, which expects a byte
    ; string terminated with '$', I print each char manually using INT 29H
    ; since I can do that with LODSW.

    ; Get the address of str into SI
    mov si, hello
.loop:
    ; Load one padded UTF-16 character into AX, and advance SI.
    lodsw
    ; Print AL using INT 29H.
    int 0x29
    ; Check for the last character
    cmp al, '!'
    jne .loop
    ; Return to exit
    ret
    ; On the minified version, this is a $-terminated string for INT 21:09H.
    ; db "Hello, World!$"
    ; On the non-minified version it is just dummy thicc.
hello:
    dw __UTF16__("Hello, World!") ; Unicode strings are proof that nasm > gas
    ; this one doesn't need padding
    db '$'

The full variant does this:

    [org 0x100]
start:
    ; bunch of pointless mov instructions

    mov si, str
.loop:
    lodsw
    int 0x29
    cmp al, '!'
    jne .loop
    ret
str:
    dw __UTF16__("Hello, World!")

while the small variant does this

    [org 0x100]
start:
    mov dx, str
    mov ah, 0x09
    int 0x21
    ret
    ; Unexecuted nonsense here
str:
    db "Hello, World!$"

This does not attempt to do even bytes only, unlike Hagen von Eitzen's solution. This allows me to simplify it greatly.

As the comments say, I use two methods for printing the string. For the larger code, I loop over the padded string with LODSB; INT 29H, and for the shorter code I use INT 21:09H to just print the string directly from memory. This means I don't need to use any complex tricks.

I feel like there is some improvement possible, as I think I could take better advantage of the nops and padding.

no officer I definitely wasn't up at 3:30 am

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

JavaScript (V8), 66 bytes

"";alert`Hello, World`; " ; a l e r t ` H e l l o ,   W o r l d `"

Try it online!

String as comment, nothing wrong

\$\endgroup\$
1 2
3

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.