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

Your goal is to write a program that prints another program. That printed program should print another program, and the new program should print another program, until the end.

Rules

  1. Each program must less than 256 bytes. (If this needs to be changed, leave a comment)
  2. The last program must be an empty program.
  3. There must be a finite number of programs, so the program cannot be a quine.
  4. The programs must all run in the same language.
  5. No input is allowed.
  6. The winning program is the program that prints as many programs as possible, counting itself.

Good Luck!

share|improve this question
    
The maximum score is 2^2048, or 3.2317e616. –  orlp yesterday
    
For making it easier to compare big scores, please include an approximation to your score in the form a*10^b where 1<=a<10 and b is a natural number. –  flawr yesterday
2  
Actually, my previous calculation was wrong. Assuming the program must be in bytes the maximum possible score is <number too long for comment> or 1.2673e614. –  orlp yesterday

10 Answers 10

CJam, 4.56 × 10526

2D#2b{"\256b_(256b:c'\s`_:(er`":T~{;38'ÿ*`{:T~{;63'ÿ*`{:T~{;88'ÿ*`{:T~{;114'ÿ*`{:T~{;140'ÿ*`{:T~{;166'ÿ*`{:T~{;192'ÿ*`{:T~{;219'ÿ*`{Q?\"_~"}s(\T}?\"_~"}s(\T`}?\"_~"}s(\T`}?\"_~"}s(\T`}?\"_~"}s(\T`}?\"_~"}s(\T`}?\"_~"}s(\T`}?\"_~"}s(\T`}?\"_~"}_~

Exact score: 254219 + 254192 + 254166 + 254140 + 254114 + 25488 + 25463 + 25438 + 25413 + 3

All programs have to be saved using the ISO-8859-1 encoding to comply with the file size limit.

Thanks to @ChrisDrost who pointed out a bug suggested the nesting approach.

Try it online in the CJam interpreter.

How it works (outdated)

Assume the string is already on the stack.

{                        }_~  Push a code block, a copy and execute the copy.
 \                              Swap the original code block with the string.
  256b                          Interpret the string as an integer in base 256.
      _                         Push a copy of the integer.
       {              }&      If it is non-zero:
        (256b                   Decrement the integer and convert back to base 256.
             :c                 Replace the integer digits with characters.
               `                Inspect the string (add surrounding quotes.
                \               Swap the string with the code block.
                 "_~"1          Push those tokens.
                        *     If the conditional block got executed, this repeats
                              the sting "_~" one time. Otherwise, the integer on
                              the stack is zero, and * executes the block zero
                              times, clearing the stack.
share|improve this answer
    

JavaScript, 1000 programs

x=999;
q=";alert(x=999?`q=${JSON.stringify(q)+q}`.split(x).join(x-1):``)";
alert(
    x ? `x=999;q=${JSON.stringify(q)+q}`.split(x).join(x-1) // basically .replaceAll(x, x-1)
      : ``
)

Whether this is valid depends on precisely how to understand the third rule.

share|improve this answer
    
It isn't technically a quine, since it prints out a modified version of its own source code rather than an identical copy. It does use quine-like techniques, obviously. I think we'll need clarification from @TheTurtle. –  JohnE 2 days ago
5  
@JohnE and Ypnypn This is something like I envisioned. This works. –  The Turtle 2 days ago
4  
You still are well below the code length limit. Why don't you change 999 to something bigger? –  DankMemes yesterday

Ruby, 1.628 × 10^237 programs

a=0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;_="a=%#x-1;_=%p;puts _%%[a,_]if a";puts _%[a,_]if a

Same approach as my Perl answer, but because Ruby handles big ints already, it's easier to store as hex.


Ruby, 9.277 × 10^90 programs

a=0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;b=0xf;(b<1)&&(a-=1)&&b=eval('0x'+'f'*(74-("%x"%a).length));_="a=%#x;b=%#x;(b<1)&&(a-=1)&&b=eval('0x'+'f'*(74-('%%x'%%a).length));_=%p;puts _%%[a,b-1,_]if a";puts _%[a,b-1,_]if a

So this attempt is a slightly different variation on the previous quine-like, but because of all the extra functions I'm not getting the number anywhere near as high as the other one... Was interesting to try another approach though!

share|improve this answer

Python 2, 9.7*10^229 programs

O=0
if len(hex(O))<191:print"O=0x%x"%(O+1)+open(__file__).read()[-68:]
share|improve this answer
    
Nice, didn't think of string repetition! –  Dom Hastings yesterday

C, 2.2 * 10^177 programs

#define S(s)char*q=#s,n[]="#####################################################################################################";i;s
S(main(){while(n[i]==91)n[i++]=35;i==101?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})

It's not perfect, but pretty good. I mean it's exactly 255 bytes long and generates programs of the same length. You could probably fiddle arround some more to gain some more programs, but I'll leave it as it is for now.

The program is based of a simple C quine. Additionally there's a quite simple counting algorithm which counts through all possible values of the char array n. We have as many programs as permutations of the string n.

The char range is limmited to a range from # (=35) to [ = (91). That's because I don't want any " or \ in the string, because they need to be escaped.

The program generation ends when all values in the char array n are [. Then it outputs a simple dummy program main(){}, which itself outputs nothing.

#define  S(s) char *q = #s; \
char n[] = "#####################################################################################################"; \ 
int i; \
int s;
S(main() {
    while(n[i]=='[') /* clear out highest value, so next array element be incremented */
        n[i++]='#'; 
    i==101 /* end of array reached? output dummy program */
        ? q = "main(){}"
        : n[i]++; /* count one up in the whole array */
    printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)", n, q);
})

As a demonstration that it should work I just changed the limits, so only characters between ASCII-Code 35 and 36 are used and only 4 array elements.

The resulting programs are

% echo > delim; find -iname 'program_*.c' | xargs -n1 cat delim

#define S(s)char*q=#s,n[]="####";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="$###";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="#$##";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="$$##";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="##$#";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="$#$#";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="#$$#";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="$$$#";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="###$";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="$##$";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="#$#$";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="$$#$";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="##$$";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="$#$$";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="#$$$";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="$$$$";i;s
S(main(){while(n[i]==36)n[i++]=35;i==4?q="main(){}":n[i]++;printf("#define S(s)char*q=#s,n[]=\"%s\";i;s\nS(%s)",n,q);})
#define S(s)char*q=#s,n[]="####";i;s
S(main(){})

This outputs 2^4 + 1 = 17 different programs.

So the program above outputs ((91-35)+1)^101 + 1 = 57^101 + 1 ~= 2.2 * 10^177 different programs. I'm not entierly sure if this counts, or if my calculation is even correct

share|improve this answer
1  
Could you please include that this is about 2.2 * 10^177 (for those who want to compare)? –  flawr yesterday
    
Didn't know how to calculate this one, but I included it ;-) –  MarcDefiant yesterday
    
wolframalpha.com =) –  flawr yesterday

Perl, 1 × 10^163

Otherwise this is a pretty basic quine, shrunk down to as few chars as possible, that only runs whilst the counter isn't 0.

use bigint;$i=9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999||die;$_=<<'e';eval
print"use bigint;\$i=$i-1||die;\$_=<<'e';eval
${_}e
"
e
share|improve this answer

Common Lisp, 10113-1

(LET ((X
       99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999))
  (WHEN #1=(PLUSP X)
    #2=(SETF *PRINT-CIRCLE* T)
    #3=(PRINT (LIST 'LET `((X ,(1- X))) (LIST 'WHEN '#1# '#2# '#3#)))))
  • There are 113 nines.
  • The next program has 112 nines followed by a 8
  • The next program has 112 nines followed by a 7
  • ...

The number of nines is limited by the maximum size of code, 256, taking into account the spaces introduced by the printer.

share|improve this answer

Perl, 1.4*10^225

use bignum;open$F,__FILE__;$_=<$F>;s/0x\w+/($&-1)->as_hex/e;0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff&&print

Similar approach to python; same result!

share|improve this answer

><>, 65534 (?) Programs

I've added a question mark next to 65533 since I have yet to verify that it can print 65533 (although I have reason to believe it should). Once I have a bit more time, I'm going to figure out a way to test it.

":?!;1-r00gol?!;a0.�

You can try it online here.

The gist of this program is that it changes the output of the character at the very end and then decrements its numerical value before printing. I got 65534 programs because the ascii value of the character at the end of the code is 65533, so counting the first program we have 65534 (if you count the empty program 65535, I guess). The last program "returned" is nothing; it simply ends when the character value is 0.

I'm quite sure it will be able to print a character for all iterations: I couldn't find a definitive source for how many characters ><> can print, but there are characters directly below 65533, numerically.

Let me know if there are any problems with this implementation; I'm a little unsure about the validity of my entry.


Explanation

I have shamelessly stolen the idea of using a single quotation mark to create a pseudo-quine from the ><> wiki and a comment I saw here once.

":?!;1-r00gol?!;a0.�
"                     begins string parsing
 :?!;                 terminates program if final character is 0, numerically
     1-               decrements final character by 1
       r              reverses stack
        00g           grabs quotation mark (fancy way of putting " ")
           ol?!;      prints and terminates if stack is empty
                a0.   jumps back to o to loop 

What it does is parse everything after the quotation mark as characters, then decrement the last one. From there it just reverses the stack (so as to print in the right order), pushes a quotation mark onto the stack, and then prints until the stack is empty.

share|improve this answer

Python, 1 × 10^189 programs

n=999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
if n!=0: print open(__file__).read().replace(str(n), str(n-1))

This must be run from a file, not an interactive repl. It is not a quine.

share|improve this answer
    
@ Cheese Lover The if n!=0 is redundant. You can just write if n. –  The Turtle 54 mins ago

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.