Skip to content

Instantly share code, notes, and snippets.

@zerosum
Last active December 11, 2015 09:09
Show Gist options
  • Save zerosum/4578404 to your computer and use it in GitHub Desktop.
Save zerosum/4578404 to your computer and use it in GitHub Desktop.
Project Euler: Problem 9
object Euler0009 {
private val max = 1000
def main(args: Array[String]): Unit = {
println(
buildTriplets(1, 2, Nil).filter(_.isPitagolyan).map(_.product)
)
}
private class Triplet(val a: Int, val b: Int, val c: Int) {
def isPitagolyan(): Boolean = {
a*a + b*b == c*c
}
def product() : Int = {
a * b * c
}
}
private def buildTriplets(a: Int, b: Int, triplets: List[Triplet]): List[Triplet] = {
val c = max - a - b
if(a > max/3) {
triplets
} else if(b < c) {
buildTriplets(a, b+1, new Triplet(a, b, c) :: triplets)
} else {
buildTriplets(a+1, a+2, triplets)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment