From c8617da8e134cc1e1065ec03f00316af42e2b9bb Mon Sep 17 00:00:00 2001 From: filifa Date: Tue, 12 Aug 2025 21:27:37 -0400 Subject: [PATCH] add sqrtRepetend command --- cmd/sqrtRepetend.go | 105 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 cmd/sqrtRepetend.go diff --git a/cmd/sqrtRepetend.go b/cmd/sqrtRepetend.go new file mode 100644 index 0000000..d6292e8 --- /dev/null +++ b/cmd/sqrtRepetend.go @@ -0,0 +1,105 @@ +/* +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 ( + "errors" + "fmt" + "log" + "math/big" + + "github.com/spf13/cobra" +) + +func computeSqrtRepetend(x *big.Int) ([]*big.Int, error) { + m := big.NewInt(0) + d := big.NewInt(1) + a0 := new(big.Int).Sqrt(x) + + s := new(big.Int).Exp(a0, big.NewInt(2), nil) + if x.Cmp(s) == 0 { + return nil, errors.New("input is a perfect square") + } + + repetend := make([]*big.Int, 0) + + a := new(big.Int).Set(a0) + twoa0 := new(big.Int).Mul(big.NewInt(2), a0) + for a.Cmp(twoa0) != 0 { + // m = d * a - m + tmp := new(big.Int) + m.Sub(tmp.Mul(d, a), m) + + // d = (x - m^2) // d + tmp.Exp(m, big.NewInt(2), nil) + d.Div(tmp.Sub(x, tmp), d) + + // a = (a0 + m) // d + a.Div(tmp.Add(a0, m), d) + + repetend = append(repetend, new(big.Int).Set(a)) + } + + return repetend, nil +} + +func sqrtRepetend(cmd *cobra.Command, args []string) { + x := new(big.Int) + + for _, s := range args { + _, ok := x.SetString(s, 10) + if !ok { + log.Print("invalid input " + s) + continue + } + + repetend, err := computeSqrtRepetend(x) + if err != nil { + log.Print(s + " is a perfect square") + continue + } + + fmt.Print(repetend[0]) + for _, r := range repetend[1:] { + fmt.Printf(" %s", r) + } + fmt.Println() + } +} + +// sqrtRepetendCmd represents the sqrtRepetend command +var sqrtRepetendCmd = &cobra.Command{ + Use: "sqrt-repetend N [N ...]", + Short: "Compute the repetend of the square root of the input", + Long: `Compute the repetend of the square root of the input.`, + Args: cobra.MinimumNArgs(1), + Run: sqrtRepetend, +} + +func init() { + rootCmd.AddCommand(sqrtRepetendCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // sqrtRepetendCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // sqrtRepetendCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +}