From 97620f134cf945bfbe7dc2e5c54f467fec3f3a0c Mon Sep 17 00:00:00 2001 From: thundergnat Date: Thu, 29 Oct 2020 19:35:42 -0400 Subject: [PATCH 1/2] Fix spelling of Sterling -> Stirling --- README.md | 4 ++-- lib/Math/Sequences/Integer.pm6 | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6546380..1ebbdd1 100644 --- a/README.md +++ b/README.md @@ -84,9 +84,9 @@ To gain access to these, use: * `sigma($n, $exponent=1)` The sum of positive divisors function σ. The optional exponent is the power to which each divisor is raised before summing. -* `Sterling1($n, $k)` +* `Stirling1($n, $k)` Count permutations according to their number of cycles. -* `Sterling2($n, $k)` +* `Stirling2($n, $k)` The number of ways to partition a set of n objects into k non-empty subsets. * `totient($n)` The numbers from zero to n that are co-prime to n. diff --git a/lib/Math/Sequences/Integer.pm6 b/lib/Math/Sequences/Integer.pm6 index 3e0052d..718c88d 100644 --- a/lib/Math/Sequences/Integer.pm6 +++ b/lib/Math/Sequences/Integer.pm6 @@ -337,20 +337,20 @@ sub Eulers-number ( Int $terms = 500 ) is export(:support) { } # Stirling numbers of the first kind -multi Sterling1 (0, 0) is export(:support) { 1 } -multi Sterling1 (Int \n where * > 0, 0) is export(:support) { 0 } -multi Sterling1 ( 0, Int \k where * > 0) is export(:support) { 0 } -multi Sterling1 (Int \n, Int \k) is export(:support) { +multi Stirling1 (0, 0) is export(:support) { 1 } +multi Stirling1 (Int \n where * > 0, 0) is export(:support) { 0 } +multi Stirling1 ( 0, Int \k where * > 0) is export(:support) { 0 } +multi Stirling1 (Int \n, Int \k) is export(:support) { state %seen; - (%seen{"{n - 1}|{k - 1}"} //= Sterling1(n - 1, k - 1)) - - (n - 1) * (%seen{"{n - 1}|{k}"} //= Sterling1(n - 1, k)) + (%seen{"{n - 1}|{k - 1}"} //= Stirling1(n - 1, k - 1)) - + (n - 1) * (%seen{"{n - 1}|{k}"} //= Stirling1(n - 1, k)) } # Stirling numbers of the second kind -multi Sterling2 (0, 0) is export(:support) { 1 } -multi Sterling2 (Int \n where * > 0, 0) is export(:support) { 0 } -multi Sterling2 (Int \n, Int \k where * == n) is export(:support) { 1 } -multi Sterling2 (Int \n, Int \k) is export(:support) { +multi Stirling2 (0, 0) is export(:support) { 1 } +multi Stirling2 (Int \n where * > 0, 0) is export(:support) { 0 } +multi Stirling2 (Int \n, Int \k where * == n) is export(:support) { 1 } +multi Stirling2 (Int \n, Int \k) is export(:support) { 1/factorial(k) * sum (0 .. k).map: -> \j { (-1)**j * (k choose j) * (k - j)**n } @@ -863,9 +863,9 @@ our @A006966 is export = 1, 1, 1, 1, 2, 5, 15, 53, 222, 1078, 5994, 37622, # A007318 / Pascal's triangle our @A007318 is export = 𝕀.triangle.map: -> ($n,$k) { $n choose $k }; # A008275 / Stirling 1 -our @A008275 is export = flat (1..*).map: -> $s { (1..$s).map: { Sterling1($s, $_) } }; +our @A008275 is export = flat (1..*).map: -> $s { (1..$s).map: { Stirling1($s, $_) } }; # A008277 / Stirling 2 -our @A008277 is export = flat (1..*).map: -> $s { (1..$s).map: { Sterling2($s, $_) } }; +our @A008277 is export = flat (1..*).map: -> $s { (1..$s).map: { Stirling2($s, $_) } }; # A008279 / permutations k at a time our @A008279 is export = 𝕀.triangle.map: -> ($n,$k) { factorial($n)/factorial($n-$k); From ee95620ca3c390399740e9625bbd0906d81fcd2c Mon Sep 17 00:00:00 2001 From: thundergnat Date: Thu, 29 Oct 2020 19:48:36 -0400 Subject: [PATCH 2/2] Add a generator function for @A000041 / partitions --- lib/Math/Sequences/Integer.pm6 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Math/Sequences/Integer.pm6 b/lib/Math/Sequences/Integer.pm6 index 718c88d..48bbf8e 100644 --- a/lib/Math/Sequences/Integer.pm6 +++ b/lib/Math/Sequences/Integer.pm6 @@ -417,7 +417,13 @@ our @A000035 is export = |(0,1) xx *; # A000040 / primes our @A000040 is export = lazy 𝕀.grep: {.is-prime}; # A000041 / partitions -our @A000041 is export = 1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42, &NOSEQ ... *; +our @A000041 is export = 1, { partition-sum(++$) } … *; +my @A000041-adder = lazy [\+] flat 1, ( (1 .. *) Z (1 .. *).map: * × 2 + 1 ); +sub partition-sum ($n) { + sum @A000041[$n X- @A000041-adder[^(@A000041-adder.first: * > $n, :k)]] + Z× (flat (1, 1, -1, -1) xx *) +} + # A000043 / Mersenne our @A000043 is export = lazy 𝕀.grep: { .is-prime and (2**$_-1).is-prime }; # A000045 / Fibonacci