Skip to content

Commit 80e6cf9

Browse files
committed
Merge pull request #6 from gajomi/combinations
method,test,docs,require for combinations
2 parents 0e94648 + de27ab9 commit 80e6cf9

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ most of the functions always return `BigInt`, and are marked as such below.
1212
This library provides the following functions:
1313
- `bell(n)`: returns the n-th Bell number; always returns a `BigInt`;
1414
- `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);
1516
- `derangement(n)`/`subfactorial(n)`: returns the number of permutations of n with no fixed points; always returns a `BigInt`;
1617
- `doublefactorial(n)`: returns the double factorial n!!; always returns a `BigInt`;
1718
- `fibonacci(n)`: the n-th Fibonacci number; always returns a `BigInt`;

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
julia 0.3+
22
Compat
33
Polynomials
4+
Iterators

src/Combinatorics.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module Combinatorics
22

3-
using Compat, Polynomials
3+
using Compat, Polynomials, Iterators
4+
5+
import Base:combinations
46

57
export bell,
68
derangement,
@@ -49,6 +51,10 @@ function catalan(bn::Integer)
4951
div(binomial(2*n, n), (n + 1))
5052
end
5153

54+
#generate combinations of all orders, chaining of order iterators is eager,
55+
#but sequence at each order is lazy
56+
combinations(a) = chain([combinations(a,k) for k=1:length(a)]...)
57+
5258
# The number of permutations of n with no fixed points (subfactorial)
5359
function derangement(sn::Integer)
5460
n = BigInt(sn)

test/basic.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
@test Combinatorics.catalan(5) == 42
33
@test Combinatorics.catalan(30) == parse(BigInt,"3814986502092304")
44

5+
#combinations
6+
@test collect(combinations([])) == []
7+
@test collect(combinations("abc")) == ["a","b","c","ab","ac","bc","abc"]
8+
59
# derangement
610
@test derangement(4) == 9
711
@test derangement(24) == parse(BigInt,"228250211305338670494289")

0 commit comments

Comments
 (0)