Compare commits
4 Commits
v0.1.0
...
afd8fe57da
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
afd8fe57da | ||
|
|
592fadb6d7 | ||
|
|
0dd446fe74 | ||
|
|
6081757183 |
75
cmd/quadraticResidues.go
Normal file
75
cmd/quadraticResidues.go
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
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 (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"scm.dairydemon.net/filifa/mathtools/internal/lib/sieve"
|
||||
)
|
||||
|
||||
var quadraticResiduesN uint
|
||||
var quadraticResiduesCoprime bool
|
||||
|
||||
func quadraticResidues(cmd *cobra.Command, args []string) {
|
||||
bufStdout := bufio.NewWriter(os.Stdout)
|
||||
defer bufStdout.Flush()
|
||||
|
||||
ch := sieve.QuadraticResidues(quadraticResiduesN, quadraticResiduesCoprime, 1000)
|
||||
for i := 0; ; i++ {
|
||||
v, ok := <-ch
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Fprintln(bufStdout, v)
|
||||
}
|
||||
}
|
||||
|
||||
// quadraticResiduesCmd represents the quadraticResidues command
|
||||
var quadraticResiduesCmd = &cobra.Command{
|
||||
Use: "quadratic-residues",
|
||||
Short: "Compute the number of quadratic residues for all moduli less than n",
|
||||
Long: `Compute the number of quadratic residues for all moduli less than n.`,
|
||||
Run: quadraticResidues,
|
||||
}
|
||||
|
||||
func init() {
|
||||
sieveCmd.AddCommand(quadraticResiduesCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// quadraticResiduesCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// quadraticResiduesCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
|
||||
quadraticResiduesCmd.Flags().UintVarP(&quadraticResiduesN, "limit", "n", 0, "upper limit")
|
||||
quadraticResiduesCmd.MarkFlagRequired("limit")
|
||||
|
||||
quadraticResiduesCmd.Flags().BoolVarP(&quadraticResiduesCoprime, "coprime-only", "c", false, "only count residues coprime to the modulus")
|
||||
}
|
||||
131
internal/lib/sieve/qresidue.go
Normal file
131
internal/lib/sieve/qresidue.go
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
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
|
||||
|
||||
// NOTE: these formulas come from https://web.archive.org/web/20151224013638/http://www.maa.org/sites/default/files/Walter_D22068._Stangl.pdf
|
||||
|
||||
func qrPowersOfTwoCoprime(sieve []uint, n uint) {
|
||||
for q := uint(8); 2*q < n; q *= 2 {
|
||||
// q(2^n) = 2^(n-3) for n >= 3
|
||||
sieve[2*q] = 2 * sieve[q]
|
||||
}
|
||||
}
|
||||
|
||||
func qrPowersOfOddPrimesCoprime(sieve []uint, p uint, n uint) {
|
||||
for q := p; p*q < n; q *= p {
|
||||
// q(p^n) = (p^n - p^(n-1)) / 2
|
||||
sieve[p*q] = (p*q - q) / 2
|
||||
}
|
||||
}
|
||||
|
||||
func qrPowersOfTwo(sieve []uint, n uint) {
|
||||
k := 1
|
||||
for q := uint(1); 2*q < n; q *= 2 {
|
||||
if k%2 == 0 {
|
||||
// s(2^n) = (2^(n-1) + 4) / 3 for even n
|
||||
sieve[2*q] = (q + 4) / 3
|
||||
} else {
|
||||
// s(2^n) = (2^(n-1) + 5) / 3 for odd n
|
||||
sieve[2*q] = (q + 5) / 3
|
||||
}
|
||||
|
||||
k += 1
|
||||
}
|
||||
}
|
||||
|
||||
func qrPowersOfOddPrimes(sieve []uint, p uint, n uint) {
|
||||
k := 3
|
||||
for q := p * p; p*q < n; q *= p {
|
||||
if k%2 == 0 {
|
||||
// s(p^n) = (p^(n+1) + p + 2) / (2*(p+1)) for even n
|
||||
sieve[p*q] = (p*p*q + p + 2) / (2 * (p + 1))
|
||||
} else {
|
||||
// s(p^n) = (p^(n+1) + 2*p + 1) / (2*(p+1)) for odd n
|
||||
sieve[p*q] = (p*p*q + 2*p + 1) / (2 * (p + 1))
|
||||
}
|
||||
|
||||
k += 1
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
QuadraticResidues computes the number of quadratic residues modulo k for k=1 to n.
|
||||
|
||||
see https://oeis.org/A000224 and https://oeis.org/A046073
|
||||
*/
|
||||
func QuadraticResidues(n uint, coprime bool, buflen uint) chan uint {
|
||||
sieve := make([]uint, n)
|
||||
for i := uint(0); i < n; i++ {
|
||||
sieve[i] = 1
|
||||
}
|
||||
|
||||
ch := make(chan uint, buflen)
|
||||
go func() {
|
||||
defer close(ch)
|
||||
if coprime {
|
||||
sieveQRCoprime(sieve, n, ch)
|
||||
} else {
|
||||
sieveQR(sieve, n, ch)
|
||||
}
|
||||
}()
|
||||
|
||||
return ch
|
||||
}
|
||||
|
||||
func sieveQRCoprime(sieve []uint, n uint, ch chan uint) {
|
||||
for i := uint(0); i < n; i++ {
|
||||
if i == 0 || i == 1 || i == 4 || i == 6 || i == 8 || i == 12 || i == 24 || sieve[i] != 1 {
|
||||
ch <- sieve[i]
|
||||
continue
|
||||
}
|
||||
|
||||
if i == 2 {
|
||||
qrPowersOfTwoCoprime(sieve, n)
|
||||
} else {
|
||||
// q(p) = (p - 1) / 2
|
||||
sieve[i] = (i - 1) / 2
|
||||
qrPowersOfOddPrimesCoprime(sieve, i, n)
|
||||
}
|
||||
|
||||
updateMultiples(sieve, i, n, false)
|
||||
ch <- sieve[i]
|
||||
}
|
||||
}
|
||||
|
||||
func sieveQR(sieve []uint, n uint, ch chan uint) {
|
||||
for i := uint(0); i < n; i++ {
|
||||
if i == 0 || i == 1 || sieve[i] != 1 {
|
||||
ch <- sieve[i]
|
||||
continue
|
||||
}
|
||||
|
||||
if i == 2 {
|
||||
qrPowersOfTwo(sieve, n)
|
||||
} else {
|
||||
// s(p) = (p + 1) / 2
|
||||
sieve[i] = (i + 1) / 2
|
||||
if i*i < n {
|
||||
// s(p^2) = (p^2 - p + 2) / 2
|
||||
sieve[i*i] = (i*i - i + 2) / 2
|
||||
}
|
||||
qrPowersOfOddPrimes(sieve, i, n)
|
||||
}
|
||||
|
||||
updateMultiples(sieve, i, n, false)
|
||||
ch <- sieve[i]
|
||||
}
|
||||
}
|
||||
@@ -16,24 +16,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package sieve
|
||||
|
||||
func radicalUpdateMultiples(sieve []uint, p uint, n uint) {
|
||||
for q := p; ; q *= p {
|
||||
// rad(a*b) = rad(a) * rad(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
|
||||
}
|
||||
|
||||
// rad(p^k) = rad(p)
|
||||
sieve[p*q] = sieve[q]
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Radical computes rad(k) for k=1 to n, where rad(n) is the radical of n.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user