Burt Rosenberg

Answer Set 5 ^{Out: October, not distrib., 1994}

program partitions(input,output) ;
(* Partition program, solution to HW5 EC.
   Burt Rosenberg (c) 1994
   Univ. of Miami.
*)
const NPARTS = 50 ;
var
     parts : array[0..NPARTS] of integer ;
procedure partition( n : integer ; k : integer ) ;
(* list all partitions on n w/ largest element k or smaller.
   Recursion equation:
      either partition has a part size k or not.
      In the first case, place a k in next avail. parts cell and
      recurse on n-k. Note that k<=n in this case.
      In the second case, recurse on P(n,k-1)
   Terminal conditions:
      n=0: print out partition,
      k=1: do not recurse on P(n,0).
*)
var
   i : integer ;
begin
   (* ASSERT parts[0] is the number of occupied cells in parts *)
   if (n=0) then begin
      for i:=1 to (parts[0]-1) do write(parts[i]:0,' + ') ;
      writeln( parts[parts[0]]:0)
   end else begin
      if (k<=n) then begin
         parts[0] := parts[0] + 1 ;
         parts[parts[0]] := k ;
         partition( n-k, k ) ;
         parts[0] := parts[0] - 1
      end ;
      if (k>1) then partition( n, k-1 ) ;
   end ;
end ;

var i : integer ;
begin
   parts[0] := 0 ;
   write('?> ') ;
   readln( i ) ;
   partition(i,i) ;
end .