Created
March 4, 2017 02:22
-
-
Save albertywu/e4b388711042e4325c5a5559f535735d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class IntWithPow(val i: Int) { | |
| def pow(n: Int) = Math.pow(i, n).toInt | |
| } | |
| implicit def intToInt(i: Int) = new IntWithPow(i) | |
| // z = ax + by + c | |
| type LinSolution = (Int, Int, Int) | |
| val testRange = 0 to 5 | |
| // given (x, y, z), find a solution | |
| def solveLin(x: Int, y: Int, z: Int): Set[LinSolution] = { | |
| for { | |
| a <- testRange | |
| b <- testRange | |
| c <- testRange | |
| if a*x + b*y + c == z | |
| } yield (a, b, c) | |
| }.toSet | |
| solveLin(1, 1, 2) | |
| // z = a*x^b + c*y^d + e*x^f*y^g + h | |
| type QuadSolution = (Int, Int, Int, Int, Int, Int, Int, Int) | |
| def solveQuad(x: Int, y: Int, z: Int): Set[QuadSolution] = { | |
| for { | |
| a <- testRange | |
| b <- testRange | |
| c <- testRange | |
| d <- testRange | |
| e <- testRange | |
| f <- testRange | |
| g <- testRange | |
| h <- testRange | |
| if a*x.pow(b) + c*y.pow(d) + e*x.pow(f)*y.pow(g) + h == z | |
| } yield (a, b, c, d, e, f, g, h) | |
| }.toSet | |
| //2^2 + 3^2 = 13 | |
| solveQuad(2, 3, 13) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment