The task is simple. Write an interpreter for the language *.

Here's a bigger link to the wiki.

There are only three valid * programs:

  • * Prints "Hello World"
  •  *  Prints a random number between 0 and 2,147,483,647
  • *+* Runs forever.

The third case must be an infinite loop according to the specifications in this question


  • The input can be taken via any acceptable input method by our standard I/O rules
  • It will always be one of the above programs


  • The first case should print exactly Hello World, with or without a trailing line break.
  • For the second case, if your language's integer maximum is smaller than 2,147,483,647, use your language's integer maximum
  • The first and second cases can print to any acceptable output by our standard I/O rules.
  • The third case should not give any output.


As this is , the shortest answer, in bytes, wins.

  • 7
    \$\begingroup\$ When you say 'between 0 and 2,147,483,647', is that inclusive or exclusive? (E.g., is 0 a valid output?) \$\endgroup\$ – Chas Brown Aug 9 at 21:46
  • 1
    \$\begingroup\$ @Chas the linked wiki has interpreters in Java & JS which include 0. \$\endgroup\$ – Jonathan Allan Aug 9 at 22:48
  • 7
    \$\begingroup\$ Changing the spec after posting a challenge and invalidating existing solutions is an automatic -1 from me. \$\endgroup\$ – Shaggy Aug 10 at 7:00
  • 2
    \$\begingroup\$ If our language's integers have no, or a higher, maximum may we use a higher upper bound? \$\endgroup\$ – Jonathan Allan Aug 10 at 23:43
  • 7
    \$\begingroup\$ @Shaggy I'm not seeing any rule changes in the wiki for the question, only a space to a non-breaking space (check the markdown tab), because the SE markdown renderer wasn't rendering it, but looking at the original revisision, it's clear it should be normal spaces, and the "hack' is only done for SE markdown renderer issues \$\endgroup\$ – Ferrybig Aug 11 at 20:49

27 Answers 27


*, 0 bytes

Since * has no way of reading input, the default rules allow specifying that the input must be given by concatenating it onto the program.

(... I think. There's an "at least twice as many upvotes as downvotes" condition that I don't have the rep to verify).

  • 4
    \$\begingroup\$ Your linked meta is indeed a currently accepted site standard (+31 -7). \$\endgroup\$ – Jonathan Allan Aug 10 at 19:32
  • 2
    \$\begingroup\$ @A__: It looks to me like it must have been designed specifically to satisfy someone's proposed definition of 'programming language' ("You can write hello world!" "You can write an infinite loop!" "You can write a program that doesn't always do the same thing!"). \$\endgroup\$ – Henning Makholm Aug 11 at 3:44
  • \$\begingroup\$ I believe that technically Malbolge is not a programming language either then. \$\endgroup\$ – Bob Jansen Aug 11 at 10:16
  • 1
    \$\begingroup\$ Malbolge is programming language for finite automata, the same as *, and, for example, Befunge-93. Therefore Malbolge is formal programming language, the same as *, technically the same as recursively enumerable languages when it comes to programming language definition (albeit formal languages are less powerful). \$\endgroup\$ – Krzysztof Szewczyk Aug 11 at 17:05
  • \$\begingroup\$ Downvote from me, because honestly, this answer is so boring it's actually already a Standard Loophole, even if we ignore the question whether * is a programming language \$\endgroup\$ – AlienAtSystem Oct 14 at 11:56

R, 69 bytes

switch(scan(,""),"*"="Hello, World!"," * "=sample(2^31,1)-1,repeat{})

Try it online!

switch tries to match the named arguments and if there's no match, selects the first unnamed one after the first, which in this case is the infinite loop repeat{}.


Jelly,  21  20 bytes


A monadic Link accepting a list of characters.

Try it online!

vL’... also works (see below).


ḊOSØ%HX’¤“½,⁾ẇṭ»¹Ḃ¿? - Link: list of characters   e.g.: "*"        or  " * "    or  "*+*"
Ḋ                    - dequeue                          ""             "* "         "+*"
 O                   - to ordinals                      []             [42,32]      [43,42]
  S                  - sum                              0              74           85
                   ? - if...
                  ¿  - ...if-condition: while...
                 Ḃ   -    ...while-condition: modulo 2  0              0            1
                ¹    -    ...while-true-do: identity                                85
                     -                                  0              74           (looping)
        ¤            - ...then: nilad followed by link(s) as a nilad:
   Ø%                -    literal 2^32                                 2^32
     H               -    half                                         2^31
      X              -    random integer in [1,n]                      RND[1,2^31]
       ’             -    decrement                                    RND[0,2^31)
         “½,⁾ẇṭ»     - ...else: dictionary words        "Hello World"


vL’... - Link: list of characters                 e.g.: "*"        or  " * "    or  "*+*"
 L     - length                                         1              3            3
v      - evaluate (left) as Jelly code with input (right)
       -                                                1^1            3^3          (3^3+3)^3
       -                                                1              27           27000
  ’    - decrement                                      0              26           26999
   ... - continue as above                              "Hello World"  RND[0,2^31)  (looping)

C (gcc), 66 63 bytes

Thanks to attinat for the -3 bytes.

I only have to check the second character: if the LSB is set, it's a + (thus the program is "*+*") and the program loops. After that, if it's a NUL, the program was "*" and we display Hello World; otherwise, it displays a random value (" * ", the only other option left.)

f(char*s){for(s++;*s&1;);printf(*s?"%d":"Hello World",rand());}

Try it online!

  • \$\begingroup\$ 63 bytes \$\endgroup\$ – attinat Aug 9 at 22:21
  • \$\begingroup\$ Shave a byte: f(char*s){*++s&1?f(s-1):printf(*s?"%d":"Hello World",rand());} \$\endgroup\$ – Roman Odaisky Aug 10 at 22:24
  • \$\begingroup\$ Pedantic note: rand is not guaranteed to return a sufficiently large value; RAND_MAX and INT_MAX are not guaranteed to be the same (and are not on real world compilers, e.g. Visual Studio's RAND_MAX is 32767, while INT_MAX is [on modern x86 derived systems] the 2147483647 value specified in the OP's question). \$\endgroup\$ – ShadowRanger Aug 12 at 14:45
  • \$\begingroup\$ @ShadowRanger that is completely true, but considering that >90% of all C-based CGCC entries rely on undefined and unspecified behaviour I'm not worried about that! I also didn't feel like implementing a code-golfed LCG today. :-) \$\endgroup\$ – ErikF Aug 12 at 16:53

Python 2, 103 93 89 87 bytes

I combined my earlier answer with Chas Browns's answer and got something a few bytes shorter.

The random number will be between 0 and 2**31-1 inclusive.

from random import*
print["Hello World",randrange(2**31)][i<'!']

Try it online!

Previous versions:

103 bytes

from random import*
exec['print"Hello World"','while 1:1','print randint(0,2**31-1)'][cmp(input(),'*')]

93 bytes

from random import*
while i>0:1
print["Hello World",randint(0,2**31-1)][i]
  • \$\begingroup\$ Save 2 bytes by replacing randint(0,2**31-1) with randrange(2**31). \$\endgroup\$ – Chas Brown Aug 9 at 21:53
  • \$\begingroup\$ while'*'<i saves 2 \$\endgroup\$ – Jonathan Allan Aug 10 at 0:12
  • \$\begingroup\$ Save another byte by changing randrange(2**31) to getrandbits(31) (the latter returns long, not int, but print will print the str form, not the repr form, so the trailing L won't be there). \$\endgroup\$ – ShadowRanger Aug 12 at 14:18
  • \$\begingroup\$ Relatively inexperienced with site, so quick clarification: Are you allowed to require your input to be quoted? i=input() only works if the inputs are quoted, if you just input plain */ * /*+*, it would die with a SyntaxError (because input includes an implicit eval); you'd need to input '*'/' * '/'*+*' (or equivalent with double-quotes instead). I didn't see anything obvious on the standard I/O rules that would allow this, which might mean you'd need to use raw_input(), costing four bytes. \$\endgroup\$ – ShadowRanger Aug 12 at 14:27
  • \$\begingroup\$ @ShadowRanger input() takes a string as input and evaluates it. I'm not really adding to the input, I'm merely taking a string as input, and strings have quotes. This is pretty standard, in the same way that I can take an array like [1,2,3] instead of as a delimited string that I then have to split and parse. The goal of the site is not to make input strict, it's to make I/O easy so we can focus code on the challenge at hand. \$\endgroup\$ – mbomb007 Aug 12 at 15:49

Keg, 30 26 25 24 20 bytes

?!1=[_“H%c¡“| =[~.|{

Try it online!

Shortened Hello World to dictionary string

Answer History

!1=[_Hello World| =[~.|{

Try it online!

I never ceased to be amazed at the power of Keg. Credits to user EdgyNerd for another byte saved.

Prior Versions

_!0=[Hello World|\*=[~.|{

Try it online!

Credit to user A__ for the extra byte saved.

Old version

?!1=[_Hello World| =[__~|{

Essentially, takes the input program and:

  • Checks to see if the input length is 1, printing "Hello World" if true
  • Checks to see if the last character is a space, and prints a random number
  • Otherwise runs an infinite loop

Then implicitly print the stack.

?                               #Get input from the user
 !1=                            #Compare the stack's length to 1
    [_Hello World           #Push "Hello, World!" to the stack
                     | =        #See if top item is a space
                        [__~|{  #If so, generate a random number, otherwise, infinite loop.

4 bytes saved due to the fact that hello world doesnt need punctuation.

Try it online! Old version

Try it online! New version

  • \$\begingroup\$ You can cut off 4 bytes, you don't need the comma or exclamation mark in "Hello World". \$\endgroup\$ – TheOnlyMrCat Aug 10 at 2:14
  • 1
    \$\begingroup\$ Now I have to learn another unpopular language in order to answer challenges here normally. \$\endgroup\$ – A _ Aug 10 at 6:26
  • 1
    \$\begingroup\$ -1 bytes: TIO. I am glad that I did not lose my ability to golf in Keg. \$\endgroup\$ – A _ Aug 10 at 6:36
  • \$\begingroup\$ @A__ Enjoying Keg being on TIO? \$\endgroup\$ – Jono 2906 Aug 10 at 6:48
  • 2
    \$\begingroup\$ 24 bytes :) \$\endgroup\$ – EdgyNerd Aug 10 at 8:49

Japt, 22/25 bytes

The first solution is for the original spec which had *<space> as the second programme and the other is for the updated spec which arbitrarily changed it to <space>*</space>, with thanks to EoI for the suggested "fix".

Both throw an overflow error upon entering the infinite loop of the third programme but, theoretically, with enough memory (which we may assume for the purposes of ), would run forever.

Å?¢?ß:2pHÉ ö:`HÁM Wld

Try programme 1
Try programme 2
Try programme 3

Å?UÎ>S?ß:2pHÉ ö:`HÁM Wld

Try programme 1
Try programme 2
Try programme 3

  • \$\begingroup\$ I think the second program is "[SPACE]*[SPACE]", not "[SPACE]*", so your program doesn't work \$\endgroup\$ – Embodiment of Ignorance Aug 10 at 6:51
  • \$\begingroup\$ @EmbodimentofIgnorance, at the time I posted, the second programme in the spec was *<space>. Don't have time to update now. \$\endgroup\$ – Shaggy Aug 10 at 6:58
  • \$\begingroup\$ You can fix it in three bytes with UÌ>S instead of ¢ on the second ternary \$\endgroup\$ – Embodiment of Ignorance Aug 10 at 7:08
  • \$\begingroup\$ @Downvoter, please have the courtesy to leave a comment. \$\endgroup\$ – Shaggy Aug 12 at 9:17

JavaScript (ES7), 66 bytes

s=>s[1]?s<'!'?Math.random()*2**31|0:eval(`for(;;);`):'Hello World'

Try it online! (Hello World)

Try it online! (random number)

Try it online! (infinite loop)

  • \$\begingroup\$ Would x=(z=>x())&&x() not work for -1byte from the infinite loop code, assuming a browser with no max call stack size? \$\endgroup\$ – Geza Kerecsenyi Aug 10 at 15:52
  • \$\begingroup\$ @GezaKerecsenyi We could just call ourself (like this) but I'm not sure that would be acceptable. \$\endgroup\$ – Arnauld Aug 10 at 15:55
  • \$\begingroup\$ that's fair. I wonder if there is some obscure browser out there that just keeps going (at least, until RAM runs out) \$\endgroup\$ – Geza Kerecsenyi Aug 10 at 15:57
  • 1
    \$\begingroup\$ @Arnauld, theoretically, that would run forever given infinite memory, which we may assume for code golf. \$\endgroup\$ – Shaggy Aug 10 at 20:47

Jelly, 23 21 bytes


Try it online!

A monadic link taking a single argument and returning Hello World, a random 31 bit integer or looping infinitely as per the spec.

All options: * * *+*


O                     | Convert to codepoints
 S                    | Sum
  ¹Ḃ¿                 | Loop the identity function while odd 
     ị              ¤ | Index into the following as a nilad:
      Ø%              | - 2 ** 32
        H             | - Halved
         X            | - Random integer in the range 1..2**31
          ’           | - Decrease by 1 
           ;          | - Concatenated to:
            ““½,⁾ẇṭ»  |   - "", "Hello World"

Befunge-93, 54 bytes

~"*"-_~1+#^_"dlroW olleH">:#,_@.%*2**:*::*88:*`0:?1#+<

Try it online!


~"*"-                      _                ~1+                   #^_        "dlroW olleH">:#,_    @      .%*2**:*::*88:   *`0:             ?1#+<
Compare first      If equal, go right       Compare second       If equal,        Output          Exit    Modulo by 2^31   If less than      Add 1
character to       Otherwise, go left       character to       loop forever   "Hello World"                 and output     0, multiply     a random amount
'*'                and wrap around          -1 (EOF)                                                                         by 0            of times

The randomness is not uniform. At each increment there is a 50% chance to stop incrementing.


Python 2, 91 89 88 bytes

from random import*
def f(p):
 print['Hello World',getrandbits(31)][p<'!']

Try it online!

2 bytes thanks to Jonathan Allan; 1 byte thx to ShadowRanger.

  • \$\begingroup\$ while'*'<p saves 2 \$\endgroup\$ – Jonathan Allan Aug 9 at 22:25
  • \$\begingroup\$ getrandbits(31) saves a byte over randrange(2**31). \$\endgroup\$ – ShadowRanger Aug 12 at 14:21
  • \$\begingroup\$ Nice! Didn't know about getrandbits... \$\endgroup\$ – Chas Brown Aug 12 at 18:48

PowerShell, 60, 56 bytes

switch($args){*{'Hello World'}' * '{random}*+*{for(){}}}

Pretty dumb version, the only golfing technique here is omitting Get- in Get-Random.

UPD. Stripped down to 56 bytes by removing quotes, thanks to veskah!


Perl 5 -p, 43 39 bytes

$_=/ /?0|rand~0:/\+/?redo:"Hello World"

Try it online!


C# (Visual C# Interactive Compiler), 71 bytes

s=>{for(;s=="*+*";);return"*"==s?"Hello World":new Random().Next()+"";}

Try it online!


Ruby -n, 47 bytes

puts~/ /?rand(1<<31):~/\+/?loop{}:"Hello World"

Try it online!


Wolfram Language (Mathematica), 65 bytes

#/.{"*"->"Hello World"," * "->RandomInteger[2^31-1],_:>0~Do~∞}&

Try it online!


Java (JDK), 83 bytes

s->{for(s=s.intern();s=="*+*";);return"*"==s?"Hello World":(-1>>>1)*Math.random();}

Try it online!


Brachylog, 26 23 bytes


Try it online!

Takes the program as a string through the input variable, and ignores the output variable. Heavily exploits the guarantee that the input is only ever one of the three valid programs: any length-three input will behave like either " * " or "*+*" depending on whether or not the first character is a space, and any other input will behave like "*".

l₃                         The input has length 3
  ∈                        and is an element of something,
   &h                      and the input's first element
     Ṣ                     is a space
  ∈                        (if not, try some other thing it's an element of),
      ∧2^₃₁-₁              so take 2,147,483,647 and
             ṙw            print a random number between 0 and it inclusive.
               ∨           If the input's length isn't 3,
                Ḥ⊇ᶠ³⁶tw    print the 36th subsequence of "Hello, World!".
  • \$\begingroup\$ Oops, wrong "Hello World"--fixing now \$\endgroup\$ – Unrelated String Aug 9 at 21:41

PHP, 51 bytes

for(;'*'<$l=$argn[1];);echo$l?rand():'Hello World';

Try it online! (Hello World)

Try it online! (Random Number)

Try it online! (Infinite Loop)

Takes second character of input which can be '', '*' or '+'. In case of '+' the '*'<'+' will be true and the loop will be infinite, else, after the loop, "Hello World" or a random number is shown. The rand() automatically outputs a number between 0 and getrandmax() which uses defined RAND_MAX in standard C library and by default is 2147483647 on most platforms/environments, including TIO.


05AB1E, 21 bytes


Try it online. (NOTE: The random buildin is pretty slow with big lists, so it might take awhile before the result is given.)


'*K           '# Remove all "*" from the (implicit) input
   g           # Get the length of what's remain (either 0, 1, or 2)
    D          # Duplicate this length
     i         # If the length is exactly 1:
      [        #  Start an infinite loop
     ë<i       # Else-if the length is 2:
        žI     #  Push builtin 2147483648
          L    #  Create a list in the range [1,2147483648]
           <   #  Decrease each by 1 to make the range [0,2147483647]
            Ω  #  Pop and push a random value from the list
               #  (after which the top of the stack is output implicitly as result)
     ë         # Else:
      ”Ÿ™‚ï    #  Push dictionary string "Hello World"
               #  (after which the top of the stack is output implicitly as result)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ”Ÿ™‚ï is "Hello World".


Pyth, 32 bytes

It/Jw\*#;?tlJOhC*4\ÿ"Hello World

Try it online!

Explanation (Python-ish)

I                                   # if 
  /Jw\*                             #    (J:=input()).count("*"))
 t                                  #                             - 1:
       #;                           #     try: while True: pass;except: break;
         ?                          # if (ternary)
           lJ                       #    len(J):
             O                      #     randInt(0,                    )
               C                    #                int(     , 256)
                *4\ÿ                #                    4*"ÿ"
              h                     #                                + 1
                    "Hello World    # else: (implicitly) print "Hello World"
  • \$\begingroup\$ This prints a number between 0 and 2^32, not 0 and 2^31. A shorter way to write hC*4\ÿ is ^2 32, but in order for the solution to be correct, you should use ^2 31 instead. Also, use z instead of Jw, saves 1 more byte. And your explanation skips the line with t right before lJ. \$\endgroup\$ – randomdude999 Oct 27 at 18:59
  • \$\begingroup\$ Also, you can detect the "loop forever" command by checking whether the input contains any + character, saves 1 byte because you don't need to decrement it. \$\endgroup\$ – randomdude999 Oct 27 at 19:02

Charcoal, 30 bytes

W№θ*F⁼θ*≔Hello Worldθ∨θI‽X²¦³¹

Try it online! Link is to verbose version of code. Abuses Charcoal's default input format which splits on spaces if there is only one line, thus the random number input actually looks like three inputs. Explanation:


Repeat while the first input contains a *.


If the first input is a * only...

≔Hello Worldθ

... then replace it with Hello World, thus causing the loop to terminate. *+* doesn't get replaced, resulting in an infinite loop.


If the first input is not empty then output it.


But if it is empty then output a random integer in the desired range.


Add++, 78 bytes

z:"Hello World"
b:" * "

Try it online!


z:"Hello World"	; Set z = "Hello World"
`y		; Set y as the active variable
xR2147483647	; Set y to a random number between 0 and 2147483647
x:?		; Set x to the input
a:"*"		; Set a = "*"
b:" * "		; Set b = " * "
c:"*+*"		; Set c = "*+*"
Ix=a,		; If x == a then...
	Oz	;	...output z
Ix=b,		; If x == b then...
	O	;	...output y
Wx=c,		; While x == c then...
		;	...do nothing

APL (Dyalog Unicode), 39 bytesSBCS

Anonymous prefix lambda.

{'+'∊⍵:∇⍵⋄' '∊⍵:⌊2E31×?0⋄'Hello World'}

Try it online!

{ "dfn"; is the argument:

'+'∊⍵: if plus is a member of the argument:

  ∇⍵ tail recurse on argument

' '∊⍵ if space is a member of the argument:

  ?0 random float (0–1)

  2E31× scale to (0–2³¹)


'Hello World' else return the string


Commodore BASIC (VIC-20, C64, TheC64Mini etc) - 170 tokenize BASIC bytes

 0a%=32767:goS9:b$=leF(b$,len(b$)-1):ifb$="*"tH?"hello world
 1ifb$=" * "tH?int(rN(ti)*a%)

I think to do this more accurately, I'll have to delve into the weird world of 6502 assembly language, but this is a first draft.

First point, the INPUT keyword in Commodore BASIC ignores white spaces, so the sub-routine at line 9 is a quick-and-dirty way to accept keyboard entries including spaces.

Second point, Commodore BASIC integers have a range of 16-bit signed, so -32768 to +32767 source - so I've kept the random number generated to 0 - 32767 inclusive


Wren, 143 135 bytes

I am not a good golfer... The RNG generates the same value each time because it is a pseudo-random number generator.

import"random"for Random
}else System.write(a[0]==" "?Random.new(9).int((1<<31)-1):"Hello World")

Try it online!


JavaScript, 63 Bytes, no infinite recursive

s=>s[1]?Math.random()*2**31|eval("while(s>'!');"):'Hello World'

bad network so no TIO link


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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