diff --git a/cmd/divisors.go b/cmd/divisors.go
index 9b0873f..06c255e 100644
--- a/cmd/divisors.go
+++ b/cmd/divisors.go
@@ -17,7 +17,9 @@ along with this program. If not, see .
package cmd
import (
+ "bufio"
"fmt"
+ "os"
"github.com/spf13/cobra"
"scm.dairydemon.net/filifa/mathtools/internal/lib"
@@ -27,7 +29,10 @@ var divisorsN uint
var divisorsE uint
func divisors(cmd *cobra.Command, args []string) {
- ch := lib.DivisorsSieve(divisorsN, divisorsE)
+ bufStdout := bufio.NewWriter(os.Stdout)
+ defer bufStdout.Flush()
+
+ ch := lib.DivisorsSieve(divisorsN, divisorsE, 1000)
for i := 0; ; i++ {
v, ok := <-ch
if !ok {
@@ -38,7 +43,7 @@ func divisors(cmd *cobra.Command, args []string) {
continue
}
- fmt.Println(v)
+ fmt.Fprintln(bufStdout, v)
}
}
diff --git a/cmd/mobius.go b/cmd/mobius.go
index 20798b0..4bd7bdd 100644
--- a/cmd/mobius.go
+++ b/cmd/mobius.go
@@ -17,7 +17,9 @@ along with this program. If not, see .
package cmd
import (
+ "bufio"
"fmt"
+ "os"
"github.com/spf13/cobra"
"scm.dairydemon.net/filifa/mathtools/internal/lib"
@@ -26,7 +28,10 @@ import (
var mobiusN uint
func mobius(cmd *cobra.Command, args []string) {
- ch := lib.MobiusSieve(mobiusN)
+ bufStdout := bufio.NewWriter(os.Stdout)
+ defer bufStdout.Flush()
+
+ ch := lib.MobiusSieve(mobiusN, 1000)
for i := 0; ; i++ {
v, ok := <-ch
if !ok {
@@ -37,7 +42,7 @@ func mobius(cmd *cobra.Command, args []string) {
continue
}
- fmt.Println(v)
+ fmt.Fprintln(bufStdout, v)
}
}
diff --git a/cmd/primeOmega.go b/cmd/primeOmega.go
index 9c7456c..3a76e46 100644
--- a/cmd/primeOmega.go
+++ b/cmd/primeOmega.go
@@ -17,7 +17,9 @@ along with this program. If not, see .
package cmd
import (
+ "bufio"
"fmt"
+ "os"
"github.com/spf13/cobra"
"scm.dairydemon.net/filifa/mathtools/internal/lib"
@@ -27,7 +29,10 @@ var primeOmegaN uint
var primeOmegaMul bool
func primeOmega(cmd *cobra.Command, args []string) {
- ch := lib.PrimeOmegaSieve(primeOmegaN, primeOmegaMul)
+ bufStdout := bufio.NewWriter(os.Stdout)
+ defer bufStdout.Flush()
+
+ ch := lib.PrimeOmegaSieve(primeOmegaN, primeOmegaMul, 1000)
for i := 0; ; i++ {
v, ok := <-ch
if !ok {
@@ -38,7 +43,7 @@ func primeOmega(cmd *cobra.Command, args []string) {
continue
}
- fmt.Println(v)
+ fmt.Fprintln(bufStdout, v)
}
}
diff --git a/internal/lib/divisors.go b/internal/lib/divisors.go
index e9e7267..4bc5840 100644
--- a/internal/lib/divisors.go
+++ b/internal/lib/divisors.go
@@ -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
diff --git a/internal/lib/mobius.go b/internal/lib/mobius.go
index b3e7bb3..bf183b8 100644
--- a/internal/lib/mobius.go
+++ b/internal/lib/mobius.go
@@ -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
diff --git a/internal/lib/primeOmega.go b/internal/lib/primeOmega.go
index d22ca76..f3f7a63 100644
--- a/internal/lib/primeOmega.go
+++ b/internal/lib/primeOmega.go
@@ -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