create internal library

This commit is contained in:
filifa
2025-08-21 18:58:43 -04:00
parent 37f40b25b4
commit 8e71cc87e5
2 changed files with 40 additions and 34 deletions

38
internal/lib/lib.go Normal file
View File

@@ -0,0 +1,38 @@
package lib
import (
"errors"
"math/big"
)
func SqrtRepetend(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
}