refactor checking pell solution

This commit is contained in:
filifa 2025-09-05 21:32:01 -04:00
parent d238fe196f
commit 675b98bf17
1 changed files with 11 additions and 7 deletions

View File

@ -26,6 +26,16 @@ import (
var pellCoeff string 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) { func pell(cmd *cobra.Command, args []string) {
d, ok := new(big.Int).SetString(pellCoeff, 10) d, ok := new(big.Int).SetString(pellCoeff, 10)
if !ok { if !ok {
@ -44,13 +54,7 @@ func pell(cmd *cobra.Command, args []string) {
for { for {
h, k := <-hch, <-kch h, k := <-hch, <-kch
foo := new(big.Int).Exp(k, big.NewInt(2), nil) if isPellSolution(h, k, d) {
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) fmt.Println(h, k)
} }
} }