Last active
December 11, 2015 09:09
-
-
Save zerosum/4578404 to your computer and use it in GitHub Desktop.
Project Euler: Problem 9
This file contains 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
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