move functions to lib

This commit is contained in:
filifa
2025-09-06 00:19:56 -04:00
parent b953ade1a1
commit 58d9185296
3 changed files with 63 additions and 61 deletions

View File

@@ -36,3 +36,62 @@ func SqrtRepetend(x *big.Int) ([]*big.Int, error) {
return repetend, nil
}
func cycle(seq []*big.Int) <-chan *big.Int {
ch := make(chan *big.Int)
n := len(seq)
go func() {
for i := 0; true; i = (i + 1) % n {
ch <- seq[i]
}
}()
return ch
}
func GaussianBrackets(ch <-chan *big.Int) <-chan *big.Int {
out := make(chan *big.Int)
xprev := big.NewInt(0)
x := big.NewInt(1)
go func() {
tmp := new(big.Int)
for a := range ch {
out <- x
tmp.Mul(a, x)
tmp.Add(tmp, xprev)
xprev.Set(x)
x = new(big.Int).Set(tmp)
}
}()
return out
}
func CFracConvergents(a0 *big.Int, denoms []*big.Int) <-chan *big.Rat {
hc := cycle(denoms)
_ = <-hc
hch := GaussianBrackets(hc)
kc := cycle(denoms)
kch := GaussianBrackets(kc)
_ = <-kch
a := new(big.Rat).SetInt(a0)
out := make(chan *big.Rat)
go func() {
for {
h, k := <-hch, <-kch
r := new(big.Rat).SetFrac(h, k)
r.Add(r, a)
out <- r
}
}()
return out
}