From 4d277f30eb75f5e90d5538a4e5c73bd5175eb9c4 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Sat, 3 May 2025 17:04:11 +0800 Subject: [PATCH 01/18] bellnum: improve error msg --- src/numbers.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/numbers.jl b/src/numbers.jl index 75f9211..104740a 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -19,7 +19,7 @@ Compute the ``n``th Bell number. """ function bellnum(n::Integer) if n < 0 - throw(DomainError(n)) + throw(DomainError(n, "n must be nonnegative")) elseif n < 2 return 1 end From 655fdfa4fbc7df45a3d6902ca110fc4bbfd968ea Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Sat, 3 May 2025 17:09:01 +0800 Subject: [PATCH 02/18] doc: add doc and examples for `bellnum` --- src/numbers.jl | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/numbers.jl b/src/numbers.jl index 104740a..95d89e4 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -1,4 +1,4 @@ -#Special named numbers and symbols +# Special named numbers and symbols export bellnum, catalannum, @@ -16,6 +16,28 @@ export bellnum, bellnum(n) Compute the ``n``th Bell number. + +# Examples +```jldoctest +julia> [ bellnum(i) for i in 0:5 ] +6-element Vector{Signed}: + 1 + 1 + 2 + 5 + 15 + 52 + +julia> bellnum(-1) +ERROR: DomainError with -1: +n must be nonnegative +Stacktrace: +[...] +``` + +# References +- [Bell number - Wikipedia](https://en.wikipedia.org/wiki/Bell_number) +- [DLMF: §26.7 Set Partitions: Bell Numbers](https://dlmf.nist.gov/26.7) """ function bellnum(n::Integer) if n < 0 From e60c4daac0598c51cbbe40fd7338eee004f00be5 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Sat, 3 May 2025 17:12:36 +0800 Subject: [PATCH 03/18] test: update tests for `bellnum` --- test/numbers.jl | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/test/numbers.jl b/test/numbers.jl index 73330f9..d8b6521 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1,4 +1,24 @@ + @testset "numbers" begin + # bell + # https://dlmf.nist.gov/26.7#T1 + @test bellnum.(0:10) == [ + 1 + 1 + 2 + 5 + 15 + 52 + 203 + 877 + 4140 + 21147 + 115975 + ] + # gap> Bell(42); + @test bellnum(42) == parse(BigInt, "35742549198872617291353508656626642567") + @test_throws DomainError bellnum(-1) + # catalan @test catalannum(5) == 42 @test catalannum(30) == parse(BigInt, "3814986502092304") @@ -68,22 +88,4 @@ @test stirlings2(6, 6) == 1 @test stirlings2(big"26", 10) == 13199555372846848005 - # bell - @test bellnum.(0:10) == [ - 1 - 1 - 2 - 5 - 15 - 52 - 203 - 877 - 4140 - 21147 - 115975 - ] - - @test bellnum(42) == parse(BigInt, "35742549198872617291353508656626642567") - @test_throws DomainError(-1) bellnum(-1) - end From c125e16fcffb6a0bb34eac3eb7f1af0002d20c95 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Sat, 3 May 2025 17:21:57 +0800 Subject: [PATCH 04/18] doc: add doc and examples for `catalannum` --- src/numbers.jl | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/numbers.jl b/src/numbers.jl index 95d89e4..92fbd72 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -60,7 +60,32 @@ end """ catalannum(n) -Compute the ``n``th Catalan number. +Compute the ``n``th Catalan number given by: +```math +C_n = \\frac{1}{n+1} \\binom{2n}{n} +``` + +# Examples +```jldoctest +julia> [ catalannum(i) for i in 0:5 ] +6-element Vector{BigInt}: + 1 + 1 + 2 + 5 + 14 + 42 + +julia> catalannum(-1) +ERROR: DomainError with -1: +n must be nonnegative +Stacktrace: +[...] +``` + +# References +- [Catalan number - Wikipedia](https://en.wikipedia.org/wiki/Catalan_number) +- [DLMF: §26.5 Lattice Paths: Catalan Numbers](https://dlmf.nist.gov/26.5) """ function catalannum(bn::Integer) if bn < 0 From ac3abb691937eb7f7591dfd3cd0629be34cc1cc1 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Sat, 3 May 2025 17:22:13 +0800 Subject: [PATCH 05/18] test: update tests for `catalannum` --- test/numbers.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/numbers.jl b/test/numbers.jl index d8b6521..8acdbca 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -20,7 +20,9 @@ @test_throws DomainError bellnum(-1) # catalan - @test catalannum(5) == 42 + # https://oeis.org/A000108 + @test catalannum.(0:10) == [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796] + @test catalannum(20) == 6564120420 @test catalannum(30) == parse(BigInt, "3814986502092304") @test_throws DomainError catalannum(-1) From 7588066723af05f11c66b91906359d899580779f Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Sat, 3 May 2025 17:35:34 +0800 Subject: [PATCH 06/18] lobbnum: improve error msg --- src/numbers.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/numbers.jl b/src/numbers.jl index 92fbd72..798fff1 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -104,7 +104,7 @@ Wikipedia : https://en.wikipedia.org/wiki/Lobb_number """ function lobbnum(bm::Integer,bn::Integer) if !(0 <= bm <= bn) - throw(DomainError("m and n must be non-negative")) + throw(DomainError((m=bm, n=bn), "m and n must be non-negative and m <= n")) else m = BigInt(bm) n = BigInt(bn) From b5b8570ce936e717d29327ed1a4cba7a40e42f62 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Sun, 11 May 2025 00:34:44 +0800 Subject: [PATCH 07/18] doc: add doc and examples for `lobbnum` --- src/numbers.jl | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/numbers.jl b/src/numbers.jl index 798fff1..b1dee62 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -97,12 +97,47 @@ function catalannum(bn::Integer) end """ - lobbnum(m,n) + lobbnum(m, n) -Compute the Lobb number `L(m,n)`, or the generalised Catalan number given by ``\\frac{2m+1}{m+n+1} \\binom{2n}{m+n}``. -Wikipedia : https://en.wikipedia.org/wiki/Lobb_number +Compute the Lobb number `L(m,n)`, or the generalised Catalan number given by: +```math +L_{m,n} = \\frac{2m+1}{m+n+1} \\binom{2n}{m+n} +``` +For `m = 0`, we get the ``n``-th Catalan number. + +See also: [`catalannum`](@ref). + +# Examples +```jldoctest +julia> [ [lobbnum(m, n) for m in 0:n] for n in 0:5 ] +6-element Vector{Vector{BigInt}}: + [1] + [1, 1] + [2, 3, 1] + [5, 9, 5, 1] + [14, 28, 20, 7, 1] + [42, 90, 75, 35, 9, 1] + +julia> lobbnum(0, 25) == catalannum(25) +true + +julia> lobbnum(-1, 1) +ERROR: DomainError with (m = -1, n = 1): +m and n must be non-negative and m <= n +Stacktrace: +[...] + +julia> lobbnum(5, 1) +ERROR: DomainError with (m = 5, n = 1): +m and n must be non-negative and m <= n +Stacktrace: +[...] +``` + +# References +- [Lobb number - Wikipedia](https://en.wikipedia.org/wiki/Lobb_number) """ -function lobbnum(bm::Integer,bn::Integer) +function lobbnum(bm::Integer, bn::Integer) if !(0 <= bm <= bn) throw(DomainError((m=bm, n=bn), "m and n must be non-negative and m <= n")) else From 036533d6d3ccae8a9949b6474a6a91b1f1faf2a5 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Thu, 20 Nov 2025 21:37:34 +0800 Subject: [PATCH 08/18] doc: add doc and examples for `fibonaccinum` --- src/numbers.jl | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/numbers.jl b/src/numbers.jl index b1dee62..94659b1 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -163,6 +163,43 @@ function narayana(bn::Integer,bk::Integer) div(binomial(n, k)*binomial(n, k - 1) , n) end +""" + fibonaccinum(n) + +Compute the ``n``th Fibonacci number, ``F_n``, given by: +```math +F_0 = 0 +\\\\ +F_1 = 1 +\\\\ +F_n = F_{n-1} + F_{n-2} +``` + +# Examples +```jldoctest +julia> [ fibonaccinum(i) for i in 0:5 ] +6-element Vector{BigInt}: + 0 + 1 + 1 + 2 + 3 + 5 + +julia> fibonaccinum(13) +233 + +julia> fibonaccinum(-1) +ERROR: DomainError with -1: +n must be nonnegative +Stacktrace: +[...] +``` + +# References +- [Fibonacci sequence - Wikipedia](https://en.wikipedia.org/wiki/Fibonacci_sequence) +- [DLMF: §26.11 Fibonacci number](https://dlmf.nist.gov/26.11#p4) +""" function fibonaccinum(n::Integer) if n < 0 throw(DomainError(n, "n must be nonnegative")) From ee909e274e249e35aa828691ca7385c534c7e0cc Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Thu, 20 Nov 2025 22:03:26 +0800 Subject: [PATCH 09/18] test: update tests for `fibonaccinum` --- test/numbers.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/numbers.jl b/test/numbers.jl index 8acdbca..e72ad54 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -27,8 +27,14 @@ @test_throws DomainError catalannum(-1) # fibonacci - @test fibonaccinum(5) == 5 + # https://oeis.org/A000045 + # Fibonacci and Lucas Factorizations: https://mersennus.net/fibonacci/f1000.txt + @test fibonaccinum.(0:10) == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] + @test fibonaccinum(92) == big"7540113804746346429" + @test fibonaccinum(92) < typemax(Int64) < fibonaccinum(93) @test fibonaccinum(101) == parse(BigInt, "573147844013817084101") + @test fibonaccinum(184) < typemax(Int128) < fibonaccinum(185) + @test fibonaccinum(233) == (139801 * 25047390419633 * big"631484089583693149557829547141") @test_throws DomainError fibonaccinum(-1) # lobb From e874697b9f8a68c316682aedda6a0695bc1a6d10 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Thu, 20 Nov 2025 22:53:11 +0800 Subject: [PATCH 10/18] doc: add doc and examples for `jacobisymbol` --- src/numbers.jl | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/numbers.jl b/src/numbers.jl index 94659b1..a3a4e99 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -210,6 +210,44 @@ function fibonaccinum(n::Integer) end +""" + jacobisymbol(a, b) + +Compute the Jacobi symbol ``\\left(\\tfrac{a}{b}\\right)`` for odd ``b``. +Returns ``-1``, ``0``, or ``1``. + +# Examples +```jldoctest +julia> jacobisymbol.(2:4, 3) +3-element Vector{Int32}: + -1 + 0 + 1 + +julia> jacobisymbol.(-2:2, 1) # (a|1) = 1 +5-element Vector{Int32}: + 1 + 1 + 1 + 1 + 1 + +julia> [jacobisymbol(a, n) for n in (1,3,5,7,9), a in 1:9] +5×9 Matrix{Int32}: + 1 1 1 1 1 1 1 1 1 + 1 -1 0 1 -1 0 1 -1 0 + 1 -1 -1 1 0 1 -1 -1 1 + 1 1 -1 1 -1 -1 0 1 1 + 1 1 0 1 1 0 1 1 0 + +julia> jacobisymbol(1001, 9907) +-1 +``` + +# References +- [Jacobi symbol - Wikipedia](https://en.wikipedia.org/wiki/Jacobi_symbol) +- [DLMF: §27.9 Jacobi symbol](https://dlmf.nist.gov/27.9#p3) +""" function jacobisymbol(a::Integer, b::Integer) ba = Ref{BigInt}(a) bb = Ref{BigInt}(b) From f7eed5f7a9bb23635d0dc625b2ca7e67817447e2 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Thu, 20 Nov 2025 23:00:21 +0800 Subject: [PATCH 11/18] doc: add doc and examples for `legendresymbol` --- src/numbers.jl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/numbers.jl b/src/numbers.jl index a3a4e99..f6bfd23 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -267,6 +267,36 @@ function lassallenum(m::Integer) A[m] end +""" + legendresymbol(a, p) + +Compute the Legendre symbol ``\\left(\\tfrac{a}{p}\\right)`` for odd prime ``p``. +Returns ``-1``, ``0``, or ``1``. + +# Examples +```jldoctest +julia> legendresymbol.(6:8, 7) +3-element Vector{Int32}: + -1 + 0 + 1 + +julia> [ [legendresymbol.(a, n) for a in 0:(n-1)] for n in (1,3,5,7,9) ] +5-element Vector{Vector{Int32}}: + [1] + [0, 1, -1] + [0, 1, -1, -1, 1] + [0, 1, 1, -1, 1, -1, -1] + [0, 1, 1, 0, 1, 1, 0, 1, 1] + +julia> legendresymbol(1001, 9907) +-1 +``` + +# References +- [Legendre symbol - Wikipedia](https://en.wikipedia.org/wiki/Legendre_symbol) +- [DLMF: §27.9 Legendre symbol](https://dlmf.nist.gov/27.9) +""" function legendresymbol(a::Integer, b::Integer) ba = Ref{BigInt}(a) bb = Ref{BigInt}(b) From 58a84aa3340ca5d7760e1bda840d1fd093ce3798 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Thu, 20 Nov 2025 23:06:39 +0800 Subject: [PATCH 12/18] doc: add doc and examples for `lucasnum` --- src/numbers.jl | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/numbers.jl b/src/numbers.jl index f6bfd23..fca30d7 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -303,6 +303,43 @@ function legendresymbol(a::Integer, b::Integer) return ccall((:__gmpz_legendre, :libgmp), Cint, (Ref{BigInt}, Ref{BigInt}), ba, bb) end +""" + lucasnum(n) + +Compute the ``n``th Lucas number, ``L_n``, given by: +```math +L_0 = 2 +\\\\ +L_1 = 1 +\\\\ +L_n = L_{n-1} + L_{n-2} +``` + +# Examples +```jldoctest +julia> [ lucasnum(i) for i in 0:5 ] +6-element Vector{BigInt}: + 2 + 1 + 3 + 4 + 7 + 11 + +julia> lucasnum(10) +123 + +julia> lucasnum(-1) +ERROR: DomainError with -1: +n must be nonnegative +Stacktrace: +[...] +``` + +# References +- [Lucas number - Wikipedia](https://en.wikipedia.org/wiki/Lucas_number) +- [DLMF: §24.15 Lucas numbers](https://dlmf.nist.gov/24.15#iv.p1) +""" function lucasnum(n::Integer) if n < 0 throw(DomainError(n, "n must be nonnegative")) From f9d7bd14a7db77c1fb67ff47ac62fce3ccb9cc75 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Thu, 20 Nov 2025 23:26:52 +0800 Subject: [PATCH 13/18] doc: add doc and examples for `stirlings1` --- src/numbers.jl | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/numbers.jl b/src/numbers.jl index fca30d7..21973cd 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -350,9 +350,45 @@ function lucasnum(n::Integer) end """ - stirlings1(n::Integer, k::Integer) + stirlings1(n::Integer, k::Integer, signed::Bool=false) -Compute the Stirling number of the first kind, `s(n,k)`. +Compute the Stirling number of the first kind, ``s(n,k)``. + +If `signed` is `true`, return the signed value ``(-1)^{n-k} s(n,k)``. + +# Examples +```jldoctest +julia> stirlings1(5, 5) # s(n, n) = 1 +1 + +julia> n=9; stirlings1(n, 1) == factorial(n-1) +true + +julia> n=233; stirlings1(n, n-1) == binomial(n,2) +true + +julia> stirlings1(6, 3, true) +-225 + +julia> [(k<=n ? stirlings1(n,k,true) : 0) for n in 1:6, k in 1:6] +6×6 Matrix{Int64}: + 1 0 0 0 0 0 + -1 1 0 0 0 0 + 2 -3 1 0 0 0 + -6 11 -6 1 0 0 + 24 -50 35 -10 1 0 + -120 274 -225 85 -15 1 + +julia> stirlings1(-1, 1) +ERROR: DomainError with -1: +n must be nonnegative +Stacktrace: +[...] +``` + +# References +- [Stirling numbers of the first kind - Wikipedia](https://en.wikipedia.org/wiki/Stirling_numbers_of_the_first_kind) +- [DLMF: §26.8 Stirling number of the first kind](https://dlmf.nist.gov/26.8#i.p1) """ function stirlings1(n::Integer, k::Integer, signed::Bool=false) if signed == true From fbb188e15b4e246be0236957f615625dbe0bf56c Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Thu, 20 Nov 2025 23:42:59 +0800 Subject: [PATCH 14/18] doc: add doc and examples for `stirlings2` --- src/numbers.jl | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/numbers.jl b/src/numbers.jl index 21973cd..86eea30 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -419,7 +419,54 @@ end """ stirlings2(n::Integer, k::Integer) -Compute the Stirling number of the second kind, `S(n,k)`. +Compute the Stirling number of the second kind, ``S(n,k)``. + +# Examples +```jldoctest +julia> stirlings2(0, 0) +1 + +julia> n=233; stirlings2(n, 0) == 0 # n > 0 +true + +julia> stirlings2(0, 1) +0 + +julia> n=13; stirlings2(n, 1) == stirlings2(n, n) == 1 # n > 0 +true + +julia> n=6; [stirlings2(6, k) for k in 0:6] +7-element Vector{Int64}: + 0 + 1 + 31 + 90 + 65 + 15 + 1 + +julia> n=6; sum(stirlings2(6, k) for k in 0:6) == bellnum(n) +true + +julia> [stirlings2(n,k) for n in 1:6, k in 1:6] +6×6 Matrix{Int64}: + 1 0 0 0 0 0 + 1 1 0 0 0 0 + 1 3 1 0 0 0 + 1 7 6 1 0 0 + 1 15 25 10 1 0 + 1 31 90 65 15 1 + +julia> stirlings2(-1, 1) +ERROR: DomainError with -1: +n must be nonnegative +Stacktrace: +[...] +``` + +# References +- [Stirling numbers of the second kind - Wikipedia](https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind) +- [DLMF: §26.8 Stirling number of the second kind](https://dlmf.nist.gov/26.8#i.p3) """ function stirlings2(n::Integer, k::Integer) if n < 0 From fd4f82d57e801496092787af00a5953b034954fa Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Thu, 20 Nov 2025 23:49:11 +0800 Subject: [PATCH 15/18] doc: update doc for Stirling number --- src/numbers.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/numbers.jl b/src/numbers.jl index 86eea30..ff90363 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -352,12 +352,15 @@ end """ stirlings1(n::Integer, k::Integer, signed::Bool=false) -Compute the Stirling number of the first kind, ``s(n,k)``. +Compute the Stirling number of the first kind, ``s(n,k)``, for non-negative `n`. If `signed` is `true`, return the signed value ``(-1)^{n-k} s(n,k)``. # Examples ```jldoctest +julia> stirlings1(0, 0) +1 + julia> stirlings1(5, 5) # s(n, n) = 1 1 @@ -370,7 +373,7 @@ true julia> stirlings1(6, 3, true) -225 -julia> [(k<=n ? stirlings1(n,k,true) : 0) for n in 1:6, k in 1:6] +julia> [stirlings1(n,k,true) for n in 1:6, k in 1:6] 6×6 Matrix{Int64}: 1 0 0 0 0 0 -1 1 0 0 0 0 @@ -419,7 +422,7 @@ end """ stirlings2(n::Integer, k::Integer) -Compute the Stirling number of the second kind, ``S(n,k)``. +Compute the Stirling number of the second kind, ``S(n,k)``, for non-negative `n`. # Examples ```jldoctest From f434e08fb3b3e365b3ca1911aaacb3440887016b Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Fri, 21 Nov 2025 00:14:19 +0800 Subject: [PATCH 16/18] doc: add examples for `lassallenum` --- src/numbers.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/numbers.jl b/src/numbers.jl index ff90363..068bd40 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -258,6 +258,25 @@ end lassallenum(n) Compute the ``n``th entry in Lassalle's sequence, OEIS entry A180874. + +# Examples +```jldoctest +julia> lassallenum.(1:5) +5-element Vector{BigInt}: + 1 + 1 + 5 + 56 + 1092 + +julia> lassallenum(14) +270316008395632253340 +``` + +# References +- Lassalle, M. (2012). Two integer sequences related to Catalan numbers. + *Journal of Combinatorial Theory*, Series A, 119(4), 923-935. +- [OEIS A180874](https://oeis.org/A180874) """ function lassallenum(m::Integer) A = ones(BigInt, m) From 3e79e6d46247f502ec3225ad18bfbfac7cdc572f Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Fri, 21 Nov 2025 00:17:35 +0800 Subject: [PATCH 17/18] test: add tests for `lassallenum` --- test/numbers.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/numbers.jl b/test/numbers.jl index e72ad54..a7a73c5 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -48,7 +48,10 @@ @test_throws DomainError narayana(-1, -1) # lassalle + # https://oeis.org/A180874 + @test lassallenum.(1:10) == [1,1,5,56,1092,32670,1387815,79389310,5882844968,548129834616] @test lassallenum(14) == parse(BigInt, "270316008395632253340") + @test lassallenum(17) == parse(BigInt, "4359147487054262623576455600") # legendresymbol @test legendresymbol(1001, 9907) == jacobisymbol(1001, 9907) == -1 From 5a7eb6747147899f2f96ef8a3468a9a016e2d116 Mon Sep 17 00:00:00 2001 From: Chengyu HAN Date: Fri, 21 Nov 2025 00:26:40 +0800 Subject: [PATCH 18/18] doc: add examples for `narayana` --- src/numbers.jl | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/numbers.jl b/src/numbers.jl index 068bd40..258be2f 100644 --- a/src/numbers.jl +++ b/src/numbers.jl @@ -150,12 +150,40 @@ end """ narayana(n,k) -Compute the Narayana number `N(n,k)` given by ``\\frac{1}{n}\\binom{n}{k}\\binom{n}{k-1}`` -Wikipedia : https://en.wikipedia.org/wiki/Narayana_number +Compute the Narayana number `N(n,k)` given by ``\\frac{1}{n}\\binom{n}{k}\\binom{n}{k-1}``, +where ``1 \\leq k \\leq n``. + +# Examples +```jldoctest +julia> narayana(1, 1) +1 + +julia> narayana(8, 5) +490 + +julia> [ [narayana(n, k) for k in 1:n] for n in 1:6 ] +6-element Vector{Vector{BigInt}}: + [1] + [1, 1] + [1, 3, 1] + [1, 6, 6, 1] + [1, 10, 20, 10, 1] + [1, 15, 50, 50, 15, 1] + +julia> narayana(3, 4) +ERROR: DomainError with (n = 3, k = 4): +n and k must be 1 <= k <= n +Stacktrace: +[...] +``` + +# References +- [Narayana number - Wikipedia](https://en.wikipedia.org/wiki/Narayana_number) +- [DLMF: §26.6 Narayana Number](https://dlmf.nist.gov/26.6#Px3) """ function narayana(bn::Integer,bk::Integer) if !(1 <= bk <= bn) - throw(DomainError("Domain is 1 <= k <= n")) + throw(DomainError((n=bn, k=bk), "n and k must be 1 <= k <= n")) else n = BigInt(bn) k = BigInt(bk)