You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,18 +10,18 @@ combinatorics and permutations. As overflows are expected even for low values,
10
10
most of the functions always return `BigInt`, and are marked as such below.
11
11
12
12
This library provides the following functions:
13
-
-`bell(n)`: returns the n-th Bell number; always returns a `BigInt`;
14
-
-`catalan(n)`: returns the n-th Catalan number; always returns a `BigInt`;
15
-
-`combinations(a)`: returns combinations of all order by chaining calls to `Base.combinations(a,n);
13
+
-`bellnum(n)`: returns the n-th Bell number; always returns a `BigInt`;
14
+
-`catalannum(n)`: returns the n-th Catalan number; always returns a `BigInt`;
15
+
-`combinations(a)`: returns combinations of all order by chaining calls to `Base.combinations(a,n);
16
16
-`derangement(n)`/`subfactorial(n)`: returns the number of permutations of n with no fixed points; always returns a `BigInt`;
17
17
-`doublefactorial(n)`: returns the double factorial n!!; always returns a `BigInt`;
18
18
-`fibonacci(n)`: the n-th Fibonacci number; always returns a `BigInt`;
19
19
-`hyperfactorial(n)`: the n-th hyperfactorial, i.e. prod([i^i for i = 2:n]; always returns a `BigInt`;
20
20
-`integer_partitions(n)`: returns a `Vector{Int}` consisting of the partitions of the number `n`.
21
21
-`jacobisymbol(a,b)`: returns the Jacobi symbol (a/b);
22
-
-`lassalle(n)`: returns the nth Lassalle number A<sub>n</sub> defined in [arXiv:1009.4225](http://arxiv.org/abs/1009.4225) ([OEIS A180874](http://oeis.org/A180874)); always returns a `BigInt`;
22
+
-`lassallenum(n)`: returns the nth Lassalle number A<sub>n</sub> defined in [arXiv:1009.4225](http://arxiv.org/abs/1009.4225) ([OEIS A180874](http://oeis.org/A180874)); always returns a `BigInt`;
23
23
-`legendresymbol(a,p)`: returns the Legendre symbol (a/p);
24
-
-`lucas(n)`: the n-th Lucas number; always returns a `BigInt`;
24
+
-`lucasnum(n)`: the n-th Lucas number; always returns a `BigInt`;
25
25
-`multifactorial(n)`: returns the m-multifactorial n(!^m); always returns a `BigInt`;
26
26
-`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`;
27
27
-`primorial(n)`: returns the product of all positive prime numbers <= n; always returns a `BigInt`;
"Generate all combinations of `n` elements from an indexable object. Because the number of combinations can be very large, this function returns an iterator object. Use `collect(combinations(array,n))` to get an array of all combinations.
37
+
"
38
38
functioncombinations(a, t::Integer)
39
39
if t <0
40
40
# generate 0 combinations for negative argument
@@ -43,7 +43,10 @@ function combinations(a, t::Integer)
43
43
Combinations(a, t)
44
44
end
45
45
46
-
#generate combinations of all orders, chaining of order iterators is eager,
47
-
#but sequence at each order is lazy
46
+
47
+
"""
48
+
generate combinations of all orders, chaining of order iterators is eager,
49
+
but sequence at each order is lazy
50
+
"""
48
51
combinations(a) =chain([combinations(a,k) for k=1:length(a)]...)
Generate all integer arrays that sum to `n`. Because the number of partitions can be very large, this function returns an iterator object. Use `collect(partitions(n))` to get an array of all partitions. The number of partitions to generate can be efficiently computed using `length(partitions(n))`.
26
+
"""
27
+
partitions(n::Integer) =IntegerPartitions(n)
28
+
29
+
30
+
20
31
functionnextpartition(n, as)
21
32
ifisempty(as); return Int[n]; end
22
33
@@ -77,6 +88,9 @@ end
77
88
78
89
length(f::FixedPartitions) =npartitions(f.n,f.m)
79
90
91
+
"""
92
+
Generate all arrays of `m` integers that sum to `n`. Because the number of partitions can be very large, this function returns an iterator object. Use `collect(partitions(n,m))` to get an array of all partitions. The number of partitions to generate can be efficiently computed using `length(partitions(n,m))`.
93
+
"""
80
94
partitions(n::Integer, m::Integer) = n >=1&& m >=1?FixedPartitions(n,m) :throw(DomainError())
Generate all set partitions of the elements of an array, represented as arrays of arrays. Because the number of partitions can be very large, this function returns an iterator object. Use `collect(partitions(array))` to get an array of all partitions. The number of partitions to generate can be efficiently computed using `length(partitions(array))`.
158
+
"""
142
159
partitions(s::AbstractVector) =SetPartitions(s)
143
160
144
161
start(p::SetPartitions) = (n =length(p.s); (zeros(Int32, n), ones(Int32, n-1), n, 1))
Generate all set partitions of the elements of an array into exactly m subsets, represented as arrays of arrays. Because the number of partitions can be very large, this function returns an iterator object. Use `collect(partitions(array,m))` to get an array of all partitions. The number of partitions into m subsets is equal to the Stirling number of the second kind and can be efficiently computed using `length(partitions(array,m))`.
228
+
"""
209
229
partitions(s::AbstractVector,m::Int) =length(s) >=1&& m >=1?FixedSetPartitions(s,m) :throw(DomainError())
210
230
211
231
functionstart(p::FixedSetPartitions)
@@ -278,51 +298,59 @@ function nfixedsetpartitions(n::Int,m::Int)
278
298
return numpart
279
299
end
280
300
281
-
282
-
# For a list of integers i1, i2, i3, find the smallest
283
-
# i1^n1 * i2^n2 * i3^n3 >= x
284
-
# for integer n1, n2, n3
285
-
functionnextprod(a::Vector{Int}, x)
286
-
if x >typemax(Int)
287
-
throw(ArgumentError("unsafe for x > typemax(Int), got $x"))
288
-
end
289
-
k =length(a)
290
-
v =ones(Int, k) # current value of each counter
291
-
mx = [nextpow(ai,x) for ai in a] # maximum value of each counter
292
-
v[1] = mx[1] # start at first case that is >= x
293
-
p::widen(Int) = mx[1] # initial value of product in this case
294
-
best = p
295
-
icarry =1
296
-
297
-
while v[end] < mx[end]
298
-
if p >= x
299
-
best = p < best ? p : best # keep the best found yet
300
-
carrytest =true
301
-
while carrytest
302
-
p =div(p, v[icarry])
303
-
v[icarry] =1
304
-
icarry +=1
305
-
p *= a[icarry]
306
-
v[icarry] *= a[icarry]
307
-
carrytest = v[icarry] > mx[icarry] && icarry < k
308
-
end
309
-
if p < x
310
-
icarry =1
311
-
end
312
-
else
313
-
while p < x
314
-
p *= a[1]
315
-
v[1] *= a[1]
316
-
end
317
-
end
318
-
end
319
-
best = mx[end] < best ? mx[end] : best
320
-
returnInt(best) # could overflow, but best to have predictable return type
321
-
end
322
-
323
-
# For a list of integers i1, i2, i3, find the largest
324
-
# i1^n1 * i2^n2 * i3^n3 <= x
325
-
# for integer n1, n2, n3
301
+
#This function is still defined in Base because it is being used by Base.DSP
302
+
#"""
303
+
#Next integer not less than `n` that can be written as $\prod k_i^{p_i}$ for integers $p_1$, $p_2$, etc.
304
+
#
305
+
#For a list of integers i1, i2, i3, find the smallest
306
+
# i1^n1 * i2^n2 * i3^n3 >= x
307
+
#for integer n1, n2, n3
308
+
#"""
309
+
#function nextprod(a::Vector{Int}, x)
310
+
# if x > typemax(Int)
311
+
# throw(ArgumentError("unsafe for x > typemax(Int), got $x"))
312
+
# end
313
+
# k = length(a)
314
+
# v = ones(Int, k) # current value of each counter
315
+
# mx = [nextpow(ai,x) for ai in a] # maximum value of each counter
316
+
# v[1] = mx[1] # start at first case that is >= x
317
+
# p::widen(Int) = mx[1] # initial value of product in this case
318
+
# best = p
319
+
# icarry = 1
320
+
#
321
+
# while v[end] < mx[end]
322
+
# if p >= x
323
+
# best = p < best ? p : best # keep the best found yet
324
+
# carrytest = true
325
+
# while carrytest
326
+
# p = div(p, v[icarry])
327
+
# v[icarry] = 1
328
+
# icarry += 1
329
+
# p *= a[icarry]
330
+
# v[icarry] *= a[icarry]
331
+
# carrytest = v[icarry] > mx[icarry] && icarry < k
332
+
# end
333
+
# if p < x
334
+
# icarry = 1
335
+
# end
336
+
# else
337
+
# while p < x
338
+
# p *= a[1]
339
+
# v[1] *= a[1]
340
+
# end
341
+
# end
342
+
# end
343
+
# best = mx[end] < best ? mx[end] : best
344
+
# return Int(best) # could overflow, but best to have predictable return type
345
+
#end
346
+
347
+
"""
348
+
Previous integer not greater than `n` that can be written as $\prod k_i^{p_i}$ for integers $p_1$, $p_2$, etc.
349
+
350
+
For a list of integers i1, i2, i3, find the largest
351
+
i1^n1 * i2^n2 * i3^n3 <= x
352
+
for integer n1, n2, n3
353
+
"""
326
354
functionprevprod(a::Vector{Int}, x)
327
355
if x >typemax(Int)
328
356
throw(ArgumentError("unsafe for x > typemax(Int), got $x"))
@@ -362,7 +390,7 @@ function prevprod(a::Vector{Int}, x)
362
390
end
363
391
364
392
365
-
#Lists the partitions of the number n, the order is consistent with GAP
393
+
"Lists the partitions of the number n, the order is consistent with GAP"
366
394
functioninteger_partitions(n::Integer)
367
395
if n <0
368
396
throw(DomainError())
@@ -384,20 +412,22 @@ function integer_partitions(n::Integer)
384
412
list
385
413
end
386
414
387
-
# Produces (n,k)-combinations in cool-lex order
388
-
#
389
-
#Implements the cool-lex algorithm to generate (n,k)-combinations
390
-
#@article{Ruskey:2009fk,
391
-
# Author = {Frank Ruskey and Aaron Williams},
392
-
# Doi = {10.1016/j.disc.2007.11.048},
393
-
# Journal = {Discrete Mathematics},
394
-
# Month = {September},
395
-
# Number = {17},
396
-
# Pages = {5305-5320},
397
-
# Title = {The coolest way to generate combinations},
0 commit comments