Skip to content

Commit dc6a622

Browse files
Merge branch 'master' into feature/fix-failed-tests-for-Pharo9
2 parents 220b919 + ddd56f8 commit dc6a622

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Metacello new
3838
load
3939
```
4040

41-
We have **816** green tests ! At the moment, all the development happens in the master branch (we are using [trunk-based development](https://trunkbaseddevelopment.com/)).
41+
We have **872** green tests ! At the moment, all the development happens in the master branch (we are using [trunk-based development](https://trunkbaseddevelopment.com/)).
4242

4343
PolyMath is a Pharo project, similar to existing scientific libraries like NumPy, SciPy for Python or SciRuby for Ruby. PolyMath already provides the following basic functionalities:
4444
- complex and quaternions extensions,

src/Math-Numerical/Integer.extension.st

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,37 @@ Integer >> gamma [
77
^ (self - 1) factorial.
88
]
99

10+
{ #category : #'*Math-Numerical' }
11+
Integer >> inverseBinomialCoefficient [
12+
13+
" Reverse binomial coefficient. Answer a <Collection> with all n and k such that n take: k = self. Elements in the answered Collection should be read as paired. Each pair represents (n,k) in the binomial coefficient formula.
14+
See https://math.stackexchange.com/a/103385/205 for details. "
15+
16+
| k |
17+
[ self > 1 ] assert.
18+
k := 0.
19+
^ Array streamContents: [ :stream |
20+
[ true ] whileTrue: [
21+
| nmin nmax choose |
22+
k := k + 1.
23+
2 * k + 1 * self <= (4 ** k) ifTrue: [ ^ stream contents ].
24+
nmin := ((k factorial * self) nthRoot: k) ceiling.
25+
nmax := nmin + k + 1.
26+
nmin := nmin max: 2 * k.
27+
choose := nmin asInteger numberOfCombinationsTaken: k.
28+
nmin to: nmax do: [ :n |
29+
choose = self ifTrue: [
30+
stream nextPutAll: {
31+
n asInteger.
32+
k asInteger }.
33+
k < (n - k) ifTrue: [
34+
stream nextPutAll: {
35+
n asInteger.
36+
(n - k) asInteger } ] ].
37+
choose := choose * (n + 1).
38+
choose := (choose / (n + 1 - k)) ceiling ] ] ]
39+
]
40+
1041
{ #category : #'*Math-Numerical' }
1142
Integer >> random [
1243
"Answer a random integer between 0 and the receiver."
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Extension { #name : #IntegerTest }
2+
3+
{ #category : #'*Math-Tests-Numerical' }
4+
IntegerTest >> testFindNK [
5+
6+
self assert: 6 inverseBinomialCoefficient equals: #(6 1 6 5 4 2).
7+
self assert: 10 inverseBinomialCoefficient equals: #(10 1 10 9 5 2 5 3).
8+
self assert: 20 inverseBinomialCoefficient equals: #(20 1 20 19 6 3).
9+
self assert: 21 inverseBinomialCoefficient equals: #(21 1 21 20 7 2 7 5).
10+
self assert: 55 inverseBinomialCoefficient equals: #(55 1 55 54 11 2 11 9).
11+
self assert: 120 inverseBinomialCoefficient equals: #(120 1 120 119 16 2 16 14 10 3 10 7).
12+
self assert: 3003 inverseBinomialCoefficient equals: #(3003 1 3003 3002 78 2 78 76 15 5 15 10 14 6 14 8).
13+
self assert: 8966473191018617158916954970192684 inverseBinomialCoefficient equals: #(8966473191018617158916954970192684 1 8966473191018617158916954970192684 8966473191018617158916954970192683 123 45 123 78).
14+
15+
self should: [ 1 inverseBinomialCoefficient ] raise: Error.
16+
self should: [ 0 inverseBinomialCoefficient ] raise: Error.
17+
]

0 commit comments

Comments
 (0)