home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / puzzles / archive / combinatorics < prev    next >
Encoding:
Internet Message Format  |  1996-04-26  |  22.1 KB

  1. Received: from MIT.EDU (SOUTH-STATION-ANNEX.MIT.EDU [18.72.1.2]) by bloom-picayune.MIT.EDU (8.6.13/2.3JIK) with SMTP id OAA02196; Sat, 20 Apr 1996 14:39:30 -0400
  2. Received: from [199.164.164.1] by MIT.EDU with SMTP
  3.     id AA07826; Sat, 20 Apr 96 14:11:27 EDT
  4. Received: by questrel.questrel.com (940816.SGI.8.6.9/940406.SGI)
  5.     for news-answers-request@mit.edu id LAA25207; Sat, 20 Apr 1996 11:12:00 -0700
  6. Newsgroups: rec.puzzles,news.answers,rec.answers
  7. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!spool.mu.edu!howland.reston.ans.net!europa.eng.gtefsd.com!uunet!questrel!chris
  8. From: chris@questrel.questrel.com (Chris Cole)
  9. Subject: rec.puzzles Archive (combinatorics), part 05 of 35
  10. Message-Id: <puzzles/archive/combinatorics_745653851@questrel.com>
  11. Followup-To: rec.puzzles
  12. Summary: This is part of an archive of questions
  13.  and answers that may be of interest to
  14.  puzzle enthusiasts.
  15.  Part 1 contains the index to the archive.
  16.  Read the rec.puzzles FAQ for more information.
  17. Sender: chris@questrel.questrel.com (Chris Cole)
  18. Reply-To: archive-comment@questrel.questrel.com
  19. Organization: Questrel, Inc.
  20. References: <puzzles/archive/Instructions_745653851@questrel.com>
  21. Date: Wed, 18 Aug 1993 06:04:32 GMT
  22. Approved: news-answers-request@MIT.Edu
  23. Expires: Thu, 1 Sep 1994 06:04:11 GMT
  24. Lines: 578
  25. Xref: senator-bedfellow.mit.edu rec.puzzles:24991 news.answers:11511 rec.answers:1911
  26. Apparently-To: news-answers-request@mit.edu
  27.  
  28. Archive-name: puzzles/archive/combinatorics
  29. Last-modified: 17 Aug 1993
  30. Version: 4
  31.  
  32.  
  33. ==> combinatorics/alphabet.blocks.p <==
  34. What is the minimum number of dice painted with one letter on all six sides
  35. such that all permutations without repetitions of n letters can be formed
  36. by placing n dice together in a line?
  37.  
  38. ==> combinatorics/alphabet.blocks.s <==
  39. n=   2          3          4          5          6
  40.    (8,4)      (9,7)      (9,3)      (10,7)     (11,7)
  41.  
  42.    aijklm     abcde?     acdefg     abcde?     abkmuz
  43.    bijklm     fghij?     bhijkl     fghij?     bcpwy?
  44.    cnopqr     klmno?     cmnopq     klmno?     cdlnvz
  45.    dnopqr     pqrst?     dhnrvy     pqrst?     deqxu?
  46.    estuvw     uvwxy?     eiosvw     uvwxy?     efmowz
  47.    fstuvw     afkpu?     fjptwx     afkpu?     fgryv?
  48.    gxyz??     bglqv?     gkquxy     bglqv?     ghnkx?
  49.    hxyz??     chmrwz     lmrstu     chmrwz     hisuw?
  50.               dinsxz     zab???     dinsxz     ijoly?
  51.                                     ejotyz     jatvx?
  52.                                                pqrstz
  53.  
  54. I think I can prove that there is no solution with 11 dice with 9
  55. don't cares or with 10 dice, but I haven't checked all the details, so
  56. I might have made a mistake.  In any case, that leaves open the case
  57. of 11 dice with 8 don't cares; my guess is that it is not possible.
  58.  
  59. -- John Rickard (jrickard@eoe.co.uk)
  60.  
  61. ==> combinatorics/coinage/combinations.p <==
  62. Assuming you have enough coins of 1, 5, 10, 25 and 50 cents, how many
  63. ways are there to make change for a dollar?
  64.  
  65. ==> combinatorics/coinage/combinations.s <==
  66. 292.  The table is shown below:
  67.  
  68. Amount  00 05 10 15 20 25 30 35 40 45 50 55 60 65  70  75  80  85  90  95  100
  69. Coins  
  70. .01      1  1  1  1  1  1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1
  71. .05      1  2  3  4  5  6  7  8  9 10 11 12 13 14  15  16  17  18  19  20  21
  72. .10      1  2  4  6  9 12 16 20 25 30 36 42 49 56  64  72  81  90 100 110 121
  73. .25      1  2  4  6  9 13 18 24 31 39 49 60 73 87 103 121 141 163 187 214 242
  74. .50      1  2  4  6  9 13 18 24 31 39 49 62 77 93 112 134 159 187 218 253 292
  75.  
  76. The meaning of each entry is as follows:
  77. If you wish to make change for 50 cents using only pennies, nickels and dimes,
  78. go to the .10 row and the 50 column to obtain 36 ways to do this.
  79.  
  80. To calculate each entry, you start with the pennies.  There is exactly one
  81. way to make change for every amount.  Then calculate the .05 row by adding 
  82. the number of ways to make change for the amount using pennies plus the number
  83. of ways to make change for five cents less using nickels and pennies.  This 
  84. continues on for all denominations of coins.  
  85.  
  86. An example, to get change for 75 cents using all coins up to a .50, add the
  87. number of ways to make change using only .25 and down (121) and the number of
  88. ways to make change for 25 cents using coins up to .50 (13).  This yields the
  89. answer of 134.
  90.  
  91. ==> combinatorics/coinage/dimes.p <==
  92. "Dad wants one-cent, two-cent, three-cent, five-cent, and ten-cent
  93. stamps.  He said to get four each of two sorts and three each of the
  94. others, but I've forgotten which.  He gave me exactly enough to buy
  95. them; just these dimes."  How many stamps of each type does Dad want?
  96. A dime is worth ten cents.  -- J.A.H. Hunter
  97.  
  98. ==> combinatorics/coinage/dimes.s <==
  99. The easy way to solve this is to sell her three each, for
  100. 3x(1+2+3+5+10) = 63 cents.  Two more stamps must be bought, and they
  101. must make seven cents (since 17 is too much), so the fourth stamps are
  102. a two and a five.
  103.  
  104. ==> combinatorics/coinage/impossible.p <==
  105. What is the smallest number of coins that you can't make a dollar with?
  106. I.e., for what N does there not exist a set of N coins adding up to a dollar?
  107. It is possible to make a dollar with 1 current U.S. coin (a Susan B. Anthony),
  108. 2 coins (2 fifty cent pieces), 3 coins (2 quarters and a fifty cent piece),
  109. etc.  It is not possible to make exactly a dollar with 101 coins.
  110.  
  111. ==> combinatorics/coinage/impossible.s <==
  112. The answer is 77:
  113.  
  114. a) 5c  = 1 or 5;
  115. b) 10c = 1 or 2 a's (1,2,6,10)
  116. c) 25c = 1 or 2 b's + 1 a
  117. d) 50c = 1 or 2 c's
  118. e) $1  = 1 or 2 d's
  119.  
  120. total    penny    nickel    dime    quarter    half
  121. 5        1    2    1    1
  122. 6        3    1    1    1
  123. 7        5        1    1
  124. 8        4    3        1
  125. 9        6    2        1
  126. 10        8    1        1
  127. 11        10            1
  128. 12        7    4    1
  129. 13        9    3    1
  130. 14        11    2    1
  131. 15        13    1    1
  132. 16        15        1
  133. 17        14    3
  134. 18        16    2
  135. 19        18    1
  136. 20        20
  137. 21    5    13    3
  138. 22    5    15    2
  139. 23    5    17    1
  140. 24    5    19
  141. 25    10    12    3
  142. 26    10    14    2
  143. 27    10    16    1
  144. 28    10    18
  145. 29    15    11    3
  146. 30    15    13    2
  147. 31    15    15    1
  148. 32    15    17
  149. 33    20    10    3
  150. 34    20    12    2
  151. 35    20    14    1
  152. 36    20    16
  153. 37    25    9    3
  154. 38    25    11    2
  155. 39    25    13    1
  156. 40    25    15
  157. 41    30    8    3
  158. 42    30    10    2
  159. 43    30    12    1
  160. 44    30    14
  161. 45    35    7    3
  162. 46    35    9    2
  163. 47    35    11    1
  164. 48    35    13
  165. 49    40    6    3
  166. 50    40    8    2
  167. 51    40    10    1
  168. 52    40    12
  169. 53    45    5    3
  170. 54    45    7    2
  171. 55    45    9    1
  172. 56    45    11
  173. 57    50    4    3
  174. 58    50    6    2
  175. 59    50    8    1
  176. 60    50    10
  177. 61    55    3    3
  178. 62    55    5    2
  179. 63    55    7    1
  180. 64    55    9
  181. 65    60    2    3
  182. 66    60    4    2
  183. 67    60    6    1
  184. 68    60    8
  185. 69    65    1    3
  186. 70    65    3    2
  187. 71    65    5    1
  188. 72    65    7
  189. 73    70        3
  190. 74    70    2    2
  191. 75    70    4    1
  192. 76    70    6
  193. 77    can't be done
  194. 78    75    1    2
  195. 79    75    3    1
  196. 80    75    5
  197. 81    can't be done
  198. 82    80        2
  199. 83    80    2    1
  200. 84    80    4    
  201. 85    can't be done
  202. 86    can't be done
  203. 87    85    1    1
  204. 88    85    3    
  205. 89    can't be done
  206. 90    can't be done
  207. 91    90        1
  208. 92    90    2
  209. 93-95    can't be done
  210. 96    95    1
  211. 97-99    can't be done
  212. 100    100
  213.  
  214. ==> combinatorics/color.p <==
  215. An urn contains n balls of different colors.  Randomly select a pair, repaint
  216. the first to match the second, and replace the pair in the urn.  What is the
  217. expected time until the balls are all the same color?
  218.  
  219. ==> combinatorics/color.s <==
  220. (n-1)^2.
  221.  
  222. If the color classes have sizes k1, k2, ..., km, then the expected number of
  223. steps from here is (dropping the subscript on k):
  224.  
  225.      2               k(k-1)              (j-1) (k-j)
  226. (n-1)  -  SUM      ( ------  +   SUM   --------------- )
  227.          classes,      2        1<j<k      (n-j)
  228.       class.size=k
  229.  
  230. The verification goes roughly as follows.  Defining  phi(k) as  (k(k-1)/2 +
  231. sum[j]...), we first show that  phi(k+1) + phi(k-1) - 2*phi(k) = (n-1)/(n-k)
  232. except when k=n; the k(k-1)/2 contributes 1, the term j=k contributes
  233. (j-1)/(n-j)=(k-1)/(n-k), and the other summands j<k contribute nothing.
  234. Then we say that the expected change in phi(k) on a given color class is
  235. k*(n-k)/(n*(n-1)) times (phi(k+1) + phi(k-1) - 2*phi(k)), since with
  236. probability k*(n-k)/(n*(n-1)) the class goes to size k+1 and with the same
  237. probability it goes to size k-1.  This expected change comes out to k/n.
  238. Summing over the color classes (and remembering the minus sign), the
  239. expected change in the "cost from here" on one step is -1, except when we're
  240. already monochromatic, where the handy exception k=n kicks in.
  241.  
  242. One can rewrite the contribution from k as
  243.  
  244.    (n-1) SUM (k-j)/(n-j)
  245.         0<j<k
  246.  
  247. which incorporates both the k(k-1)/2 and the previous sum over j.
  248. That makes the proof a little cleaner.
  249.  
  250. ==> combinatorics/full.p <==
  251. Consider a string that contains all substrings of length n.  For example,
  252. for binary strings with n=2, a shortest string is 00110 -- it contains 00,
  253. 01, 10 and 11 as substrings.  Find the shortest such strings for all n.
  254.  
  255. ==> combinatorics/full.s <==
  256. Knuth, Volume 2 Seminumerical Algorithms, section 3.2.2 discusses this problem.
  257. He cites the following results:
  258. Shortest length: m^n + n - 1, where m = number of symbols in the language.
  259. Algorithms:
  260. [Exercise 7, W. Mantel, 1897]
  261. The binary sequence is the LSB of X computed by the MIX program:
  262.     LDA X
  263.     JANZ *+2
  264.     LDA A
  265.     ADD X
  266.     JNOV *+3
  267.     JAZ *+2
  268.     XOR A
  269.     STA X
  270. [Exercise 10, M. H. Martin, 1934]
  271. Set x[1] = x[2] = ... = x[n] = 0.  Set x[i+1] = largest value < n such that
  272. substring of n digits ending at x[i+1] does not occur earlier in string.
  273. Terminate when this is not possible.
  274.  
  275. If we instead consider the strings as circular, we have a well known
  276. problem whose solution is given by any hamiltonian cycle in the de
  277. Bruijn (or Good) graph of dimension K.  (Or equivalently an eulerian
  278. circuit in the de Bruijn graph of dimension K-1) As a string of length
  279. 2^K is produced, it must be optimal, and any shortest sequence must be
  280. an eulerian circuit in a dB graph.
  281.  
  282. The de Bruijn graph Tn has as its vertex set the binary n-strings.
  283. Directed edges join n-strings that may be derived by deleting the left
  284. most digit and appending a 0 or 1 to the right end.  de Bruijn + van
  285. Ardenne-Ehrenfest (in 1951) counted the number of eulerian circuits in
  286. Tn. There are 2^(2^(n-1)-n) of them.
  287.  
  288. Some examples:
  289. K=2 1100
  290. K=3 11101000
  291. K=4 1111001011010000
  292.  
  293. The solution to the above problem (non-circular strings) can be found
  294. by duplicating the first K-1 digits of the solution string at the end
  295. of the string.  These are not the only solutions, but they
  296. are of minimum length: 2^K + K-1.  
  297.  
  298. We can obtain a lower bound for the optimal sequence for the general case as
  299. follows:
  300.  
  301. Consider first the simpler case of breaking into an answer machine which
  302. accepts d+1 digits, values 0 to n-1.  We wish to find the minimal universal
  303. code that will allow us access to any such answering machine.
  304.  
  305. Let us construct a digraph G = (V,E), where the n^d vertices are labelled
  306. with a d sequence of digits.  Notation: let [v_{i,1},v_{i,2},...,v_{i,d}]
  307. denote the labelling on node v_i.  An edge e = (v_i, v_j) is in E iff for k
  308. in 1, ..., d-1: v_{i,k+1} = v_{j,k}, i.e., the last d-1 digits in the
  309. labelling of the initial vertex of e is identical with the first d-1 digits
  310. in the labelling of the terminal vertex of e.  We associate with each edge a
  311. value, t(e) = v_{j,d}, the last digit in the labelling of the terminal
  312. vertex.
  313.  
  314. The intuition goes as follows:  we are going to perform a Euler circuit of
  315. the digraph, where the label on the current vertex gives the last d digits
  316. in the output sequence so far.  If we make a transition on edge e, we output
  317. the tone/digit t(e) as the next output value, thus preserving the invariant
  318. on the labelling.
  319.  
  320. How do we know that a Euler circuit exists?  Simple:  a connected digraph
  321. has an Euler circuit iff for all vertices v: indegree(v) = outdegree(v).
  322. This property is trivially true for this digraph.
  323.  
  324. So, in order to generate a universal code for the AM, we simply output 0^d
  325. (to satisfy the precondition for being in vertex [0,...,0]), and perform an
  326. Euler circuit starting at node [0,...,0].
  327.  
  328. Now, the total length of the universal sequence is just the number of edges
  329. traversed in the Euler circuit plus the initial precondition sequence, or
  330. n^d * n + d (number of vertices times the out-degree) or n^{d+1} + d.  That
  331. this is a minimal sequence is obvious.
  332.  
  333. Next, let us consider the machine AM' where the security code is of the form
  334. [0...n-1]^d [0...m-1], i.e., d digits ranging from 0 to n-1, followed by a
  335. terminal digit ranging from 0 to m-1, m < n.
  336.  
  337. We build a digraph G = (V, E) similar to the construction above, except for
  338. the following:  an edge e is in E iff t(e) in 0 to m-1.  This digraph is
  339. clearly non-Eulerian.  In particular, there are two classes of vertices:
  340.  
  341. (1) v is of the form [0...n-1]^{d-1} [0...m-1]  (``fat'' vertices)
  342.  
  343.     and
  344.  
  345. (2) v is of the form [0...n-1]^{d-1} [m...n-1]  (``thin'' vertices)
  346.  
  347. Observations:  there are (n^{d-1} * m) fat vertices, and (n^{d-1} * (n-m))
  348. thin vertices.  All vertices have out-degree of m.  Fat vertices have
  349. in-degrees of n, and thin vertices have in-degrees of 0.  Color all the
  350. edges blue.
  351.  
  352. The question now becomes:  can we put a bound on how many new (red) edges
  353. must we add to G in order to make a blue edge covering path possible?
  354. (Instead of thinking of edges being traversed multiple times in the blue
  355. edge covering path, we allow multiple edges between vertices and allow each
  356. edge to be traversed once.)  Note that, in this procedure, we add edges only
  357. if it is allowed (the vertex labelling constraint).  We will first obtain a
  358. lower bound on the length of a blue covering circuit, and then transform it
  359. into a bound for arbitrary blue covering paths.
  360.  
  361. Clearly, we must add at least (n-m)*(n^{d-1}*m) edges incident from the fat
  362. vertices.  [ We need (n-m) new out-going edges for each of (n^{d-1}*m)
  363. vertices to bring the out-degree up to the in-degree. ]
  364.  
  365. Let us partition our vertices into sets.  Denote the range [0..m-1] by S,
  366. the range [m..n-1] by L, and the range [0..n-1] by X.
  367.  
  368. Let S_0 = { v: v = [X^{d-1}S] }.  S_0 is just the set of fat vertices.
  369. Define in(S_0) = number of edges from vertices not in S to vertices in S.
  370. Define out(S_0) in the corresponding fashion, and let excess(S_0) =
  371. in(S_0)-out(S_0).  Clearly, excess(S_0) = n^{d-1}m(n-m) from the argument
  372. above.  Generalizing the requirement for Eulerian digraphs, we see that we
  373. must add excess(S_0) edges from S_0 if the blue edges connected to/within
  374. S_0 are to be covered by some circuit (edges may not be travered multiple
  375. times -- we add parallel edges to handle that case).  In particular, edges
  376. from S_0 will be incident on vertices of the form [X^{d-2}SX].  Furthermore,
  377. they can not be [X^{d-2}SS] since that is a subset of S_0 and adding those
  378. edges will not help excess(S_0).  [Now, these edges may be needed if we are
  379. to have a circuit, but we do not consider them since they do not help
  380. excess(S_0).] So, we are forced to add excess(S_0) edges from S_0 to S_1 = {
  381. v: v = [X^{d-2}SL] }.  Color these newly added edges red.
  382.  
  383. Let us define in(S_1), out(S_1) and excess(S_1) as above for the modified
  384. digraph, i.e., including the red excess(S_0) edges that we just added.
  385. Clearly, in(S_1) = out(S_0) = n^{d-1}m(n-m), and out(S_1) = m*|S_1| =
  386. m*n^{d-2}m(n-m), so excess(S_1) = n^{d-2}m(n-m)^2.  Consider S_0 union S_1.
  387. We must add excess(S_1) edges to S_0 union S_1 to make it possible for the
  388. digraph to be covered by a circuit, and these edges must go from {S_0 union
  389. S_1} to S_2 = { v: v = [X^{d-3}SL^2] } by a similar argument as before.
  390. Repeating this partitioning process, eventually we get to S_{d-1} = { v: v =
  391. [SL^{d-1}] }, where union of S_0 to S_{d-1} will need edges to S_d = { v: v
  392. = [L^d] }, where this process terminates.  Note that at this time,
  393. excess(union of S_0 to S_{d-1}) = m(n-m)^d, but in(S_d) = 0 and out(S_d) =
  394. m(n-m)^d, and the process terminates.
  395.  
  396. What have we shown?  Adding up blue edges and the red edges gives us a lower
  397. bound on the total number of edges in a blue-edges covering circuit (not
  398. necessarily Eulerian) in the complete digraph.  This comes out to be
  399. n^{d+1}-(n-m)^{d+1} edges.
  400.  
  401. Next, we note that if we had an optimal path covering all the blue edges, we
  402. can transform it into a circuit by adding d edges.  So, a minimal path can
  403. be no more than d edges shorter than the minimal circuit covering all blue
  404. edges.  [Otherwise, we add d extra edges to make it into a shorter circuit.]
  405.  
  406. So the shortest blue covering path through the digraph is at least
  407. n^{d+1}-{n-m}^{d+1}-d.  With an initial pre-condition sequence of length d
  408. (to establish the transition invariant), the shortest universal answering
  409. machine sequence is of length at least n^{d+1}-(n-m)^{d+1}.
  410.  
  411. While this has not been that constructive, it is easy to see that we can
  412. achieve this bound.  If we looked at the vertices in each of the S_i's, we
  413. just add exactly the edges to S_{i+1} and no more.  The resultant digraph
  414. would be Eulerian, and to find the minimal path we need only start at the
  415. vertex labelled [{n-1}^d], find the Euler circuit, and omit the last d edges
  416. from the tour.
  417.  
  418. ==> combinatorics/gossip.p <==
  419. n people each know a different piece of gossip.  They can telephone each other
  420. and exchange all the information they know (so that after the call they both
  421. know anything that either of them knew before the call).  What is the smallest
  422. number of calls needed so that everyone knows everything?
  423.  
  424. ==> combinatorics/gossip.s <==
  425. 1 for n=2
  426. 3 for n=3
  427. 2n-4 for n>=4
  428.  
  429. This can be achieved as follows: choose four people (A, B, C, and D) as
  430. the "core group".  Each person outside the core group phones a member of
  431. the core group (it doesn't matter which); this takes n-4 calls.  Now the
  432. core group makes 4 calls: A-B, C-D, A-C, and B-D.  At this point, each
  433. member of the core group knows everything.  Now, each person outside the
  434. core group calls anybody who knows everything; this again requires n-4
  435. calls, for a total of 2n-4.
  436.  
  437. The solution to the "gossip problem" has been published several times:
  438.  
  439.     1.  R. Tidjeman, "On a telephone problem", Nieuw Arch. Wisk. 3
  440.         (1971), 188-192.
  441.  
  442.     2.  B. Baker and R. Shostak, "Gossips and telephones", Discrete
  443.         Math. 2 (1972), 191-193.
  444.  
  445.     3.  A. Hajnal, E. C. Milner, and E. Szemeredi, "A cure for the
  446.         telephone disease", Canad Math. Bull 15 (1976), 447-450.
  447.  
  448.     4. Kleitman and Shearer, Disc. Math. 30 (1980), 151-156.
  449.  
  450.     5.  R. T. Bumby, "A problem with telephones", Siam J. Disc. Meth. 2
  451.         (1981), 13-18.
  452.  
  453. ==> combinatorics/grid.dissection.p <==
  454. How many (possibly overlapping) squares are in an mxn grid?  Assume that all
  455. the squares have their edges parallel to the edges of the grid.
  456.  
  457. ==> combinatorics/grid.dissection.s <==
  458. Given an n*m grid with n > m.
  459.  
  460. Orient the grid so n is its width.  Divide the grid into two portions, 
  461. an m*m square on the left and an (n-m)*m rectangle on the right.
  462. Count the squares that have their upper right-hand corners in the
  463. m*m square.  There are m^2 of size 1*1, (m-1)^2 of size 2*2, ...
  464. up to 1^2 of size m*m.  Now look at the n-m columns of lattice points
  465. in the rectangle on the right, in which we find upper right-hand
  466. corners of squares not yet counted.  For each column we count m new
  467. 1*1 squares, m-1 new 2*2 squares, ... up to 1 new m*m square.
  468.  
  469. Combining all these counts in summations:
  470.  
  471.      m           m
  472. total = sum i^2 + (n - m) sum i
  473.     i=1          i=1
  474.  
  475.     (2m + 1)(m + 1)m   (n - m)(m + 1)m
  476.       = ---------------- + ---------------
  477.         6          2
  478.  
  479.       = (3n - m + 1)(m + 1)m/6
  480.  
  481. -- David Karr
  482.  
  483. ==> combinatorics/permutation.p <==
  484. Compute the nth permutation of k numbers (or objects).
  485.  
  486. ==> combinatorics/permutation.s <==
  487. #include <stdio.h>
  488.  
  489. /*
  490. adapted from 'Notation as a Tool of Thought', by K.E.Iverson
  491.  
  492. Radix Representation of Permutations
  493. */
  494.  
  495. /* direct from radix; of given order */
  496. dfr(short direct[],short radix[],long order)
  497. {
  498.     if (order)
  499.     {
  500.         direct[0] = radix[0];
  501.         dfr (direct+1, radix+1, order-1);
  502.         while (--order)
  503.             direct[order] += direct[order] >= direct[0];
  504.     }
  505. }
  506.  
  507. /* radix representation; of given order and given index */
  508. rr(short radix[], long order, long index)
  509. {
  510.     int i;
  511.     
  512.     for (i=1; i<=order; i++)
  513.     {
  514.         radix[order-i] = index % i;
  515.         index = index/i;
  516.     }
  517. }
  518.  
  519. show(short perm[],long order)
  520. {
  521.     while(order--)
  522.         printf("%hd ",*perm++);
  523.     printf("\n");
  524. }
  525.  
  526.  
  527. short parity(short radix[],long order)
  528. {
  529.     long p=0;
  530.     
  531.     while(order--)
  532.         p+=*radix++;
  533.     return p%2;
  534. }
  535.  
  536. void usage(char *name)
  537. {
  538.     fprintf(stderr,"usage: %s order number_of_permutation\n",name);
  539.     exit(-1);
  540. }
  541.  
  542. main(int argc, char *argv[])
  543. {
  544. #define MAX_ORDER    512
  545.     short radix[MAX_ORDER], direct[MAX_ORDER];
  546.     long order, nth;
  547.     
  548.     if (argc!=3) usage(argv[0]);
  549.     order = atol(argv[1]);
  550.     nth = atol(argv[2]);
  551.     rr(radix, order, nth-1);    /* where 0 is the first permuatation */
  552.     dfr(direct, radix, order);
  553.     
  554.     printf("radix  "); show(radix,order);
  555.     printf("direct "); show(direct,order);
  556.     printf("parity %d\n",parity(radix,order));
  557. }
  558.  
  559.  
  560. -- 
  561. J. Henri Schueler, H&h Software, Toronto    +1 416 698 9075 
  562. jhs@ipsa.reuter.com
  563.  
  564. ==> combinatorics/subsets.p <==
  565. Out of the set of integers 1,...,100 you are given ten different
  566. integers.  From this set, A, of ten integers you can always find two
  567. disjoint non-empty subsets, S & T, such that the sum of elements in S
  568. equals the sum of elements in T.  Note: S union T need not be all ten
  569. elements of A.  Prove this.
  570.  
  571. ==> combinatorics/subsets.s <==
  572. There are 2^10 = 1,024 subsets of the 10 integers, but there can be only 901
  573. possible sums, the number of integers between the minimum and maximum sums.
  574. With more subsets than possible sums, there must exist at least one sum that
  575. corresponds to at least two subsets.  Call two subsets with equal sums A and B.
  576. Let C = A intersect B; define S = A - C, T = B - C.  Then S is disjoint from T,
  577. and sum(S) = sum(A-C) = sum(A) - sum(C) = sum(B) - sum(C) = sum(B-C) = sum(T).
  578. QED
  579.  
  580. Addendum: 9 integers suffice.  This was part of my Westinghouse project
  581. in 1981 (the above problem was in Martin Gardner's Scientific American
  582. column not long before).  The argument is along the same lines, but
  583. a bit more complicated; for starters you only work with the subsets
  584. consisting of 3, 4, 5, or 6 of the 9 elements.
  585.  
  586. Let M(n) be the smallest integer such that there exists an n-element
  587. set {a1,a2,a3,...,an=M(n)} of positive integers all 2^n of whose
  588. subsums are distinct.  The pigeonhole argument of subsets.s shows that
  589. M(n)>2^n/n, and it is known that M(n)>c*2^n/sqrt(n) for some c>0.
  590. It is still an unsolved problem (with an Erdos bounty) whether
  591. there is a positive constant c such that M(n)>c*2^n for all n.
  592.  
  593. --Noam D. Elkies (elkies@zariski.harvard.edu)
  594.   Dept. of Mathematics, Harvard University
  595.  
  596. ==> combinatorics/transitions.p <==
  597. How many n-bit binary strings (0/1) have exactly k transitions
  598. (an adjacent pair of dissimilar bits, i.e., a 01 or a 10)?
  599.  
  600. ==> combinatorics/transitions.s <==
  601. A transition can occur at an adjacent pair (i,i+1) where 1<=i<i+1<=n.
  602. Since there are k transitions, there are C(n-1,k) total number of ways
  603. that transitions can occur.  But the string may start with a 1 or a 0
  604. (after which its transitions uniquely determine the string).  So there
  605. are a total of 2C(n-1,k) such strings.
  606.  
  607.