78
\$\begingroup\$

In a language of your choice, write a program that exactly outputs the characters Hello world! followed by a newline. The code:

  • should not use any character more than twice (alphanumeric, symbol, whitespace...anything)
  • should not use any external resources
  • should not use any user input
  • should not output anything else

An example of a valid python program:

print("He%so world!"%(2*'l'))

An example of an invalid python program (the character 'r' is used three times):

print("Hel"+chr(108)+'o world!')

Winner is whoever has the most votes after 14 days.

EDIT: A winner has been chosen! Thanks all for your work on this question!

\$\endgroup\$
31
  • 25
    \$\begingroup\$ What about the repeated letter o in the "valid" python example. There also these:- " ' ( ) \$\endgroup\$ Jan 17, 2014 at 16:35
  • 15
    \$\begingroup\$ @AdamSpeight what do you mean? The letter is only used twice, as per the specs. \$\endgroup\$
    – Josh
    Jan 17, 2014 at 16:37
  • 5
    \$\begingroup\$ @AdamSpeight ... which is literally the same. \$\endgroup\$
    – VisioN
    Jan 17, 2014 at 16:42
  • 4
    \$\begingroup\$ I think you mean a character should not appear more than twice, that is, it should not be repeated more than once. \$\endgroup\$ Jan 17, 2014 at 18:15
  • 58
    \$\begingroup\$ I guess brainfuck is out of the question... \$\endgroup\$
    – jub0bs
    Jan 17, 2014 at 18:26

70 Answers 70

1 2
3
1
\$\begingroup\$

PHP I am amazed at how many people give invalid answers without mentioning they are. My attempt, only reusing o" characters twice.

<?Php EchO "Hello wor\154d!";

Edit: fixed thanks to @FireFly: i understood to use each character once. This is no challenge anymore.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Wouldn't changing the \X6C into l make it completely valid? I only see one other l in there. \$\endgroup\$
    – FireFly
    Jan 23, 2014 at 10:50
1
\$\begingroup\$

JavaScript (52 characters)

alert(['He\x6C\x6Co world!'][(!1,0)-0], +-1+"Hadtw")

Each character is used 2 times, and it doesn't use any comments!

Edit: I've just posted the same answer here.

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

Active Server Pages (Classic ASP):

Hel<%REspOnsE.wRite Chr((0<0>-.6&"!")+&H6C+h-dinptO)
%>o world!


Each character is used twice!

To run it, you'll need a web server running IIS.

Save it in the site root with a ".asp" extension, make a HTTP request to the server, and you'll see that the response text is exactly "Hello world!" and a new line:

Hello world!


Edit: You could also use this smaller code:

Hel<%= Chr((0<0>we=6&"!")+&H6C+hd)
%>o world!

Or this:

Hel<%= CHr((0<1>wedC=8&"!&")++108)
%>o world!

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

c-41

main(){printf("He%clo world!%c",154,10);}
\$\endgroup\$
10
  • \$\begingroup\$ Doesn't work in general: codepad.org/O09aQhCe - on what platform does character code 186 give you 'll' ? \$\endgroup\$
    – Paul R
    Jan 18, 2014 at 10:33
  • \$\begingroup\$ In MS-DOS (code page 437 and similar) character with code 186 is double vertical line, used for drawing frames. \$\endgroup\$
    – AMK
    Jan 18, 2014 at 12:22
  • 1
    \$\begingroup\$ It's 38 chars. You missed '!'. Besides, the two vertical bars are not two L's \$\endgroup\$ Jan 19, 2014 at 2:06
  • \$\begingroup\$ It produces this output: He||o world! I'm using code blocks \$\endgroup\$
    – Mohammad
    Jan 19, 2014 at 9:43
  • \$\begingroup\$ Without ending '\n' this answer contains 35 chars. And output looks wery similar to Hello world!. But this answer may be wroted shorter as main(){puts("He║o world!");} \$\endgroup\$
    – AMK
    Jan 19, 2014 at 18:52
0
\$\begingroup\$

JavaScript 35

With inspiration from @VisioN

alert(atob("SGVsbG8gV29y")+'ld!\n')
\$\endgroup\$
0
\$\begingroup\$

Tcl, 21 bytes

puts Hello\ wor\x6cd!

Try it online!

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

SmileBASIC

?@H[1]+@e[!0];CHR$(108);"lo world!

Another solution:

CLS?"He__o world!
LOCATE 2,0?@l[1]*2
\$\endgroup\$
-1
\$\begingroup\$

In C. Choose one of these.

My favourite (crazy but compact in memory):

#include <stdint.h>
printf("%s", (char*)(int32_t[]){1819043144,1870078063,6581362});

For those who like big numbers:

printf("%s", (char*)(int64_t[]){8031924123371070792,43509902450});

Or here the more descriptive versions:

printf("%s", (char*)(int32_t[]){0x6c6c6548,0x6f77206f,0x646c72});
printf("%s", (char*)(int64_t[]){0x6f77206f6c6c6548,0x0a21646c72});

char map:

H  e  l  l     o     w  o     r  l  d  !     <LF>
48 65 6c 6c    6f 20 77 6f    72 6c 64 21    0a
(remember to swap big/little endian bytes when composing)

Btw: why there is no 128 bit integer in C?

printf("%s", (char*)(int128_t[]){0x0a21646c726f77206f6c6c6548});
printf("%s", (char*)(int128_t[]){802616035175250124568770929992}); // string uses only 100 of 128 bits
\$\endgroup\$
5
  • 2
    \$\begingroup\$ These don't met the requirements as some characters are used more than twice. Eg ( ) 0 1 2 9 etc etc \$\endgroup\$ Jan 17, 2014 at 22:50
  • 1
    \$\begingroup\$ When I look at the example in the question, I see print("He%so world!"%(2*'l')) which is 1p 2r 1i 1n 1t 2( 2" 1H 1e 2% etc. Even when I focus to the string I still see 2 o's, so why should my answer follow the rules when the question doesn't? ;-) \$\endgroup\$ Jan 17, 2014 at 22:57
  • \$\begingroup\$ printf("%s", (char*)(int32_t[]){1819043144,1870078063,6581362}); ( x 3, ) x 3, 1 x 5, 3 x 3, 4 x 4, 6 x 3, 8 x 4 \$\endgroup\$ Jan 17, 2014 at 23:08
  • \$\begingroup\$ Ok, I see. I misunderstood the specs. But I'm not the only one, when I look at the bash + ruby solutions of SztupY and the solution of cnst \$\endgroup\$ Jan 18, 2014 at 0:11
  • 1
    \$\begingroup\$ Just because someone else didn't follow the rules doesn't justify you. \$\endgroup\$
    – Dozer789
    Jan 18, 2014 at 19:11
-1
\$\begingroup\$

AWK and echo

EDIT: fix the extra o's following the note. (thanks ProgramFOX)

echo He: | awk -F: '{print $1,Q,"\157 Wor",Q}' OFS=l ORS=d\!

and thanks awk which give me the option to say "-F" instead of "IFS=" (:-)

\$\endgroup\$
1
  • 3
    \$\begingroup\$ There are too many spaces, too. after echo, after He:, after |, and so on. \$\endgroup\$
    – Vereos
    Jan 21, 2014 at 8:33
-2
\$\begingroup\$

Java

Okay I found this on SO, it doesn't output a big H

Rare sequence of random stream of digits for 2 specific seeds.
Probably fixed out by brute forcing
One of the SO solutions also include a brute forcer to make your own sequences.

System.out.println(randomString(-229985452) + " " + randomString(-147909649) + "!");

And randomString() looks like this

public static String randomString(int i)
{
    Random ran = new Random(i);
    StringBuilder sb = new StringBuilder();
    for (int n = 0; ; n++)
    {
        int k = ran.nextInt(27);
        if (k == 0)
            break;

        sb.append((char)('`' + k));
    }

    return sb.toString();
}

Source: https://stackoverflow.com/questions/15182496/why-does-this-code-print-hello-world

\$\endgroup\$
1
  • 5
    \$\begingroup\$ And this meets the specs, how? I can easily count 3 S's, 3 spaces, 3 2's, etc. \$\endgroup\$
    – Justin
    Jan 18, 2014 at 8:05
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.