create internal library
This commit is contained in:
38
internal/lib/lib.go
Normal file
38
internal/lib/lib.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user