That then inspires another question: given a function, foo(), that we wish to call exactly 1000 times, what is the way to do it in the least amount of code given that we are only allowed the following:
1. calling foo().
2. defining new functions, whose bodies consist entirely of calls to previously defined functions or to foo.
3. calling those new functions.
For computing amount of code, let's take it as 1 line per function call, and 1 line per function defined.
So, for example (in pseudocode):
function foo_50
foo
foo
...
foo
foo_50
foo_50
...
foo_50
where there are 50 foo's in foo_50, and 20 calls to foo_50, would be a program of length 71. Breaking it down by powers of two: function foo_2
foo
foo
function foo_4
foo_2
foo_2
...
function foo_512
foo_256
foo_256
foo_512
foo_256
foo_128
foo_64
foo_32
foo_8
would have length 33.Is that the best we can do? How about for a general N? How about writing a program that given N writes the optimal program in the above form for N?
def bar(baz):
baz()
baz()
bar(bar(bar(bar(foo)))) and bar(bar(foo)) and foo()
for N=21, for example. Actually, you can make it two lines if your goal is really to minimize LOC: def bar(baz): baz() and baz()
bar(bar(bar(bar(foo)))) and bar(bar(foo)) and foo()Same goes for `bar(bar(...)) and bar()`; all the `and`s should be commands instead in my solution above. Of course, this is Python-specific.