make same performance improvements to all sieve commands
This commit is contained in:
@@ -51,15 +51,16 @@ func updateMultiples(sieve []uint, x uint, p uint, n uint) {
|
||||
/*
|
||||
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) 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++ {
|
||||
sieve[i] = 1
|
||||
}
|
||||
|
||||
ch := make(chan uint)
|
||||
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]
|
||||
@@ -70,8 +71,6 @@ func DivisorsSieve(n uint, x uint) chan uint {
|
||||
updateMultiples(sieve, x, i, n)
|
||||
ch <- sieve[i]
|
||||
}
|
||||
|
||||
close(ch)
|
||||
}()
|
||||
|
||||
return ch
|
||||
|
||||
@@ -19,14 +19,15 @@ package lib
|
||||
/*
|
||||
MobiusSieve computes mobius(k) for k=1 to n, where mobius is the Mobius function.
|
||||
*/
|
||||
func MobiusSieve(n 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
|
||||
}
|
||||
|
||||
ch := make(chan int)
|
||||
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]
|
||||
@@ -43,8 +44,6 @@ func MobiusSieve(n uint) chan int {
|
||||
sieve[j] /= -i
|
||||
}
|
||||
}
|
||||
|
||||
close(ch)
|
||||
}()
|
||||
|
||||
return ch
|
||||
|
||||
@@ -38,14 +38,15 @@ func primeOmegaUpdateMultiples(sieve []uint, p uint, n uint, multiplicity bool)
|
||||
/*
|
||||
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) 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
|
||||
}
|
||||
|
||||
ch := make(chan uint)
|
||||
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]
|
||||
@@ -56,8 +57,6 @@ func PrimeOmegaSieve(n uint, multiplicity bool) chan uint {
|
||||
primeOmegaUpdateMultiples(sieve, i, n, multiplicity)
|
||||
ch <- sieve[i]
|
||||
}
|
||||
|
||||
close(ch)
|
||||
}()
|
||||
|
||||
return ch
|
||||
|
||||
Reference in New Issue
Block a user