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.

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

share|improve this question
2  
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...". –  undergroundmonorail yesterday
3  
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. –  Geobits yesterday
    
@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? –  Mukul Kumar yesterday
1  
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. –  Geobits yesterday
    
@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). –  m.buettner yesterday
show 7 more comments

2 Answers

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
$ 
share|improve this answer
add comment

perl/perl, unlimited score

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 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).

share|improve this answer
    
What is the kind of output ? like you may give first non-repetitive lines. –  Mukul Kumar yesterday
    
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. –  tomsmeding yesterday
add comment

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.