94 lines
2.3 KiB
Go
94 lines
2.3 KiB
Go
/*
|
|
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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"math/big"
|
|
|
|
"github.com/spf13/cobra"
|
|
"scm.dairydemon.net/filifa/mathtools/internal/lib"
|
|
)
|
|
|
|
var pellCoeff string
|
|
|
|
func pell(cmd *cobra.Command, args []string) {
|
|
d, ok := new(big.Int).SetString(pellCoeff, 10)
|
|
if !ok {
|
|
cobra.CheckErr("invalid input " + args[0])
|
|
}
|
|
|
|
a0 := new(big.Int).Sqrt(d)
|
|
|
|
repetend, err := lib.SqrtRepetend(d)
|
|
if err != nil {
|
|
cobra.CheckErr(pellCoeff + " is a perfect square")
|
|
}
|
|
|
|
hprev := big.NewInt(0)
|
|
kprev := big.NewInt(1)
|
|
|
|
h := big.NewInt(1)
|
|
k := big.NewInt(0)
|
|
|
|
seq(h, hprev, a0)
|
|
seq(k, kprev, a0)
|
|
|
|
for i := 0; true; i = (i + 1) % len(repetend) {
|
|
foo := new(big.Int).Exp(k, big.NewInt(2), nil)
|
|
foo.Mul(d, foo)
|
|
|
|
bar := new(big.Int).Exp(h, big.NewInt(2), nil)
|
|
bar.Sub(bar, foo)
|
|
|
|
if bar.Cmp(big.NewInt(1)) == 0 {
|
|
fmt.Println(h, k)
|
|
}
|
|
|
|
a := repetend[i]
|
|
seq(h, hprev, a)
|
|
seq(k, kprev, a)
|
|
}
|
|
}
|
|
|
|
// pellCmd represents the pell command
|
|
var pellCmd = &cobra.Command{
|
|
Use: "pell",
|
|
Short: "Find solutions to a Pell equation",
|
|
Long: `Find solutions to a Pell equation.`,
|
|
Run: pell,
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(pellCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// pellCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// pellCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
pellCmd.Flags().StringVarP(&pellCoeff, "coefficient", "d", "", "coefficient")
|
|
pellCmd.MarkFlagRequired("coefficient")
|
|
|
|
// TODO: add support for generalized Pell equations
|
|
}
|