Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //FindSecretLinearMultiEquation finds the secrets
- func FindSecretLinearMultiEquation(x []int, y []int64, degree int) int {
- if len(x) < degree+1 || len(y) < degree+1 {
- return -1
- }
- var matrix [][]int64
- var foundSeed int
- for i := 0; i < degree+1; i++ {
- var vector []int64
- for j := 0; j < degree; j++ {
- vector = append(vector, int64(math.Pow(float64(x[i]), float64(j+1))))
- }
- vector = append(vector, 1)
- vector = append(vector, y[i])
- matrix = append(matrix, vector)
- }
- //printMatrix(matrix)
- transformMatrix(matrix)
- lastRow := matrix[len(matrix)-1]
- length := len(lastRow)
- printMatrix(matrix)
- foundSeed = int(lastRow[length-1] / lastRow[length-2])
- return foundSeed
- }
- func transformMatrix(matrix [][]int64) {
- for i := 0; i < len(matrix)-1; i++ {
- for k := i + 1; k < len(matrix); k++ {
- transformRow(matrix[i], matrix[k], i)
- }
- }
- }
- func transformRow(v1 []int64, v2 []int64, round int) {
- if v1[round] == 0 {
- SwapVector(v1, v2)
- } else if v2[round] != 0 {
- lcm := calculateLCM(
- int64(math.Abs(float64(v1[round]))),
- int64(math.Abs(float64(v2[round]))))
- timesV1 := lcm / v1[round]
- timesV2 := lcm / v2[round]
- AlgebraVector(v1, v2, "-", timesV1, timesV2)
- }
- }
- func multiply(v []int64, a int64) {
- for i := 0; i < len(v); i++ {
- v[i] = a * v[i]
- }
- }
Add Comment
Please, Sign In to add comment