Write a program to compose a quine of a specified length

7

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.

AJMansfield

Posted 2013-12-19T01:42:18.270

Reputation: 2 758

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 – 2013-12-19T01:44:39.780

so wait... more possible supported sizes is penalized?! – Doorknob – 2013-12-19T01:48:44.960

@DoorknobofSnow Sorry, fixed it. – AJMansfield – 2013-12-19T01:51:11.417

2Perhaps 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 – 2013-12-19T15:33:50.443

@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 – 2013-12-19T23:24:31.063

Answers

9

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.

Ilmari Karonen

Posted 2013-12-19T01:42:18.270

Reputation: 19 513

1Damn 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 – 2013-12-19T15:24:29.563

@Iszi: No, but PHP will echo back verbatim anything outside <? ?> tags. – Ilmari Karonen – 2013-12-19T15:29:20.943

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 – 2013-12-19T15:30:47.600

Beat me to it in APL, and smaller – TwiNight – 2013-12-19T23:53:02.150

The first character ~ is not even necessary. For me, 1\*` works perfectly. – ProgramFOX – 2013-12-21T18:54:13.493

@ProgramFOX: I assumed the input was to be provided on stdin, in which case the ~ is needed to turn it from a string into a number. You're right that, if the desired length is provided as a number on the stack, three characters is enough. – Ilmari Karonen – 2013-12-21T20:05:42.003

@IlmariKaronen: I understand. When I tested it, I had to push the input as a number on the stack (I didn't see a way to provide the input using STDIN, I tried the way as said in the GolfScript tutorial, but it doesn't seem to work on my Windows computer).

– ProgramFOX – 2013-12-22T08:47:04.790

@ProgramFOX: An easy way to test GolfScript code using "canned" input is to insert ;'input goes here' at the beginning of the program. – Ilmari Karonen – 2014-01-07T06:20:53.447

4

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)

Doorknob

Posted 2013-12-19T01:42:18.270

Reputation: 68 138

Sorry, fixed it, I meant the exponent to be negative. – AJMansfield – 2013-12-19T01:50:33.023

@AJMansfield Yay, now I have a score of negative infinity :P Impossible to beat! – Doorknob – 2013-12-19T01:51:02.177

1No. 512 * x^(-3/4) tends toward 0 as x increases without bound. – AJMansfield – 2013-12-19T01:52:09.107

@AJMansfield Whoops, I fail at math. Fixed – Doorknob – 2013-12-19T01:52:26.340

No, still wrong, the score is 71. – AJMansfield – 2013-12-19T01:52:54.987

@AJMansfield Gah! Sorry, really not thinking straight at all :P – Doorknob – 2013-12-19T01:53:44.130

2+1 Solving the problem with "real" programming languages instead of taking advantage of esoteric languages. I'm impressed! – Kevin – 2013-12-19T06:23:23.620

@Kevin I don't really take advantage of esoteric languages. I use them. For instance, it is rather fun (ie difficult) to solve project euler problems using Befunge. – Justin – 2013-12-19T22:16:58.033

@Quincunx I was comparing your answer to the others that output quines in H9Q+, which has a quine operator, making the problem trivial. I don't doubt that languages like Befunge are challenging to use! – Kevin – 2013-12-19T22:30:52.870

3

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:$$>,:# _@

Justin

Posted 2013-12-19T01:42:18.270

Reputation: 19 757

3

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

Blckknght

Posted 2013-12-19T01:42:18.270

Reputation: 701

Deserves +10 for not trying to dodge the rules by using dummy languages. – AJMansfield – 2013-12-20T01:28:07.743

2

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+++++++++

Doorknob

Posted 2013-12-19T01:42:18.270

Reputation: 68 138

0

Ruby -> Ruby (79 points)

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

user11485

Posted 2013-12-19T01:42:18.270

Reputation:

-2

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.

i`A:""_Str1:"1"_Str2l`1:A--A:Str1+Str2_Str1@Ag`1d`Str1

Timtech

Posted 2013-12-19T01:42:18.270

Reputation: 12 038