From a65e54ff46e49df106985a25f2d81f5983bb17bf Mon Sep 17 00:00:00 2001 From: filifa Date: Fri, 5 Sep 2025 22:03:31 -0400 Subject: [PATCH] output solutions based on repetend period --- cmd/pell.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/cmd/pell.go b/cmd/pell.go index 8eeb85e..7630aed 100644 --- a/cmd/pell.go +++ b/cmd/pell.go @@ -26,16 +26,6 @@ import ( var pellCoeff string -func isPellSolution(h, k, d *big.Int) bool { - 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) - - return bar.Cmp(big.NewInt(1)) == 0 -} - func pell(cmd *cobra.Command, args []string) { d, ok := new(big.Int).SetString(pellCoeff, 10) if !ok { @@ -49,12 +39,20 @@ func pell(cmd *cobra.Command, args []string) { cobra.CheckErr(pellCoeff + " is a perfect square") } + r := len(repetend) + var period int + if r%2 == 0 { + period = r + } else { + period = 2 * r + } + hch := gaussianBrackets(a0, big.NewInt(1), cycle(repetend)) kch := gaussianBrackets(big.NewInt(1), big.NewInt(0), cycle(repetend)) - for { + for i := 1; true; i = (i + 1) % period { h, k := <-hch, <-kch - if isPellSolution(h, k, d) { + if i == period-1 { fmt.Println(h, k) } }