make same performance improvements to all sieve commands

This commit is contained in:
filifa
2025-10-01 23:44:56 -04:00
parent 5f65b35b9c
commit 51ae15cedc
6 changed files with 30 additions and 18 deletions

View File

@@ -17,7 +17,9 @@ 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"
@@ -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)
}
}

View File

@@ -17,7 +17,9 @@ 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"
@@ -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)
}
}

View File

@@ -17,7 +17,9 @@ 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"
@@ -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)
}
}