Skip to content

Commit fdd6416

Browse files
authored
Merge pull request #16 from ymer/master
New implementation for stirlings
2 parents 1e2479b + 1ad1aae commit fdd6416

File tree

3 files changed

+79
-9
lines changed

3 files changed

+79
-9
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ This library provides the following functions:
2525
- `multifactorial(n)`: returns the m-multifactorial n(!^m); always returns a `BigInt`;
2626
- `multinomial(k...)`: receives a tuple of `k_1, ..., k_n` and calculates the multinomial coefficient `(n k)`, where `n = sum(k)`; returns a `BigInt` only if given a `BigInt`;
2727
- `primorial(n)`: returns the product of all positive prime numbers <= n; always returns a `BigInt`;
28-
- `stirlings1(n, k)`: the signed `(n,k)`-th Stirling number of the first kind; returns a `BigInt` only if given a `BigInt`.
28+
- `stirlings1(n, k, signed=false)`: returns the `(n,k)`-th Stirling number of the first kind; the number is signed if `signed` is true; returns a `BigInt` only if given a `BigInt`.
29+
- `stirlings2(n, k)`: returns the `(n,k)`-th Stirling number of the second kind; returns a `BigInt` only if given a `BigInt`.
2930

3031
Young diagrams
3132
--------------

src/numbers.jl

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ export bellnum,
77
lassallenum,
88
legendresymbol,
99
lucasnum,
10-
stirlings1
10+
stirlings1,
11+
stirlings2
1112

13+
import Base: factorial, binomial
14+
1215
"Returns the n-th Bell number"
1316
function bellnum(bn::Integer)
1417
if bn < 0
@@ -85,9 +88,44 @@ function lucasnum(n::Integer)
8588
return z
8689
end
8790

88-
"Returns s(n, k), the signed Stirling number of first kind"
89-
function stirlings1(n::Integer, k::Integer)
90-
p = poly(0:(n-1))
91-
p[n - k + 1]
91+
function stirlings1(n::Int, k::Int, signed::Bool=false)
92+
if signed == true
93+
return (-1)^(n - k) * stirlings1(n, k)
94+
end
95+
96+
if n < 0
97+
throw(DomainError())
98+
elseif n == k == 0
99+
return 1
100+
elseif n == 0 || k == 0
101+
return 0
102+
elseif n == k
103+
return 1
104+
elseif k == 1
105+
return factorial(n-1)
106+
elseif k == n - 1
107+
return binomial(n, 2)
108+
elseif k == n - 2
109+
return div((3 * n - 1) * binomial(n, 3), 4)
110+
elseif k == n - 3
111+
return binomial(n, 2) * binomial(n, 4)
112+
end
113+
114+
return (n - 1) * stirlings1(n - 1, k) + stirlings1(n - 1, k - 1)
92115
end
93116

117+
function stirlings2(n::Int, k::Int)
118+
if n < 0
119+
throw(DomainError())
120+
elseif n == k == 0
121+
return 1
122+
elseif n == 0 || k == 0
123+
return 0
124+
elseif k == n - 1
125+
return binomial(n, 2)
126+
elseif k == 2
127+
return 2^(n-1) - 1
128+
end
129+
130+
return k * stirlings2(n - 1, k) + stirlings2(n - 1, k - 1)
131+
end

test/numbers.jl

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,42 @@ using Base.Test
2323
@test_throws DomainError lucasnum(-1)
2424

2525
# stirlings1
26-
@test sum([abs(stirlings1(8, i)) for i = 0:8]) == factorial(8)
26+
@test_throws DomainError stirlings1(-1, 1)
27+
@test typeof(stirlings1(6, 2)) == Int
28+
@test stirlings1(0, 0) == 1
29+
@test stirlings1(1, 1) == 1
30+
@test stirlings1(2, 6) == 0
31+
@test stirlings1(6, 0) == 0
32+
@test stirlings1(6, 0, true) == 0
33+
@test stirlings1(6, 1) == 120
34+
@test stirlings1(6, 1, true) == -120
35+
@test stirlings1(6, 2) == 274
36+
@test stirlings1(6, 2, true) == 274
37+
@test stirlings1(6, 3) == 225
38+
@test stirlings1(6, 3, true) == -225
39+
@test stirlings1(6, 4) == 85
40+
@test stirlings1(6, 4, true) == 85
41+
@test stirlings1(6, 5) == 15
42+
@test stirlings1(6, 5, true) == -15
43+
@test stirlings1(6, 6) == 1
44+
@test stirlings1(6, 6, true) == 1
45+
@test sum([abs(stirlings1(8, i, true)) for i = 0:8]) == factorial(8)
46+
47+
# stirlings2
48+
@test_throws DomainError stirlings2(-1, 1)
49+
@test typeof(stirlings2(6, 2)) == Int
50+
@test stirlings2(0, 0) == 1
51+
@test stirlings2(1, 1) == 1
52+
@test stirlings2(2, 6) == 0
53+
@test stirlings2(6, 0) == 0
54+
@test stirlings2(6, 1) == 1
55+
@test stirlings2(6, 2) == 31
56+
@test stirlings2(6, 3) == 90
57+
@test stirlings2(6, 4) == 65
58+
@test stirlings2(6, 5) == 15
59+
@test stirlings2(6, 6) == 1
2760

2861
# bell
2962
@test bellnum(7) == 877
3063
@test bellnum(42) == parse(BigInt,"35742549198872617291353508656626642567")
3164
@test_throws DomainError bellnum(-1)
32-
33-

0 commit comments

Comments
 (0)