move to lib
This commit is contained in:
@@ -18,46 +18,15 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"scm.dairydemon.net/filifa/mathtools/internal/lib"
|
||||
)
|
||||
|
||||
var partitionsN string
|
||||
var partitionsK string
|
||||
|
||||
// given a slice where vals[i] = p_{k-1}(i) for a given k, nextPartition updates the slice so vals[i] = p_k(i) using the property that p_k(n) = p_k(n-k) + p_{k-1}(n)
|
||||
func nextPartition(k int, vals []*big.Int) {
|
||||
for i := 1; i < len(vals); i++ {
|
||||
if i-k >= 0 {
|
||||
vals[i].Add(vals[i], vals[i-k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func p(n, k int) *big.Int {
|
||||
if n < 0 {
|
||||
return big.NewInt(0)
|
||||
}
|
||||
|
||||
if k > n {
|
||||
k = n
|
||||
}
|
||||
|
||||
vals := make([]*big.Int, n+1)
|
||||
for i := range vals {
|
||||
vals[i] = big.NewInt(0)
|
||||
}
|
||||
vals[0] = big.NewInt(1)
|
||||
|
||||
for i := 1; i <= k; i++ {
|
||||
nextPartition(i, vals)
|
||||
}
|
||||
|
||||
return vals[n]
|
||||
}
|
||||
|
||||
func partitions(cmd *cobra.Command, args []string) {
|
||||
n, err := strconv.Atoi(partitionsN)
|
||||
if err != nil {
|
||||
@@ -83,7 +52,7 @@ func partitions(cmd *cobra.Command, args []string) {
|
||||
cobra.CheckErr("k must be nonnegative")
|
||||
}
|
||||
|
||||
z := p(n, k)
|
||||
z := lib.Partitions(n, k)
|
||||
fmt.Println(z)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user