Update Thursday, 29th of January, Anno Domini MMIX, at the hour of the Buffalo
[git/dscho.git] / source-1233022809.txt
blobdc8a0ae959f67ae25427af3e57696e692444624a
1 Fun with calculus after midnight
3 Problem: what is the shortest way of defining a variable consisting of ''N''
4 spaces?  I.e. for ''N=80'' the result will look something like
6 <bash>
7 s='    '
8 s="$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s"
9 </bash>
11 Let's see.  Let the minimal number of characters needed be ''A(N)''.  For
12 simplicity, let's say that we only use one variable.  Then, certainly, ''A(N)''
13 cannot be larger than ''5+N'', as we could define a variable using 1 character
14 for the name, 1 for the equal sign, 2 for the quotes, and one for the semicolon
15 or newline character (whichever).
17 Now, let's assume ''N'' is a product ''K*L''.  Then certainly, ''A(N)'' cannot
18 be larger than ''A(K)+5+2*L'', as we could first define a variable that has
19 exactly ''K'' spaces and then use that to define the end result (in the example
20 above, ''K=5'' and ''L=20'').
22 So, for which ''N=K*L'' is it better to use two definitions instead of one?
24 Simple calculus says that ''5+K*L>5+K+5+2*L'' must hold true, or (after some
25 scribbling): ''L>1+7/(K-2)''. Which means that it makes no sense to define
26 a variable with 1 or 2 spaces first, which is kinda obvious (writing '$s'
27 alone would use two characters, so we could write the spaces right away).
29 But what for the other values?  For ''K=3'', ''L'' must be at least 9 to make
30 sense (in other words, ''N'' must be at least 27).  For ''K=4'', ''L'' needs
31 to be greater or equal to 5 (''N>=20''), the next pairs are ''(5,4)'',
32 ''(6,3)'', ''(7,3)'', ''(8,3)'', ''(9,3)'' and starting with ''K=10'', any
33 ''L>1'' makes sense.
35 The second definition can also contain spaces at the end, however, so for any
36 ''N=K*L+M'', ''A(N)'' cannot be larger than ''A(K)+5+2*L+M''.
38 Not surprisingly, this leads to exactly the same ''L>1+7/(K-2)'' (as we can
39 append the ''M'' spaces in the last definition, no matter if we use 1 or
40 2 definitions).
42 However, that means that as soon as ''N>=18'', we should use two definitions,
43 prior to that, it makes no sense.
45 So for ''N<18'', ''A(N)=5+N''.
47 But what ''K'' should one choose, i.e. how many spaces in the first definition?
48 In other words, what is ''A(N)'' given that we use two definitions?
50 That will have to wait for another midnight.  Just a teaser: ''A(80)=36''.  Oh,
51 and with 80 characters, you can define a string of 9900 spaces...