-
-
Save Kuchitama/4578521 to your computer and use it in GitHub Desktop.
Other patterns of [zerosum's answer](https://gist.github.com/4578404)
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
/** try to use Tuple */ | |
object Euler0009 { | |
private val max = 1000 | |
def main(args: Array[String]) { | |
println( | |
triplet(1, 2).filter{ case(a, b, c) => a*a + b*b == c*c} | |
.map{case (a, b, c) => a * b * c} | |
) | |
} | |
def triplet(a: Int, b: Int, triplets: List[(Int, Int, Int)] = Nil): List[(Int, Int, Int)] = { | |
val c = max - a - b | |
if(a > max/3) { | |
triplets | |
} else if(b < c) { | |
triplet(a, b+1, (a, b, c) :: triplets) | |
} else { | |
triplet(a+1, a+2, triplets) | |
} | |
} | |
} |
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
/** try to use pattern match */ | |
object Euler0009 { | |
private val max = 1000 | |
def main(args: Array[String]): Unit = { | |
println( | |
triplet(1, 2).filter{case a :: b :: c :: Nil => a*a + b*b == c*c} | |
.map(_.product) | |
) | |
} | |
def triplet(a: Int, b: Int, triplets: List[List[Int]] = Nil): List[List[Int]] = { | |
val c = max - a - b | |
if(a > max/3) { | |
triplets | |
} else if(b < c) { | |
triplet(a, b+1, List(a, b, c) :: triplets) | |
} else { | |
triplet(a+1, a+2, triplets) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment