Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


Write the shortest program P such that

  • P takes any positive integer n as input, and outputs a program string P1
  • executing P1 (with no input) outputs a program string P2,

  • executing P2 (with no input) outputs a program string P3,

  • ...

  • executing Pn (with no input) outputs exactly "Hello!" (without the quotes).


                P(n) → P1 → P2 → ... → Pn → Hello!


  • I/O is via stdin/stdout.
  • Program length is measured in bytes.
  • Programs consist only of ASCII printable characters.
  • There must be no duplicates among the programs P, P1,...,Pn.

EDIT: Programs P, P1,...,Pn are meant to be source code, and "executing the program" refers to whatever processing produces the output (i.e., interpretation, compilation & execution, etc.).

share|improve this question
@dmckee - The relationship, if any, seems very remote. Schematically, a quine's behavior is like P-->P, and the linked programs' behavior is like P-->Q-->P; but here the behavior is like P(n)-->...-->Hello!, where P is not required to resemble any of the outputs. – r.e.s. Jul 7 '12 at 0:12
That's not a criticism, r.e.s., just a note that people who like this problem might like the other one as well. – dmckee Jul 7 '12 at 0:21
@dmckee - Good point, thanks. – r.e.s. Jul 7 '12 at 0:31

12 Answers 12

up vote 8 down vote accepted

Golfscript 39 14



Assuming that n=4 and our program is P, these are the steps:

  1. Run P with parameter 4. The output is "\"\\\"\\\\\\\"Hello!\\\\\\\"\\\"\"" (P1)

  2. Running P1 outputs "\"\\\"Hello!\\\"\"" (P2)

  3. Running P2 outputs "\"Hello!\"" (P3)

  4. Running P3 outputs "Hello!"(P4)

  5. Running P4 outputs Hello!. If you don't trust me, follow this link ☺.

share|improve this answer
I find your P1,P2,P3,P4 behaving as required, but with input from stdin your P doesn't output P1 -- instead, it outputs \'\\\'\\\\\\\'Hello!\\\\\\\'\\\'\' (i.e., missing the outer quotes), when I run it both under Linux and under Win 7 (using echo 4 | ... at a command prompt). Any idea what the problem might be? – r.e.s. Jul 4 '12 at 23:04
@r.e.s. You are right; that's a shortcoming of the Golfscript testing website I made. I managed to fix that by adding three chars, but in the meantime I found a much shorter version and edited my post. – w0lf Jul 5 '12 at 7:31
@r.e.s. The correct version of the old approach is ~''\(,{)2\?('\\'*}/]"'"*:z"'Hello!"z-1%"'" – w0lf Jul 5 '12 at 7:36
At 14 bytes, impressive! ... and nice website btw. (It seems that stdin can be simulated by preceding a program by ;"...", where ... is the input. E.g., ;"4" would have revealed the missing-quote problem above, whereas simply preceding the program by "4" does not.) ... – r.e.s. Jul 5 '12 at 12:54
@r.e.s. Thanks a lot! You are absolutely right about the leading ;. When I wrote the first program I forgot about this along the way, although it was me who wrote about it on the meta site: – w0lf Jul 5 '12 at 12:58

JavaScript, 58 chars

function $(_){alert(_?$+'$('+--_+')':'Hello!')}$(prompt())
share|improve this answer
How does it works? – thepirat000 Mar 8 '14 at 22:58
@thepirat000 If the argument _ is 0, it outputs "Hello!", otherwise it outputs the function definition and call with _ minus one – copy Mar 8 '14 at 23:30
+1 for abusing rules by using a counter rather than elongating the code... – WallyWest Mar 9 '14 at 11:59

Perl (45 36)

say"say q("x($m=<>),"Hello!",")"x$m         

Run with perl -M5.010 to use say.

For n=1, outputs say q(Hello!)
For n=2, outputs say q(say q(Hello!))
For n=3, outputs say q(say q(say q(Hello!)))
and so on.

share|improve this answer

Python3, 66

share|improve this answer

Common Lisp, 68 characters

(do((n(read)(1- n))(s'(princ"Hello!")`(print',s)))((= 0 n)(eval s)))
(do ((n (read) (1- n))
     (s '(princ "Hello!") `(print ',s)))
    ((= 0 n) (eval s)))

The only non-straightforward thing: it generates one more level of program than needed and evaluates it, because eval is one character shorter than print.

share|improve this answer

Common Lisp: 65

#1=(SETF N(READ)(CADDR'#1#)(1- N)W(PRINT(IF(= N 0)"Hello!"'#1#)))

This version is smaller than the existing CL-answer. In case your Lisp croaks with a stackoverflow, then you have to use this, 81 bytes:

#1=(SETF N(READ)*PRINT-CIRCLE* T(CADDR'#1#)(1- N)W(PRINT(IF(= N 0)"Hello!"'#1#)))

I know I'm late to the party, and I'm sure this could still be optimized.

Demonstration (careful, some online REPLs don't work correctly):

* #1=(SETF N(READ)*PRINT-CIRCLE* T(CADDR'#1#)(1- N)W(PRINT(IF(= N 0)"Hello!"'#1#)))
#1=(SETF N 1
         *PRINT-CIRCLE* T
         (CADDR '#1#) (1- N)
            (IF (= N 0)
#1=(SETF N 1
         *PRINT-CIRCLE* T
         (CADDR '#1#) (1- N)
            (IF (= N 0)
* (eval *)

#1=(SETF N 0
         *PRINT-CIRCLE* T
         (CADDR '#1#) (1- N)
            (IF (= N 0)
#1=(SETF N 0
         *PRINT-CIRCLE* T
         (CADDR '#1#) (1- N)
            (IF (= N 0)
* (eval *)

Note that:

  • Tested using SBCL 1.2.11.debian

  • There are lots of warnings because I'm doing things that are seriously not supposed to be done. Thankfully, all warnings are printed with a semi-colon, so they would be treated as comments anyway.

  • Duplication is because one is the return value and one actual output. If the return value would also count, then it simplifies to 58 bytes: #1=(SETF N(READ)(CADDR'#1#)(1- N)W(IF(= N 0)"Hello!"'#1#))

  • I still know I'm late to the party

share|improve this answer

Python 2.7, 57

print reduce(lambda x,y:y+`x`,input()*["print"],"Hello!")
share|improve this answer

Haskell, 62

share|improve this answer

Bash, 47 or 12 (if I use golfbash)


read x;for((;x>=0;x--)){ O+=" echo";};$O Hello!

golfbash code:

R;n e Hello!

Previous code (49):

read x;E=echo;eval \"\${E[0]\"{0..$x}\"}\" Hello!


Make program P

$ echo '<paste code here>' > P

$ chmod +x P


Make P1:

$> echo 4 | ./P > 
echo echo echo echo Hello!

Make P2:

$> echo 4 | ./P | bash
echo echo echo Hello!

Make P3:

$ echo 4 | ./P | bash | bash
echo echo Hello!

Make P4:

$ echo 4 | ./P | bash | bash | bash
echo Hello!

Run P4:

$ echo 4 |./P | bash | bash | bash | bash

Say that I make a new general purpose language called golfbash - at bit like golfscript - that has some useful commands like these:

R which reads stdin and places result into variable $REPLY.
n $1 $2 which echo's $1 $REPLY times followed by $2.
e which echo's all parameters to stdout.

Then I could write this:

R;n e Hello!

This would be legal, would't it?

share|improve this answer
That's rude. -1 for what? – philcolbourn Mar 8 '14 at 9:14
+1. I guess the downvote was for your invented language that appears to be specifically for this challenge. See… – Cees Timmerman Oct 30 '15 at 10:59
Fine, but I did it in bash as well. – philcolbourn Nov 1 '15 at 11:22

F#, 182

let say msg = sprintf "%s" msg
let gen num = 
    let rec g n a =
        match n with
        | i when i > 0 -> g (i-1) ("say \"" + a + "\"")
        | _ -> say a
    g num "Hello!"
share|improve this answer
When I run your program, it does not appear to input anything from stdin, nor to output anything to stdout. – r.e.s. Jul 6 '12 at 12:50

J - 31 char

Keep in mind that J uses Pascal-style strings: 'It''s too easy!'



  • ".1!:1]1 - Read in a line from stdin and convert it to a number.
  • <:2^ - Take 2 to the power of this number, and decrement.
  • ''''#~ - Make a string with that many copies of the single quote character.
  • 'Hello!'(],,) - Append these quotes to the front and back of Hello!.


   'Hello!'(],,)''''#~<:2^".1!:1]1   NB. let's try 3
   '''''''Hello!'''''''  NB. P1
   '''Hello!'''          NB. P2
   'Hello!'              NB. and P3 prints Hello!
share|improve this answer

Python 2.7: 75

def h(n):
 return "Hello"if n<1 else'print %s' % `h(n-1)`
print h(input())

First time golfing, not sure if I'm doing it right ;p

share|improve this answer
Doesn't that assume that the definition of h is also known in the subsequent programs? (So that this would have to be run in a REPL or something?) – Martin Ender May 29 '15 at 10:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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