69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
/*
|
|
Copyright © 2025 filifa
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package lib
|
|
|
|
import (
|
|
"math/big"
|
|
)
|
|
|
|
// TODO: implement both of these with dynamic programming
|
|
|
|
func Stirling1(n, k *big.Int) *big.Int {
|
|
if n.Cmp(big.NewInt(0)) == 0 && k.Cmp(big.NewInt(0)) == 0 {
|
|
return big.NewInt(1)
|
|
}
|
|
|
|
if n.Cmp(big.NewInt(0)) == 0 || k.Cmp(big.NewInt(0)) == 0 {
|
|
return big.NewInt(0)
|
|
}
|
|
|
|
newN := new(big.Int).Set(n)
|
|
newN.Sub(newN, big.NewInt(1))
|
|
|
|
result := Stirling1(newN, k)
|
|
result.Mul(result, newN)
|
|
result.Neg(result)
|
|
|
|
newK := new(big.Int).Set(k)
|
|
newK.Sub(newK, big.NewInt(1))
|
|
|
|
result.Add(result, Stirling1(newN, newK))
|
|
return result
|
|
}
|
|
|
|
func Stirling2(n, k *big.Int) *big.Int {
|
|
if n.Cmp(k) == 0 {
|
|
return big.NewInt(1)
|
|
}
|
|
|
|
if n.Cmp(big.NewInt(0)) == 0 || k.Cmp(big.NewInt(0)) == 0 {
|
|
return big.NewInt(0)
|
|
}
|
|
|
|
newN := new(big.Int).Set(n)
|
|
newN.Sub(newN, big.NewInt(1))
|
|
|
|
result := Stirling2(newN, k)
|
|
result.Mul(result, k)
|
|
|
|
newK := new(big.Int).Set(k)
|
|
newK.Sub(newK, big.NewInt(1))
|
|
|
|
result.Add(result, Stirling2(newN, newK))
|
|
return result
|
|
}
|