home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / sci / math / 9431 < prev    next >
Encoding:
Text File  |  1992-07-22  |  2.0 KB  |  88 lines

  1. Newsgroups: sci.math
  2. Path: sparky!uunet!decwrl!csus.edu!netcomsv!sjsumcs!kellum
  3. From: kellum@sjsumcs.sjsu.edu (Ken Kellum)
  4. Subject: Re: Factorial program...
  5. Message-ID: <1992Jul22.222658.3613@sjsumcs.sjsu.edu>
  6. Organization: San Jose State University - Math/CS Dept.
  7. References: <1992Jul20.160312.10723@infonode.ingr.com>
  8. Date: Wed, 22 Jul 1992 22:26:58 GMT
  9. Lines: 77
  10.  
  11. In article <1992Jul20.160312.10723@infonode.ingr.com> "Vilva Natarajan <vilva@madras.b23b.ingr.COM>" writes:
  12. >Hi,
  13. >
  14. >    I am looking for source code that prints out all the factorial
  15. >    combinations of a set of numbers.
  16. >
  17. >        Eg.
  18. >        factorial combo of 1 2 3 4 would be, (there are 24 of them)
  19. >        1 2 3 4
  20. >        1 2 4 3
  21. >        1 3 2 4
  22. >        1 3 4 2
  23. >        1 4 2 3 
  24. >        1 4 3 2
  25. >        2 1 3 4
  26. >        2 1 4 3
  27. >        2 3 1 4 
  28. >        2 3 4 1
  29. >        2 4 1 3 
  30. >        2 4 3 1
  31. >        
  32. >        3 1 2 4
  33. >        3 1 4 2
  34. >        3 2 1 4 
  35. >        3 2 4 1
  36. >        3 4 1 2
  37. >        3 4 2 1
  38. >        4 1 2 3
  39. >        4 1 3 2
  40. >        4 2 1 3
  41. >        4 2 3 1
  42. >        4 3 1 2
  43. >        4 3 2 1
  44. >
  45. >    Any help would be appreciated. Thanks>
  46. >
  47. >vn
  48.  
  49. The following (pseudo) Pascal procedure prints all the permutations
  50. of a one dimensional array.  It can be modified to do what you want.
  51.  
  52. Procedure Permute (var A : array [1..n] of whatever)
  53.  
  54.    Procedure Inner (i : integer);
  55.   { prints the permutations of a[i] .. a[n] }
  56.    var j : integer
  57.        temp : whatever;
  58.  
  59.    begin
  60.       if i > n then
  61.          writeln(a)
  62.       else 
  63.          for j := i to n do begin
  64.             temp := a[i]; a[i] := a[j]; a[j] := temp;
  65.             Inner (i + 1);
  66.             temp := a[i]; a[i] := a[j]; a[j] := temp;
  67.          end;
  68.     end; { Inner }
  69.  
  70. begin {permute}
  71.    Inner(1);
  72. end;
  73.  
  74. It's an amusing exercise to construct an inductive proof that this
  75. procedure works.
  76.  
  77. Kenneth Kellum
  78. ___________________________________________
  79.  
  80. Those who give up claims of uniqueness only become more unbearable
  81. when they claim to be representative.
  82.