summaryrefslogtreecommitdiff
path: root/ipl/procs/partit.icn
blob: 1432d8e859b60a4edd3816d813abf143f98cd3ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
###########################################################################
#
#	File:     partit.icn
#
#	Subject:  Procedures to partition integer
#
#	Author:   Ralph E. Griswold
#
#	Date:     December 5, 1995
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  partit(i, min, max) generates, as lists, the partitions of i; that is the
#  ways that i can be represented as a sum of positive integers with
#  minimum and maximum values.
#
#  partcount(i, min, max) returns just the number of partitions.
#
#  fibpart(i) returns a list of Fibonacci numbers that is a partition of i.
#
############################################################################
#
#  Links:  fastfncs, numbers
#
############################################################################

link fastfncs
link numbers

procedure partit(i, min, max, k)
   local j

   if not(integer(i)) | (i < 0) | (\min > \max) then
      stop("*** illegal argument to partit(i)")

   /min := 1
   /max := i
   max >:= i
   /k := i
   k >:= max
   k >:= i

   if i = 0 then return []

   every j := k to min by -1 do {
      suspend push(partit(i - j, min, max, j), j)
      }

end

procedure partcount(i, min, max)
   local count

   count := 0

   every partitret(i, min, max) do
      count +:= 1

   return count

end

#  This is a version of partit() that doesn't do all the work
#  of producing the partitions and is used only by partcount().

procedure partitret(i, min, max, k)
   local j

   /min := 1
   /max := i
   max >:= i
   /k := i
   k >:= max
   k >:= i
 
   if i = 0 then return

   every j := k to min by -1 do {
      suspend partitret(i - j, min, max, j)
      }

end

#  Partition of an integer into Fibonacci numbers.

procedure fibpart(i)
   local partl, n
   static m

   initial    m := 1 / log(( 1 + sqrt(5)) / 2)

   partl := []

   while i > 2 do {
      push(partl, n := fib(ceil(log(i) * m)))
      i -:= n
      }

   if i > 0 then push(partl, i)

   return partl

end