# Write a program to compose a quine of a specified length

Write a program, in a language of your choice, that will compose a quine program of a requested size. The generated program may be in the same or a different language, if desired.

Programs are scored based on the following formula:

generating_program_length + smallest_supported_size + 512 * number_of_supported_sizes ^ (-3/4)

Halve your score if you support every size between the minimum and maximum supported sizes.

Lowest score wins.

• Also, to explain the rather complicated scoring criteria, the point is to ensure trivial "any color as long as it's black" solutions don't work, and to encourage solutions that tend toward supporting all the numbers possible, without penalizing more limited solutions too harshly. – AJMansfield Dec 19 '13 at 1:44
• so wait... more possible supported sizes is penalized?! – Doorknob Dec 19 '13 at 1:48
• @DoorknobofSnow Sorry, fixed it. – AJMansfield Dec 19 '13 at 1:51
• Perhaps a little late, but I think there should be a significant bonus here for programs that produce the quines in their own language. Otherwise, everyone's going to be writing quine generators for HQ9+ or languages where quining is similarly simple. – Iszi Dec 19 '13 at 15:33
• @Iszi I think you're right, also I have another possible change that could eliminate trivial quining solutions: output must not contain any matches of the regex (.+)\1. – AJMansfield Dec 19 '13 at 23:24

## GolfScript → GolfScript: (4 + 0 + 0) / 2 = 2

~1*

• Program length: 4
• Minimum supported size: 0
• Maximum supported size: unbounded
• All sizes supported: yes

This program simply reads an integer n from its input, and outputs an n-digit integer consisting of n 1 digits. For example, the input 5 yields the output 11111. In GolfScript, any integer literal (without leading zeros) is a quine, and since GolfScript integers can be arbitrarily large, the maximum supported length is only bounded by the amount of memory available.

For the input 0, the code outputs an empty program, which is also a quine in GolfScript.

Ps. The generated quines work for several other languages too, including PHP.

• Damn Golfscript and its arbitrary precision. I was going to do the same in PowerShell, but it falls back to scientific notation after a point. Does PHP support arbitrary precision by default also? – Iszi Dec 19 '13 at 15:24
• @Iszi: No, but PHP will echo back verbatim anything outside <? ?> tags. – Ilmari Karonen Dec 19 '13 at 15:29
• Ah. For PowerShell you'd have to force the integer to a string with quotes if it's too big, at which point that alone is no longer a quine. – Iszi Dec 19 '13 at 15:30
• Beat me to it in APL, and smaller – TwiNight Dec 19 '13 at 23:53
• The first character ~ is not even necessary. For me, 1* works perfectly. – ProgramFOX Dec 21 '13 at 18:54

# Ruby -> JavaScript, 38

45 character program
31 smallest supported size
Infinity possible sizes
Supports any size in range
(45 + 31 + 512 * Infinity ^ -3/4) / 2 = 38

"function f(){alert(f+'f()')#{?;*(i-31)}}f()"


# GolfScript -> JavaScript, 37.5

1 char less :P

"function f(){alert(f+'f()')"";"i 31-*"}f()"


Example output for i = 31:

function f(){alert(f+'f()')}f()


For i = 41:

function f(){alert(f+'f()');;;;;;;;;;}f()


The output quines work on latest Chrome (31.0.1650.63), at least. (Untested for any other browser)

• Sorry, fixed it, I meant the exponent to be negative. – AJMansfield Dec 19 '13 at 1:50
• @AJMansfield Yay, now I have a score of negative infinity :P Impossible to beat! – Doorknob Dec 19 '13 at 1:51
• No. 512 * x^(-3/4) tends toward 0 as x increases without bound. – AJMansfield Dec 19 '13 at 1:52
• @AJMansfield Whoops, I fail at math. Fixed – Doorknob Dec 19 '13 at 1:52
• +1 Solving the problem with "real" programming languages instead of taking advantage of esoteric languages. I'm impressed! – Kevin Dec 19 '13 at 6:23

# Python 2 - 62.6 points

s='s=%r;print(s%%s+s*99)[:input()]#';print(s%s+s*99)[:input()]


This code is both the generator and the core of the quine. It expects you to provide a length on standard input, and prints to standard output a version of itself of that length (larger sizes get a trailing comment full of junk).

So, if you pass it 62, it prints itself (this is the minimum size). If you call it with some longer length (up to the maximum length of 3231), it will produce output of that length. Any version will be a quine if passed its own length.

The length limit was the result of my attempts at minimizing the number of characters used. Supporting more lengths was costing more in points for code length than it was reducing the penalty for a limited range. It's not hard to make a variant that will work for unlimited sizes, but it will need to be longer by about 20 characters (and so score worse).

Note that requesting a shorter length than 62 will produce output of the desired size, but that output will not be working code. Passing a larger length than 3231 will produce the 3231-character version of the quine. Passing something other than an integer will raise an exception.

Scoring:

• Generator length: 62 characters, 62 points
• Minimum quine size: 62 characters, 62 points
• Number of supported sizes: 3169, 1.2 points
• All sizes between minimum and maximum accepted: Half off all points above.
• Total: (62+62+1.212)/2 = 62.6 points
• Deserves +10 for not trying to dodge the rules by using dummy languages. – AJMansfield Dec 20 '13 at 1:28

# Befunge 98 - (14 + 0 + 0) / 2 = 7

'1&k:$$>,:# _@  Output is in HQ9+ Golfscript (Thanks to Ilmari) Output for 0:   Output for 1: 1  Output for 10: 1111111111  Explanation: '1 ;push the character '1' onto the stack & ;get the inputted number (n), push on stack '1&k: ;duplicate the '1', n + 1 times, leaving n + 2 of '1' on the stack '1&k:$$        ;drop the top two '1's on the stack
>,      ;print the top element of the stack
>,:# _  ;duplicate the next, then check if it is 0.
@ ;if it is 0, execute this, which ends the program.
, # _  ;otherwise, go back the other way and continue printing
>       ;turn back to the right.
'1&k:>,:# _@


# Ruby or GolfScript -> HQ9+, 6

11 character program
1 smallest supported size
Infinity possible sizes
Supports any size in range
(11 + 1 + 512 * Infinity ^ -3/4) / 2 = 6

?Q+?+*(i-1)


Same size program written in GolfScript:

"Q""+"i 1-*


Example output for i = 1:

Q


For i = 10:

Q+++++++++


# Ruby -> Ruby (79 points)

char: 51
min size: 28
max size: infinite
(0..num){|x|print "eval s=\"print 'eval s=';p s\""}

## Golf-Basic 84 -> Golf-Basic 84

(54 + 0 + 0) / 2 = 27

• Program length: 54
• Minimum supported size: 0
• Maximum supported size: none
• All sizes supported: yes

Only limited by the calculator's RAM.

iA:""_Str1:"1"_Str2l1:A--A:Str1+Str2_Str1@Ag1dStr1