Skip to content

Instantly share code, notes, and snippets.

@albertywu
Created March 4, 2017 02:22
Show Gist options
  • Select an option

  • Save albertywu/e4b388711042e4325c5a5559f535735d to your computer and use it in GitHub Desktop.

Select an option

Save albertywu/e4b388711042e4325c5a5559f535735d to your computer and use it in GitHub Desktop.
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