Skip to content

Instantly share code, notes, and snippets.

@daiksy
Created November 22, 2012 17:27
Show Gist options
  • Save daiksy/4132269 to your computer and use it in GitHub Desktop.
Save daiksy/4132269 to your computer and use it in GitHub Desktop.
Project Euler Problem 9
/**
* http://projecteuler.net/problem=9
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%209
*/
def triplesOfTotal1000: List[(Int, Int, Int)] = {
def search(a: Int, b: Int, c: Int, xs: List[(Int, Int, Int)]): List[(Int, Int, Int)] = {
val ys = if (a + b + c == 1000) (a, b, c) :: xs else xs
(a, b, c) match {
case (_, _, 997) => search(a, b + 1, b + 2, ys)
case (_, 499, _) => search(a + 1, a + 2, a + 3, ys)
case (333, _, _) => ys
case _ => search(a, b, c + 1, ys)
}
}
search(1, 2, 3, Nil)
}
import scala.math.pow
val t = triplesOfTotal1000.find {case (a, b, c) => pow(a, 2) + pow(b, 2) == pow(c, 2)}.get
val ret = t._1 * t._2 * t._3
println(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment