home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / sci / math / 9535 < prev    next >
Encoding:
Internet Message Format  |  1992-07-25  |  2.0 KB

  1. Path: sparky!uunet!wupost!zaphod.mps.ohio-state.edu!malgudi.oar.net!uoft02.utoledo.edu!desire.wright.edu!jmatthews
  2. Newsgroups: sci.math
  3. Subject: Re: Factorial program...
  4. Message-ID: <1992Jul24.162820.3080@desire.wright.edu>
  5. From: jmatthews@desire.wright.edu
  6. Date: 24 Jul 92 16:28:20 EST
  7. References: <1992Jul20.160312.10723@infonode.ingr.com> <1992Jul22.222658.3613@sjsumcs.sjsu.edu>
  8. Organization: Wright State University 
  9. Lines: 45
  10.  
  11. In article <1992Jul22.222658.3613@sjsumcs.sjsu.edu>, kellum@sjsumcs.sjsu.edu (Ken Kellum) writes:
  12. > In article <1992Jul20.160312.10723@infonode.ingr.com> "Vilva Natarajan <vilva@madras.b23b.ingr.COM>" writes:
  13. >>Hi,
  14. >>
  15. >>    I am looking for source code that prints out all the factorial
  16. >>    combinations of a set of numbers.
  17. >> [exampple deleted]
  18.  
  19. > The following (pseudo) Pascal procedure prints all the permutations
  20. > of a one dimensional array.  It can be modified to do what you want.
  21. > Procedure Permute (var A : array [1..n] of whatever)
  22. >    Procedure Inner (i : integer);
  23. >   { prints the permutations of a[i] .. a[n] }
  24. >    var j : integer
  25. >        temp : whatever;
  26. >    begin
  27. >       if i > n then
  28. >          writeln(a)
  29. >       else 
  30. >          for j := i to n do begin
  31. >             temp := a[i]; a[i] := a[j]; a[j] := temp;
  32. >             Inner (i + 1);
  33. >             temp := a[i]; a[i] := a[j]; a[j] := temp;
  34. >          end;
  35. >     end; { Inner }
  36. > begin {permute}
  37. >    Inner(1);
  38. > end;
  39.  
  40. The first time I saw this algorith was in Wirth's _Algorithms_+_Data_
  41. _Structures_=_Programs_. The question was posed as an exercise (3.2)
  42. in a chapter on recursion. The _answer_ appeared in the following
  43. chapter as the (otherwise meaningless) sample output of a cross-
  44. reference generating program.
  45.  
  46. An Easter egg in a textbook:-)
  47.  
  48. o----------------------------------------------------------------------------o
  49. | John B. Matthews, jmatthews@desire.wright.edu, disclaimer:= myViews <> WSU |
  50. | "I'm a commensal .sig virus, indistinguishable from an ordinary organelle."|
  51. o----------------------------------------------------------------------------o
  52.