Generate programs that print n times their length

16

1

Given a nonnegative integer n, your solution must output a program in your chosen language whose output has n times as many bytes as the outputted program.

Rules

  • You must specify the language and encoding of the programs your solution outputs, and you may not choose different languages or encodings for different inputs to your solution. The language of your output program may or may not be the same as the language of your solution.
  • Your submission only needs to handle integers in your language's range, but please do not abuse this rule.

This is so the shortest submission in bytes wins.

Example

Suppose n is 4. My solution then outputs f_8, a program which, in my (imaginary) language outputs j3 1s+/2]!mz. The program output has length 3 and its output has length 3 * 4 = 12, so the solution is correct for input 4.

Suppose instead that n is 1 and my program outputs ffffpfpffp (10 bytes). The program ffffpfpffp in my chosen language would have to produce an output of 10 * 1 = 10 bytes.

HyperNeutrino

Posted 2017-08-22T01:17:35.593

Reputation: 26 575

At the moment, this is pretty underspecified. Could you provide some examples? – James – 2017-08-22T01:32:17.467

3I think I know what we're supposed to do, but every time I read the spec, I become less certain. output some text that has n times as many bytes as the outputted program seems to imply that we have to output a text and a program. The character set and the language must be consistent for all integers. Which "character set" is this referring to? – Dennis – 2017-08-22T01:49:57.153

Nevermind, I actually did misunderstand, and so did all but one of the answers. – Dennis – 2017-08-22T01:53:54.517

1@Dennis It doesn't help that the title seems to contradict the body. Perhaps Output a program that outputs n times its length would be a better title, if I understand the challenge correctly (I'm not sure I do) – Sisyphus – 2017-08-22T01:55:08.213

"A program in your chosen language whose output has n times as many bytes as the outputted program". On what input? I would guess given no input, but that should be explicit. – Peter Taylor – 2017-08-22T07:31:15.173

Can I use HQ9+ as the output language? (I think consensus allows that now) if so then a 3-byte solution would be trivial. – Erik the Outgolfer – 2017-08-22T07:55:47.110

What happens if the two languages use different code pages? – Neil – 2017-08-22T08:38:16.007

1Can we take n as a string? – Shaggy – 2017-08-22T11:19:33.507

Answers

7

JavaScript (ES6), 38 bytes

n=>`(x="${n}")=>(x+1/7+1e9).repeat(x)`

Demo

let f =

n=>`(x="${n}")=>(x+1/7+1e9).repeat(x)`

function update() {
  let n = I.value,
      func = f(n),
      res = eval(func)();
  O.innerText =
    'n = ' + n + '\n\n' +
    'Function (' + func.length + ' bytes):\n' + func + '\n\n' +
    'Function output (' + res.length + ' bytes):\n' + res;
}
update()
<input type=range id=I min=0 max=100 oninput="update()">
<pre id=O>

Arnauld

Posted 2017-08-22T01:17:35.593

Reputation: 111 334

3

Jelly, 10 bytes

;“DL+8×x@⁶

Try it online!

For input 12, it outputs 12DL+8×x@⁶, which outputs 120 spaces. Try it online!

Leaky Nun

Posted 2017-08-22T01:17:35.593

Reputation: 45 011

3

brainfuck, 348 bytes

--[>+<++++++]>>--[>+<++++++]>++[-<+>]----[>+<----]>---[-<+>]----[>+<----]>-[-<+>]-[>+<---]>++++++[-<+>]-[>+<---]>++++++++[-<+>]--[>+<++++++]>+++[-<+>]<[<],[->.<]>>>>>.<<<.>>.<<<.>>>.<<<.>>>.<<<.>>...>>>.<<....<<.>>>.<<..<<.>>>..<<.......>>>>.<<<..<<.>.....>>>.<<.>>.>>.<<<<<.>>>>.<<.<<.>>.>.<<<.>>.<<<.>>...<<.>>>..>>.<<.>.<<<.>.<<.>>>.>>.<<<..>>>.

Try it online! Or see the Ungolfed version (i.e. what I had to work with)


Disclaimer

I spent more time making this than I thought was humanly possible. I'd like to thank my girlfriend for allowing me to ditch her to work on this; as well as my savior.

How does it even work?

No clue.

How does it work?

All outputs have a trailing snippet of code that are all the same:

[->+>+>+<<<]>>>>-[<<+>>-------]<<+-----[<[.-]>->[->+<<<+>>]>[-<+>]<<]

Let's split it into three parts called a,b,c

a : [->+>+>+<<<]>>>>               THE DUPLICATOR
b : -[<<+>>-------]<<+-----        THE ADJUSTER
c : [<[.-]>->[->+<<<+>>]>[-<+>]<<] THE PRINTER

The input i is simply tacked on to the front in unary:

iabc

(e.g; if the input was 10, then i = '++++++++++')

The Duplicator - Splits the input into two identical numbers m, n, equivalent to the input

The Adjuster - Adjusts n such that it equals the length of the program

The Printer - Prints m*n ASCII characters


Note that the input in the example is a newline, which as an ASCII value of 10, therefore the input is 10. If you want to test other small numbers, replace the , with however many +'s you desire.

Graviton

Posted 2017-08-22T01:17:35.593

Reputation: 2 295

2

Cheddar, 10 9 bytes

(*)&" "*9

An awkward Proton polyglot >_<

Downgoat

Posted 2017-08-22T01:17:35.593

Reputation: 27 116

+1 for Proton polyglot with Cheddar to show the inspiration :D – HyperNeutrino – 2017-09-18T16:23:33.600

2

Haskell, 55 bytes

f n=(++)<*>show$"main=putStr$[1.."++show n++"*2]>>'#':"

Try it online! Example usage: f 1 yields the following 54 bytes program:

main=putStr$[1..1*2]>>'#':"main=putStr$[1..1*2]>>'#':"

Try it online! which produces the following 54 byte output:

#main=putStr$[1..1*2]>>'#':#main=putStr$[1..1*2]>>'#':

Laikoni

Posted 2017-08-22T01:17:35.593

Reputation: 23 676

1

Python 3 -> HQ9+, 11 bytes

'Q'.__mul__

It had to be done

Try it online!

michi7x7

Posted 2017-08-22T01:17:35.593

Reputation: 405

Where can I test HQ? – Titus – 2017-08-22T08:49:29.873

1

Well, Q just prints its own source code. You can test that here: http://hq9plus.alwaysdata.net. This was never supposed to be a serious entry

– michi7x7 – 2017-08-22T08:52:53.613

This doesn't work. If I input 1, then you output Q, which in turn outputs Q. Q has length 1, but your code has length 11. – NoOneIsHere – 2017-09-18T15:06:26.790

@NoOneIsHere 'whose output has n times as many bytes as the outputted program.' (not the submitted program) – michi7x7 – 2017-09-19T16:33:25.673

@michi7x7 Ok, but the interpreter you linked outputs (for QQ) QQ\nQQ, which is 5 bytes, not 4. – NoOneIsHere – 2017-09-19T16:45:16.207

1

@NoOneIsHere this does not: https://almnet.de/esolang/hq9plus.php (The language specification only states "Prints the entire text of the source code file")

– michi7x7 – 2017-09-19T16:49:37.180

1

Java 8, 175 174 bytes

interface M{static void main(String[]a){System.out.printf("interface M{static void main(String[]a){int i=(88+%s)*%s;for(;i-->0;System.out.print(0));}}",a[0].length(),a[0]);}}

Examples:

n=1 outputs:

interface M{static void main(String[]a){int i=(88+1)*1;for(;i-->0;System.out.print(0));}}

(length=89) which outputs 89 zeroes:

00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

n=10 outputs:

interface M{static void main(String[]a){int i=(88+2)*10;for(;i-->0;System.out.print(0));}}

(length=90) which outputs 900 zeroes:

000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

n=100 outputs:

interface M{static void main(String[]a){int i=(88+3)*100;for(;i-->0;System.out.print(0));}}

(length=91) which outputs 9100 zeroes:

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Explanation:

interface M{                                // Class:
  static void main(String[]a){              //  Mandatory main method
    System.out.printf("interface M{static void main(String[]a){
                                            //   Print a new program with:
      int i=(88+%s)*%s;                     //    Integer containing (88*A)+B
      for(;i-->0;System.out.print(0));}}",  //    And print that many zeroes
        a[0].length(),                      //     Where A is the length of the number
                                            //     (0-9 = 1; 10-99 = 2; 100-999 = 3; etc.)
        a[0]);}}                            //     and B is the number itself

Kevin Cruijssen

Posted 2017-08-22T01:17:35.593

Reputation: 67 575

0

RProgN 2, 7 5 bytes

«•.* 

With a trailing space

Explained

«•.* 
«    # Yield a function from the remaining string.
 •.  # Append a space and stringify, which builds the original program (Because stringifying a function removes noops, which spaces are)
   * # Repeat the implicit input times.

Try it online!

ATaco

Posted 2017-08-22T01:17:35.593

Reputation: 7 898

0

CJam, 8 13 bytes

q_,S\" 8+*S*"

Try it Online

The generated program outputs spaces so it's kind of hard to tell.

geokavel

Posted 2017-08-22T01:17:35.593

Reputation: 6 352

I think this is the only answer that interprets the part about outputting a program that outputs a text correctly, but the ratio is off if the input has more than one digit. – Dennis – 2017-08-22T01:59:28.517

Oh yeah, duh.,, – geokavel – 2017-08-22T03:32:56.727

0

Ly, 29 bytes

"\"9>("&onu")[<&s&ol>1-]<;"&o

Try it online! (does not work currently due to a pre-processing bug) All good!

LyricLy

Posted 2017-08-22T01:17:35.593

Reputation: 3 313

0

Python → TECO, 20 bytes

The answer is in Python while the generated code is in TECO. The Python is a function returning VV12345\VV repeated n times. See here for an explanation of the TECO.

'VV12345\VV'.__mul__

feersum

Posted 2017-08-22T01:17:35.593

Reputation: 29 566

0

PHP, 47+1 bytes

<?="<?=str_pad(_,",strlen($argn)+18,"*$argn);";

prints one underscore followed by spaces.
Run as pipe with -F; run outputted program with -f or -F.

This would fail for input with more than 64 digits,
which is far higher than PHP_INT_MAX (at this time).

However, it fails for input larger than PHP_INT_MAX-18 ... does it still qualify?

Titus

Posted 2017-08-22T01:17:35.593

Reputation: 13 814

@HyperNeutrino: This will fail for input larger than PHP_INT_MAX-18. Does that disqualify? – Titus – 2017-08-22T08:31:44.923

Nah, I'll say that's fine :) – HyperNeutrino – 2017-08-23T00:38:09.970

0

PHP → Python 2, 40+1 bytes

print "A"*<?=13+strlen($argn),"*",$argn;

prints a Python program that prints repeated As. Run as pipe with -F.

Titus

Posted 2017-08-22T01:17:35.593

Reputation: 13 814

0

Batch → Charcoal, 22 bytes

I'm not sure which encoding I should be using, since these are bytes. Here are the bytes interpreted as Windows-1252:

@set/p=Á%1ñªÉñ«Ìñ¹<nul

The same bytes as PC-850:

@set/p=┴%1±¬╔±½╠±╣<nul

The same bytes in Charcoal's code page:

@set/p=A%1θ×Iθ⁺Lθ⁹<nul

The resulting Charcoal program is Plus(Length(Cast(n)), 9) bytes long:

A       Assign
 %1      (String representation of n)
   θ      To variable q
        Implicitly print a number of `-`s equal to:
×        Product of:
 Iθ       Cast of q to integer
 ⁺        Sum of:
  Lθ       Length of q
  ⁹        Integer constant 9

Neil

Posted 2017-08-22T01:17:35.593

Reputation: 95 035

0

JavaScript (ES8), 43 41 39 bytes

n=>`f=_=>"".padEnd(${n}*(88+f).length)`

Test it

The output of the generated function is a string of spaces which are replaced with *s in this Snippet.

g=
n=>`f=_=>"".padEnd(${n}*(88+f).length)`

o.innerText=(h=n=>`Function: ${x=g(n)}\nLength:   ${x.length}\nOutput:   "${x=eval(x)().replace(/./g,"*")}"\nLength:   `+x.length)(i.value=10);oninput=_=>o.innerText=h(+i.value)
<input id=i type=number><pre id=o>

Shaggy

Posted 2017-08-22T01:17:35.593

Reputation: 24 623

0

Recursiva, 16 15 bytes

++"*"V*15a'"*"'

Try it online!

This for input of n=2 prints:

*30"*"  

which outputs 30 *. Try it online!

officialaimm

Posted 2017-08-22T01:17:35.593

Reputation: 2 739

0

R, 46 bytes

function(n)sprintf("cat(rep('a',%d*23),'')",n)

Try it online!

Anonymous function that returns the string

cat(rep('a',n*23),'')

Which prints a (that's a followed by a space) 23 times n times. I needed the '' because otherwise cat wouldn't print the last space character.

Giuseppe

Posted 2017-08-22T01:17:35.593

Reputation: 21 077

0

C, 94 bytes

main(int c,char**a){if(c>1){c=atoi(a[1]);if(c>0&&c<0xFFFFFF){c*=94;while(c--)printf("r");}}}

this would be 94 bytes include the last \n that stadard C says it should be written. return as 'r' characters as the (lenght of the program) * (argument of the program) if the program argument not exist or it is <=0 or it is >0xFFFFF print nothing example

C:\>nameProg.exe 1
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

RosLuP

Posted 2017-08-22T01:17:35.593

Reputation: 3 036

0

MATLAB (63 bytes)

a=@(n)['repmat(''a'',1,',num2str(n*(15+numel(num2str(n)))),')']

For example:

>> a(5)

ans =

repmat('a',1,80)

and:

>> repmat('a',1,80)

ans =

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

PieCot

Posted 2017-08-22T01:17:35.593

Reputation: 1 039