Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Challenge

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).

Schematically:

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

Criteria

  • 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
1  
    
@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
1  
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
add comment

10 Answers 10

up vote 8 down vote accepted

Golfscript 39 14

~'Hello!'{`}@*

Example:

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: meta.codegolf.stackexchange.com/a/521/3527 –  w0lf Jul 5 '12 at 12:58
add comment

JavaScript, 58 chars

function $(_){alert(_?$+'$('+--_+')':'Hello!')}$(prompt())
share|improve this answer
    
How does it works? –  thepirat000 Mar 8 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 at 23:30
    
+1 for abusing rules by using a counter rather than elongating the code... –  WallyWest Mar 9 at 11:59
add comment

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
add comment

Python3, 66

r="print(%r)";e=eval;n=e(input())+1;e(e("(r%"*n+"'Hello!'"+")"*n))
share|improve this answer
add comment

Python 2.7, 57

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

Haskell, 62

main=interact$(iterate(("main=putStr"++).show)"Hello!"!!).read
share|improve this answer
add comment

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
add comment

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
add comment

J - 31 char

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

'Hello!'(],,)''''#~<:2^".1!:1]1

Explained:

  • ".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!.

Usage:

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

Bash, 47 or 12 (if I use golfbash)

Code:

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!

Use:

Make program P

$ echo '<paste code here>' > P

$ chmod +x P

Example

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
Hello!

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 at 9:14
add comment

Your Answer

 
discard

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.