move sieves to subpackage

This commit is contained in:
filifa
2025-10-07 17:23:16 -04:00
parent 03e463b7a6
commit a9c7f8091b
10 changed files with 16 additions and 15 deletions

View File

@@ -0,0 +1,78 @@
/*
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 pow(base uint, exp uint) uint {
result := uint(1)
for exp > 0 {
if exp%2 == 1 {
result *= base
}
exp >>= 1
base *= base
}
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]
}
}
/*
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 DivisorsSieve(n uint, x uint, buflen uint) chan uint {
sieve := make([]uint, n)
sieve[0] = 0
for i := uint(1); i < n; i++ {
sieve[i] = 1
}
ch := make(chan uint, buflen)
go func() {
defer close(ch)
for i := uint(0); i < n; i++ {
if i == 0 || i == 1 || sieve[i] != 1 {
ch <- sieve[i]
continue
}
sieve[i] = pow(i, x) + 1
updateMultiples(sieve, x, i, n)
ch <- sieve[i]
}
}()
return ch
}

View File

@@ -0,0 +1,50 @@
/*
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
/*
MobiusSieve computes mobius(k) for k=1 to n, where mobius is the Mobius function.
*/
func MobiusSieve(n uint, buflen uint) chan int {
sieve := make([]int, n)
for i := 0; i < int(n); i++ {
sieve[i] = i
}
ch := make(chan int, buflen)
go func() {
defer close(ch)
for i := 0; i < int(n); i++ {
if i == 0 || i == 1 || sieve[i] != i {
ch <- sieve[i]
continue
}
ch <- -1
for j := 2 * i; j < int(n); j += i {
if j%(i*i) == 0 {
sieve[j] = 0
}
sieve[j] /= -i
}
}
}()
return ch
}

View File

@@ -0,0 +1,63 @@
/*
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 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]
}
}
}
/*
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 PrimeOmegaSieve(n uint, multiplicity bool, buflen uint) chan uint {
sieve := make([]uint, n)
for i := uint(0); i < n; i++ {
sieve[i] = 0
}
ch := make(chan uint, buflen)
go func() {
defer close(ch)
for i := uint(0); i < n; i++ {
if i == 0 || i == 1 || sieve[i] != 0 {
ch <- sieve[i]
continue
}
sieve[i] = 1
primeOmegaUpdateMultiples(sieve, i, n, multiplicity)
ch <- sieve[i]
}
}()
return ch
}

View File

@@ -0,0 +1,63 @@
/*
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 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]
}
}
/*
RadicalSieve computes rad(k) for k=1 to n, where rad(n) is the radical of n.
*/
func RadicalSieve(n uint, buflen uint) chan uint {
sieve := make([]uint, n)
sieve[0] = 0
for i := uint(1); i < n; i++ {
sieve[i] = 1
}
ch := make(chan uint, buflen)
go func() {
defer close(ch)
for i := uint(0); i < n; i++ {
if i == 0 || i == 1 || sieve[i] != 1 {
ch <- sieve[i]
continue
}
sieve[i] = i
radicalUpdateMultiples(sieve, i, n)
ch <- sieve[i]
}
}()
return ch
}

View File

@@ -0,0 +1,46 @@
/*
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
/*
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 TotientSieve(n uint, buflen uint) chan uint {
totients := make([]uint, n)
totients[0] = 0
totients[1] = 1
for i := uint(2); i < n; i++ {
totients[i] = i - 1
}
ch := make(chan uint, buflen)
go func() {
defer close(ch)
for i := uint(0); i < n; i++ {
ch <- totients[i]
if i == 0 || i == 1 || totients[i] != i-1 {
continue
}
for j := 2 * i; j < n; j += i {
totients[j] -= totients[j] / i
}
}
}()
return ch
}