diff --git a/src/numbers.jl b/src/numbers.jl index 4465c77..d38417f 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -10,7 +10,8 @@ export bellnum, legendresymbol, lucasnum, stirlings1, - stirlings2 + stirlings2, + dealnnoy """ bellnum(n) @@ -166,3 +167,29 @@ function stirlings2(n::Int, k::Int) return k * stirlings2(n - 1, k) + stirlings2(n - 1, k - 1) end + +""" +Delannoy number describes the number of paths from the southwest corner (0, 0) of a rectangular grid +to the northeast corner (m, n), using only single steps north, northeast, or east +""" +function dealnnoy(m::Integer,n::Integer) + if m<0 + throw(DomainError(m, "m must be nonnegative")) + end + if n<0 + throw(DomainError(n, "n must be nonnegative")) + end + A = ones(BigInt, m+1, n+1) + for i = 1:m+1 + A[i,1] = 1 + end + for i = 1:n+1 + A[1,i] = 1 + end + for i = 2:m+1 + for j = 2:n+1 + A[i,j] = A[i-1,j] + A[i-1,j-1] + A[i,j-1] + end + end + return(A[m+1,n+1]) +end diff --git a/test/numbers.jl b/test/numbers.jl index b729d36..54eadf1 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -13,6 +13,11 @@ @test lobbnum(50,100) == parse(BigInt,"303574146822833458064977353764024400258025594128") @test_throws DomainError lobbnum(-1,2) +#Delannoy +@test dealnnoy(3,4) == 129 +@test dealnnoy(4,5) == 681 +@test_throws DomainError dealnnoy(-1,2) + # narayana @test narayana(8,5) == 490 @test narayana(100,50) == parse(BigInt, "99794739256977899071474889425225225330079579752931446368")