output solutions based on repetend period
This commit is contained in:
parent
675b98bf17
commit
a65e54ff46
22
cmd/pell.go
22
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue