Skip to content

Commit 55725ed

Browse files
authored
Reviving #84
> The documentation for stirlings1 and stirlings2 indicates that they can use BigInts if provided with such as argument. However the existing functions specify n and k arguments to be Int. This patch allows support for BigInt integers without breaking type consistency of return types, and also provides for some memoization, since otherwise the functions, since they are recursive, can be slow when handling very large integers. (credit https://github.com/wherrera10)
1 parent d1b633b commit 55725ed

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/numbers.jl

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ end
6868
"""
6969
narayana(n,k)
7070
71-
Compute the Narayana number `N(n,k)` given by ``\\frac{1}{n}\\binom{n}{k}\\binom{n}{k-1}``
71+
Compute the Narayana number `N(n,k)`` given by ``\\frac{1}{n}\\binom{n}{k}\\binom{n}{k-1}``
7272
Wikipedia : https://en.wikipedia.org/wiki/Narayana_number
7373
"""
7474
function narayana(bn::Integer,bk::Integer)
@@ -125,19 +125,23 @@ function lucasnum(n::Integer)
125125
return z[]
126126
end
127127

128-
function stirlings1(n::Int, k::Int, signed::Bool=false)
128+
stirlings1cache = Dict()
129+
130+
function stirlings1(n::Integer, k::Integer, signed::Bool=false)
129131
if signed == true
130132
return (-1)^(n - k) * stirlings1(n, k)
131133
end
132134

133-
if n < 0
135+
if haskey(stirlings1cache, Pair(n, k))
136+
return stirlings1cache[Pair(n, k)]
137+
elseif n < 0
134138
throw(DomainError(n, "n must be nonnegative"))
135139
elseif n == k == 0
136-
return 1
140+
return one(n)
137141
elseif n == 0 || k == 0
138-
return 0
142+
return zero(n)
139143
elseif n == k
140-
return 1
144+
return one(n)
141145
elseif k == 1
142146
return factorial(n-1)
143147
elseif k == n - 1
@@ -148,21 +152,29 @@ function stirlings1(n::Int, k::Int, signed::Bool=false)
148152
return binomial(n, 2) * binomial(n, 4)
149153
end
150154

151-
return (n - 1) * stirlings1(n - 1, k) + stirlings1(n - 1, k - 1)
155+
ret = (n - 1) * stirlings1(n - 1, k) + stirlings1(n - 1, k - 1)
156+
stirlings1cache[Pair(n, k)] = ret
157+
return ret
152158
end
153159

154-
function stirlings2(n::Int, k::Int)
160+
stirlings2cache = Dict()
161+
162+
function stirlings2(n::Integer, k::Integer)
155163
if n < 0
156164
throw(DomainError(n, "n must be nonnegative"))
165+
elseif haskey(stirlings2cache, Pair(n, k))
166+
return stirlings2cache[Pair(n, k)]
157167
elseif n == k == 0
158-
return 1
168+
return one(n)
159169
elseif n == 0 || k == 0
160-
return 0
170+
return zero(n)
161171
elseif k == n - 1
162172
return binomial(n, 2)
163173
elseif k == 2
164174
return 2^(n-1) - 1
165175
end
166176

167-
return k * stirlings2(n - 1, k) + stirlings2(n - 1, k - 1)
177+
ret = k * stirlings2(n - 1, k) + stirlings2(n - 1, k - 1)
178+
stirlings2cache[Pair(n, k)] = ret
179+
ret
168180
end

0 commit comments

Comments
 (0)