@@ -2,31 +2,33 @@ Extension { #name : #Integer }
2
2
3
3
{ #category : #' *AI-HierarchicalClustering' }
4
4
Integer >> findNK [
5
+
5
6
" Yields all n and k such that n take: k = self.
6
7
https://math.stackexchange.com/a/103385/205 "
7
8
8
9
| k |
9
10
[ self > 1 ] assert.
10
- k := 0 .
11
- ^ Array streamContents: [ : stream |
12
- [ true ] whileTrue: [
13
- | nmin nmax choose |
14
- k := k + 1 .
15
- ((2 * k + 1 ) * self ) <= (4 ** k)
16
- ifTrue: [ ^ stream contents ].
17
- nmin := self firstOver: k with: (k factorial * self ).
18
- nmax := nmin + k + 1 .
19
- nmin := nmin max: (2 * k).
20
- choose := nmin asInteger take: k.
21
- nmin to: nmax do: [ : n |
22
- choose = self
23
- ifTrue: [
24
- stream nextPutAll: { n asInteger . k asInteger }.
25
- k < (n - k)
26
- ifTrue: [ stream nextPutAll: { n asInteger . (n - k) asInteger } ] ].
27
- choose := choose * (n + 1 ).
28
- choose := (choose / (n + 1 - k)) ceiling ] ] ]
29
-
11
+ k := 0 .
12
+ ^ Array streamContents: [ :stream |
13
+ [ true ] whileTrue: [
14
+ | nmin nmax choose |
15
+ k := k + 1 .
16
+ 2 * k + 1 * self <= (4 ** k) ifTrue: [ ^ stream contents ].
17
+ nmin := self firstOver: k with: k factorial * self .
18
+ nmax := nmin + k + 1 .
19
+ nmin := nmin max: 2 * k.
20
+ choose := nmin asInteger numberOfCombinationsTaken: k.
21
+ nmin to: nmax do: [ :n |
22
+ choose = self ifTrue: [
23
+ stream nextPutAll: {
24
+ n asInteger.
25
+ k asInteger }.
26
+ k < (n - k) ifTrue: [
27
+ stream nextPutAll: {
28
+ n asInteger.
29
+ (n - k) asInteger } ] ].
30
+ choose := choose * (n + 1 ).
31
+ choose := (choose / (n + 1 - k)) ceiling ] ] ]
30
32
]
31
33
32
34
{ #category : #' *AI-HierarchicalClustering' }
0 commit comments