Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spelling of Stirling, add a generator function for A000041 #155

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 19 additions & 13 deletions lib/Math/Sequences/Integer.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -863,9 +869,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);
Expand Down