diff --git a/cmd/isprime.go b/cmd/isprime.go new file mode 100644 index 0000000..7392ead --- /dev/null +++ b/cmd/isprime.go @@ -0,0 +1,70 @@ +/* +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 . +*/ +package cmd + +import ( + "fmt" + "log" + "math/big" + + "github.com/spf13/cobra" +) + +var rounds uint + +func isprime(cmd *cobra.Command, args []string) { + x := new(big.Int) + + primes := make([]bool, len(args)) + for i, s := range args { + _, ok := x.SetString(s, 10) + if !ok { + log.Fatal("invalid input " + s) + } + + primes[i] = x.ProbablyPrime(int(rounds)) + } + + for _, b := range primes { + fmt.Println(b) + } +} + +// isprimeCmd represents the isprime command +var isprimeCmd = &cobra.Command{ + Use: "isprime", + Short: "Determine whether a number is prime", + Long: `Determine whether a number is prime.`, + Args: cobra.MinimumNArgs(1), + Run: isprime, +} + +func init() { + rootCmd.AddCommand(isprimeCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // isprimeCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // isprimeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + + isprimeCmd.Flags().UintVarP(&rounds, "rounds", "r", 10, "number of iterations") +}