Compare commits
No commits in common. "d48e9932c1f039165185351d5bb8416f4cdc2d84" and "a9c7f8091b46a3265d083a3d5d4b5f7c4b6959fb" have entirely different histories.
d48e9932c1
...
a9c7f8091b
36
README.md
36
README.md
|
|
@ -1,36 +0,0 @@
|
|||
# mathtools
|
||||
mathtools is a program for computing various mathematical results that would be
|
||||
tedious to compute by hand.
|
||||
|
||||
## Why?
|
||||
Obviously, libraries, software packages, and websites exist for these sort of
|
||||
calculations, but there are tradeoffs with any approach. Rather than needing to
|
||||
write a script, use a REPL, or load a webpage, this allows for an approach more
|
||||
like standard CLI utilities such as [GNU
|
||||
factor](https://www.gnu.org/software/coreutils/factor).
|
||||
|
||||
Generally, I've opted to implement routines for problems that are best solved
|
||||
with *algorithms*, rather than *formulas*. For instance, the quadratic formula,
|
||||
while useful, is basically plug and chug, and thus isn't implemented here. On
|
||||
the other hand, determining whether a number is prime is a little more tedious
|
||||
to do by hand, so it's provided as a routine.
|
||||
|
||||
## Available routines
|
||||
Available routines include:
|
||||
* convergents of a periodic continued fraction
|
||||
* solving systems of linear congruences with the Chinese remainder theorem
|
||||
* discrete logarithm
|
||||
* greatest common divisor
|
||||
* primality testing
|
||||
* Jacobi symbol
|
||||
* modular inverse
|
||||
* modular square root
|
||||
* multiplicative order
|
||||
* integer partitions
|
||||
* solving Pell equations
|
||||
* primitive root modulo n
|
||||
* area of a simple polygon from vertex coordinates
|
||||
* sieves for totient function, divisor function, Mobius function, and more
|
||||
* repetend of the continued fraction of a square root
|
||||
* Stirling numbers
|
||||
* summatory functions
|
||||
|
|
@ -32,7 +32,7 @@ func divisors(cmd *cobra.Command, args []string) {
|
|||
bufStdout := bufio.NewWriter(os.Stdout)
|
||||
defer bufStdout.Flush()
|
||||
|
||||
ch := sieve.Divisors(divisorsN, divisorsE, 1000)
|
||||
ch := sieve.DivisorsSieve(divisorsN, divisorsE, 1000)
|
||||
for i := 0; ; i++ {
|
||||
v, ok := <-ch
|
||||
if !ok {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func mobius(cmd *cobra.Command, args []string) {
|
|||
bufStdout := bufio.NewWriter(os.Stdout)
|
||||
defer bufStdout.Flush()
|
||||
|
||||
ch := sieve.Mobius(mobiusN, 1000)
|
||||
ch := sieve.MobiusSieve(mobiusN, 1000)
|
||||
for i := 0; ; i++ {
|
||||
v, ok := <-ch
|
||||
if !ok {
|
||||
|
|
|
|||
|
|
@ -1,75 +0,0 @@
|
|||
/*
|
||||
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 cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"scm.dairydemon.net/filifa/mathtools/internal/lib"
|
||||
)
|
||||
|
||||
var multiplicativeOrderBase string
|
||||
var multiplicativeOrderModulus string
|
||||
|
||||
func multiplicativeOrder(cmd *cobra.Command, args []string) {
|
||||
g, ok := new(big.Int).SetString(multiplicativeOrderBase, 10)
|
||||
if !ok {
|
||||
cobra.CheckErr("invalid base " + multiplicativeOrderBase)
|
||||
}
|
||||
|
||||
m, ok := new(big.Int).SetString(multiplicativeOrderModulus, 10)
|
||||
if !ok {
|
||||
cobra.CheckErr("invalid modulus " + multiplicativeOrderModulus)
|
||||
}
|
||||
|
||||
gcd := new(big.Int).GCD(nil, nil, g, m)
|
||||
if gcd.Cmp(big.NewInt(1)) != 0 {
|
||||
cobra.CheckErr("base " + multiplicativeOrderBase + " and modulus " + multiplicativeOrderModulus + " are not coprime")
|
||||
}
|
||||
|
||||
k := lib.MultiplicativeOrder(g, m)
|
||||
fmt.Println(k)
|
||||
}
|
||||
|
||||
// multiplicativeOrderCmd represents the multiplicativeOrder command
|
||||
var multiplicativeOrderCmd = &cobra.Command{
|
||||
Use: "multiplicative-order",
|
||||
Short: "Compute multiplicative order",
|
||||
Long: `Compute the multiplicative order of a number given a modulus.`,
|
||||
Run: multiplicativeOrder,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(multiplicativeOrderCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// multiplicativeOrderCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// multiplicativeOrderCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
multiplicativeOrderCmd.Flags().StringVarP(&multiplicativeOrderBase, "base", "g", "", "base")
|
||||
multiplicativeOrderCmd.Flags().StringVarP(&multiplicativeOrderModulus, "modulus", "m", "", "modulus")
|
||||
|
||||
multiplicativeOrderCmd.MarkFlagRequired("base")
|
||||
multiplicativeOrderCmd.MarkFlagRequired("modulus")
|
||||
}
|
||||
|
|
@ -32,7 +32,7 @@ func primeOmega(cmd *cobra.Command, args []string) {
|
|||
bufStdout := bufio.NewWriter(os.Stdout)
|
||||
defer bufStdout.Flush()
|
||||
|
||||
ch := sieve.PrimeOmega(primeOmegaN, primeOmegaMul, 1000)
|
||||
ch := sieve.PrimeOmegaSieve(primeOmegaN, primeOmegaMul, 1000)
|
||||
for i := 0; ; i++ {
|
||||
v, ok := <-ch
|
||||
if !ok {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func radical(cmd *cobra.Command, args []string) {
|
|||
bufStdout := bufio.NewWriter(os.Stdout)
|
||||
defer bufStdout.Flush()
|
||||
|
||||
ch := sieve.Radical(radicalN, 1000)
|
||||
ch := sieve.RadicalSieve(radicalN, 1000)
|
||||
for i := 0; ; i++ {
|
||||
v, ok := <-ch
|
||||
if !ok {
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"scm.dairydemon.net/filifa/mathtools/lib"
|
||||
"scm.dairydemon.net/filifa/mathtools/internal/lib"
|
||||
)
|
||||
|
||||
var firstKind bool
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func totient(cmd *cobra.Command, args []string) {
|
|||
bufStdout := bufio.NewWriter(os.Stdout)
|
||||
defer bufStdout.Flush()
|
||||
|
||||
for v := range sieve.Totient(totientN, 1000) {
|
||||
for v := range sieve.TotientSieve(totientN, 1000) {
|
||||
if v == 0 {
|
||||
continue
|
||||
}
|
||||
|
|
|
|||
3
go.mod
3
go.mod
|
|
@ -2,9 +2,8 @@ module scm.dairydemon.net/filifa/mathtools
|
|||
|
||||
go 1.24.4
|
||||
|
||||
require github.com/spf13/cobra v1.9.1
|
||||
|
||||
require (
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/spf13/cobra v1.9.1 // indirect
|
||||
github.com/spf13/pflag v1.0.6 // indirect
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
/*
|
||||
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 sieve
|
||||
|
||||
func updateMultiples(sieve []uint, p uint, n uint, additive bool) {
|
||||
for q := p; ; q *= p {
|
||||
// sieve[a*b] = sieve[a] * sieve[b] if gcd(a,b) = 1
|
||||
for i := 2 * q; i < n; i += q {
|
||||
if i%(p*q) != 0 {
|
||||
if additive {
|
||||
sieve[i] += sieve[q]
|
||||
} else {
|
||||
sieve[i] *= sieve[q]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if p*q >= n {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -30,10 +30,29 @@ func pow(base uint, exp uint) uint {
|
|||
return result
|
||||
}
|
||||
|
||||
func updateMultiples(sieve []uint, x uint, p uint, n uint) {
|
||||
for q := p; ; q *= p {
|
||||
// sigma_x(a*b) = sigma_x(a) * sigma_x(b) if gcd(a,b) = 1
|
||||
for i := 2 * q; i < n; i += q {
|
||||
if i%(p*q) != 0 {
|
||||
sieve[i] *= sieve[q]
|
||||
}
|
||||
}
|
||||
|
||||
if p*q >= n {
|
||||
break
|
||||
}
|
||||
println(q)
|
||||
|
||||
// sigma_x(p^k) = p^(kx) + sigma_x(p^(k-1))
|
||||
sieve[p*q] = pow(p*q, x) + sieve[q]
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Divisors computes sigma_x(k) for k=1 to n, where sigma_x is the divisor sum function. x sets the power each divisor is raised to.
|
||||
DivisorSieve computes sigma_x(k) for k=1 to n, where sigma_x is the divisor sum function. x sets the power each divisor is raised to.
|
||||
*/
|
||||
func Divisors(n uint, x uint, buflen uint) chan uint {
|
||||
func DivisorsSieve(n uint, x uint, buflen uint) chan uint {
|
||||
sieve := make([]uint, n)
|
||||
sieve[0] = 0
|
||||
for i := uint(1); i < n; i++ {
|
||||
|
|
@ -50,12 +69,7 @@ func Divisors(n uint, x uint, buflen uint) chan uint {
|
|||
}
|
||||
|
||||
sieve[i] = pow(i, x) + 1
|
||||
for j := i; i*j < n; j *= i {
|
||||
// sigma_x(p^k) = p^(kx) + sigma_x(p^(k-1))
|
||||
sieve[i*j] = pow(i*j, x) + sieve[j]
|
||||
}
|
||||
|
||||
updateMultiples(sieve, i, n, false)
|
||||
updateMultiples(sieve, x, i, n)
|
||||
ch <- sieve[i]
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -17,9 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
package sieve
|
||||
|
||||
/*
|
||||
Mobius computes mobius(k) for k=1 to n, where mobius is the Mobius function.
|
||||
MobiusSieve computes mobius(k) for k=1 to n, where mobius is the Mobius function.
|
||||
*/
|
||||
func Mobius(n uint, buflen uint) chan int {
|
||||
func MobiusSieve(n uint, buflen uint) chan int {
|
||||
sieve := make([]int, n)
|
||||
for i := 0; i < int(n); i++ {
|
||||
sieve[i] = i
|
||||
|
|
|
|||
|
|
@ -16,16 +16,29 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
||||
package sieve
|
||||
|
||||
func updatePowers(sieve []uint, p uint, n uint) {
|
||||
for q := p; p*q < n; q *= p {
|
||||
sieve[p*q] = 1 + sieve[q]
|
||||
func primeOmegaUpdateMultiples(sieve []uint, p uint, n uint, multiplicity bool) {
|
||||
for q := p; ; q *= p {
|
||||
// omega(a*b) = omega(a) + omega(b) if gcd(a,b) = 1
|
||||
for i := 2 * q; i < n; i += q {
|
||||
if i%(p*q) != 0 {
|
||||
sieve[i] += sieve[q]
|
||||
}
|
||||
}
|
||||
|
||||
if p*q >= n {
|
||||
break
|
||||
}
|
||||
|
||||
if multiplicity {
|
||||
sieve[p*q] = 1 + sieve[q]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
PrimeOmega computes omega(k) for k=1 to n, where omega is the prime omega function. If multiplicity is true, factors are counted with multiplicity.
|
||||
PrimeOmegaSieve computes omega(k) for k=1 to n, where omega is the prime omega function. If multiplicity is true, factors are counted with multiplicity.
|
||||
*/
|
||||
func PrimeOmega(n uint, multiplicity bool, buflen uint) chan uint {
|
||||
func PrimeOmegaSieve(n uint, multiplicity bool, buflen uint) chan uint {
|
||||
sieve := make([]uint, n)
|
||||
for i := uint(0); i < n; i++ {
|
||||
sieve[i] = 0
|
||||
|
|
@ -41,11 +54,7 @@ func PrimeOmega(n uint, multiplicity bool, buflen uint) chan uint {
|
|||
}
|
||||
|
||||
sieve[i] = 1
|
||||
if multiplicity {
|
||||
updatePowers(sieve, i, n)
|
||||
}
|
||||
|
||||
updateMultiples(sieve, i, n, true)
|
||||
primeOmegaUpdateMultiples(sieve, i, n, multiplicity)
|
||||
ch <- sieve[i]
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -35,9 +35,9 @@ func radicalUpdateMultiples(sieve []uint, p uint, n uint) {
|
|||
}
|
||||
|
||||
/*
|
||||
Radical computes rad(k) for k=1 to n, where rad(n) is the radical of n.
|
||||
RadicalSieve computes rad(k) for k=1 to n, where rad(n) is the radical of n.
|
||||
*/
|
||||
func Radical(n uint, buflen uint) chan uint {
|
||||
func RadicalSieve(n uint, buflen uint) chan uint {
|
||||
sieve := make([]uint, n)
|
||||
sieve[0] = 0
|
||||
for i := uint(1); i < n; i++ {
|
||||
|
|
@ -54,12 +54,7 @@ func Radical(n uint, buflen uint) chan uint {
|
|||
}
|
||||
|
||||
sieve[i] = i
|
||||
for j := i; i*j < n; j *= i {
|
||||
// rad(p^k) = rad(p)
|
||||
sieve[i*j] = sieve[i]
|
||||
}
|
||||
|
||||
updateMultiples(sieve, i, n, false)
|
||||
radicalUpdateMultiples(sieve, i, n)
|
||||
ch <- sieve[i]
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -17,9 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
package sieve
|
||||
|
||||
/*
|
||||
Totient computes totient(k) for k=1 to n, where totient is Euler's totient function. buflen sets the buffer length of the returned channel. Larger buffer lengths can result in better performance at the cost of higher memory usage.
|
||||
TotientSieve computes totient(k) for k=1 to n, where totient is Euler's totient function. buflen sets the buffer length of the returned channel. Larger buffer lengths can result in better performance at the cost of higher memory usage.
|
||||
*/
|
||||
func Totient(n uint, buflen uint) chan uint {
|
||||
func TotientSieve(n uint, buflen uint) chan uint {
|
||||
totients := make([]uint, n)
|
||||
totients[0] = 0
|
||||
totients[1] = 1
|
||||
|
|
|
|||
Loading…
Reference in New Issue