https://github.com/txaty/lfs
Blazingly fast Lagrange Four Square Sum Solver.
https://github.com/txaty/lfs
biginteger blazingly-fast four-square four-square-sum go golang lagrange-four-square lagrange-four-square-sum
Last synced: 4 days ago
JSON representation
Blazingly fast Lagrange Four Square Sum Solver.
- Host: GitHub
- URL: https://github.com/txaty/lfs
- Owner: txaty
- License: mit
- Created: 2022-09-21T08:38:48.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-11T16:26:33.000Z (about 1 year ago)
- Last Synced: 2025-12-19T00:34:18.917Z (3 months ago)
- Topics: biginteger, blazingly-fast, four-square, four-square-sum, go, golang, lagrange-four-square, lagrange-four-square-sum
- Language: Go
- Homepage:
- Size: 43 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LFS - Lagrange Four-Squares
[](https://pkg.go.dev/github.com/txaty/lfs)
[](https://goreportcard.com/report/github.com/txaty/lfs)
[](https://codecov.io/github/txaty/lfs)
[](https://app.codacy.com/gh/txaty/lfs/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[](https://opensource.org/licenses/MIT)
LFS is a Go package that implements an optimized algorithm for solving the Lagrange four-squares problem, even for very large integers.
It computes a representation of any positive integer `n` as the sum of four squares:
$$
n = {w_0}^2 + {w_1}^2 + {w_2}^2 + {w_3}^2
$$
This implementation is highly optimized for very large integers (e.g., 1000 bits, 2000 bits, or more).
The underlying algorithms are based on Section 3 of the paper [Finding the Four Squares in Lagrange's Theorem](https://campus.lakeforest.edu/trevino/finding4squares.pdf) with additional improvements that significantly enhance performance.
## Usage
Below is a simple example:
```go
package main
import (
"crypto/rand"
"fmt"
"log"
"math/big"
"github.com/txaty/lfs"
)
func main() {
// Create a new solver with default options.
solver := lfs.NewSolver()
// Define a large integer.
limit := new(big.Int).Lsh(big.NewInt(1), 1200)
n, err := rand.Int(rand.Reader, limit)
if err != nil {
log.Fatal(err)
}
// Compute the four-square representation.
result := solver.Solve(n)
// Display the result.
fmt.Printf("Representation of n as sum of four squares: %s\n", result)
// Verify the representation.
if lfs.Verify(n, result) {
fmt.Println("Verification succeeded: The squares sum to n.")
} else {
log.Fatal("Verification failed: The computed squares do not sum to n.")
}
}
```
## Configuration Options
The solver is configurable via functional options when creating a new instance. For example:
- **WithFCMThreshold**: Sets the threshold above which the advanced FCM algorithm is used.
Example:
```go
solver := lfs.NewSolver(
lfs.WithFCMThreshold(new(big.Int).Lsh(big1, 600)), // Use FCM for numbers ≥ 2^600
)
```
- **WithNumRoutines**: Sets the number of concurrent goroutines for the randomized search.
Example:
```go
solver := lfs.NewSolver(
lfs.WithNumRoutines(8), // Use 8 goroutines for parallel computation
)
```
## Dependencies
This project requires the following dependencies:
- [txaty/go-bigcomplex](https://github.com/txaty/go-bigcomplex): A big complex number library supporting big Gaussian
and Hurwitz integers.
- [lukechampine.com/frand](https://github.com/lukechampine/frand): A fast randomness generation library.
## License
This project is released under the MIT License.