12
\$\begingroup\$

Your challenge is to create a shortest code in your language of choice which writes on a .txt file the longest code to multiply two numbers by taking two numbers as input and output the answer.

NO INPUT NEEDED TO YOUR CODE WHICH IS DOING THE WRITING JOB!

The generator code and generated code can be in any languages

Do NOT use unlimited series or progression to make text file big.
Do NOT use unnecessary statements to increase the score.

SCORING

( size of text file generated in bytes )  (without any unnecessary white spaces, tabs or newlines)
-----------------------------------------
     ( size of your code in bytes )  

Winner

Winner will be chosen after 1 week from now with the highest score.

EDIT : range of input in code multiplying two numbers should be in between -32768 to 32767

\$\endgroup\$
15
  • 3
    \$\begingroup\$ I eventually figured out, but the fact that you use code formatting for the output made it seem like you were looking for the literal string "the longest code to...". \$\endgroup\$ Apr 4, 2014 at 18:09
  • 3
    \$\begingroup\$ I think you can just use this answer by Comintern, changing the + to * in the generator program he provides, and you probably already know this, since you answered that question as well. \$\endgroup\$
    – Geobits
    Apr 4, 2014 at 18:21
  • \$\begingroup\$ @Geobits I tried to keep my question away from that answer, but I think the effort is not enough, what should I ask to generate then, so that The question has nothing to relate from other question-answers? \$\endgroup\$ Apr 4, 2014 at 18:29
  • 1
    \$\begingroup\$ I don't know what you should ask, it just seems like any winner here is going to be eerily similar to the answers there. \$\endgroup\$
    – Geobits
    Apr 4, 2014 at 18:30
  • \$\begingroup\$ @MukulKumar maybe the long code should generate the short code? But don't change this question to that, the change is way too substantial. But it might be an idea for another quine-related challenge (if it hasn't been asked before). \$\endgroup\$ Apr 4, 2014 at 18:31

5 Answers 5

11
\$\begingroup\$

perl/perl, unlimited score under original rule with unlimited range

Here's some code which doesn't win:

$l=-2**5-1;
$h=2**5;
sub p{print@_}
p"sub add {(\$i,\$j)=\@_;\n";
for($i=$l;$i<=$h;++$i){
  for ($j=$l;$j<=$h;++$j){
    p" return ".($i*$j)." if (\$i == $i) && (\$j == $j);\n";
  }
}
p"}print add(<>,<>)\n";

The output has this form:

sub add {($i,$j)=@_;
 return 1089 if ($i == -33) && ($j == -33);
 return 1056 if ($i == -33) && ($j == -32);
 return 1023 if ($i == -33) && ($j == -31);
 return 990 if ($i == -33) && ($j == -30);
...
 return 928 if ($i == 32) && ($j == 29);
 return 960 if ($i == 32) && ($j == 30);
 return 992 if ($i == 32) && ($j == 31);
 return 1024 if ($i == 32) && ($j == 32);
}print add(<>,<>)

the output file is 181030 bytes long, but after stripping spaces and newlines, it is only 133109 bytes long. so, the score is 133109/248 = 536.7289...

Here's some other code that doesn't win - it is the same program except the first 2 lines:

$l=-2**6-1;
$h=2**6;

the output file is 718138 bytes long, but after stripping spaces and newlines, it is only 532233 bytes long. so, the score is 532233/248 = ~2146. better! using 7 yields a score of ~8750, 8 yields ~35347, 9 yields ~149129, 10 yields 151100000 non-space / 250 = 604,400 ....

of course we can do this as long as we want. the size of the source program, n, will increase as O(log(n)). the size of the output program is O(2*n). The limit of 2*n/log(n) as n goes to infinity is clearly infinity, so if I just substitute in my favorite large number, a googolplex, I win (until someone suggests googolplex+1).

\$\endgroup\$
7
  • \$\begingroup\$ What is the kind of output ? like you may give first non-repetitive lines. \$\endgroup\$ Apr 5, 2014 at 2:09
  • \$\begingroup\$ First time I could actually read Perl. It generates a function named add which takes two parameters. It then populates that function with return statements looking like return 39 if ($i == 13) && ($j == 3);, using all values from $l to $h for $i and $j. Smartass bending of the "only unnecessary statements" rule. \$\endgroup\$
    – tomsmeding
    Apr 5, 2014 at 5:50
  • \$\begingroup\$ Mukul, I edited the answer to include some sample output. \$\endgroup\$ Apr 7, 2014 at 17:51
  • \$\begingroup\$ @skibrianski well that's finite then, because the value the variables i,j can hold are finite. But, quite large. \$\endgroup\$ Apr 7, 2014 at 19:09
  • \$\begingroup\$ Mujul. Finite, but unlimited. Give me any number and I can choose a larger size output (or score) by simply adjusting those variables. \$\endgroup\$ Apr 7, 2014 at 20:08
10
\$\begingroup\$

C, 27297/245 = 111.4

Source code (245 bytes)

#include<stdio.h>
main(int c,char**v){char*s;FILE*f=fopen("/tmp/x.c","w");fprintf(f,"#include<stdio.h>\n#include<stdlib.h>\nmain(){int a=%s,b=%s;printf(\"%s * %s = %%d\\n\",a*b);}",v[1],v[2],v[1],v[2]);fclose(f);system("cc -E /tmp/x.c >add.c");}

When compiled and run with two integer arguments on the command line, this generates another C file containing the necessary code to calculate their product, and compiles it with the -E flag. This flag specifies that the compiler should stop after the preprocessing stage and output the processed source code (which will include the entire contents of stdio.h and stdlib.h).

Output file (27297 bytes)

# 1 "/tmp/x.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/tmp/x.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 64 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/_types.h" 1 3 4
# 27 "/usr/include/_types.h" 3 4
# 1 "/usr/include/sys/_types.h" 1 3 4
# 32 "/usr/include/sys/_types.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 33 "/usr/include/sys/_types.h" 2 3 4

********* LINES 13-1273 OMITTED *********

long long
  strtoq(const char *, char **, int);
unsigned long long
  strtouq(const char *, char **, int);

extern char *suboptarg;
void *valloc(size_t);







# 3 "/tmp/x.c" 2
main(){int a=6,b=7;printf("6 * 7 = %d\n",a*b);}

Result of running the output code

The output file is saved as add.c, which can be compiled and run normally:

$ ./a.out 6 7
$ cc add.c -o add
$ ./add
6 * 7 = 42
$ 
\$\endgroup\$
4
\$\begingroup\$

perl, 125 chars, score 1,630,326,497.312

Same basic approach as my other answer, but this time bounded between -32768 and 32767 per the updated rules, and all unnecessary whitespace stripped completely:

$l=-($h=32767)-1;print"sub a{";for$i($l..$h){print"return ".$i*$_." if\$_[0]==$i&&\$_[1]==$_;"for$l..$h;}print"}print a<>,<>"

The output program begins like so:

sub a{return 1073741824 if $_[0]==-32768&&$_[1]==-32768;return 1073709056 if $_[0]==-32768&&$_[1]==-32767;

and ends:

return 1073643522 if $_[0]==32767&&$_[1]==32766;return 1073676289 if $_[0]==32767&&$_[1]==32767;}print a<>,<>

The output program is 190GB long. More precisely, 203790812164 bytes. Score = 203790812164 / 125 = 1630326497.312

\$\endgroup\$
4
\$\begingroup\$

Windows Command Script: ~1,000,000,000 pts

Code: 158 bytes

Output: ~158000000000 bytes

echo>.txt @goto :%%1_%%2
set,=for /l %%a in (-32768 1 32767) do call :
%,%1 %%a
:1
%,%2 %1 %%a
:2
set/a]=%1*%2
echo :%1_%2>>.txt
echo @echo.%]%^&exit/b0>>.txt

The output pretty much consists of:

@goto :%1_%2
:-32768_-32768
@echo.1073741824&exit/b0
:-32768_-32767
@echo.1073709056&exit/b0
...
:32767_32767
@echo.1073676289&exit/b0
\$\endgroup\$
2
\$\begingroup\$

Python, 142 bytes

c='def m(a,b):\n'
L=32768
a=-L
while(a<L):
 b=-L
 while(b<L):c+=f' if a=={a}and {b}==b:return {a*b}\n';b+=1
 a+=1
F=open('c.txt','w')
F.write(c)

Score unknown for the moment, but I think it's more than a billion. Nope, probably just below a billion.

Score update

The program has a minimum length of 111669149696 or about 111 billion. This means that the minimum score of this answer is roughly 780 million.

It also has a maximum length of roughly 150 billion, which indicates that the score is between 730 and 1050 million. My score is most likely in the middle of that range, i.e. about 890 million.

Basically writes a function m to a file c.txt using two while loops.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.