Sha256: 08d3a317b23cb69812aa524a30768f7408fdfdf59da8cc3a7f50c87a1a8481e9
Contents?: true
Size: 696 Bytes
Versions: 396
Compression:
Stored size: 696 Bytes
Contents
object PythagoreanTriplet { private def sqr(x: Int) = x * x private def sort(triplet: (Int, Int, Int)): (Int, Int, Int) = triplet match { case (a, b, c) if b < a => sort((b, a, c)) case (a, b, c) if c < b => sort((a, c, b)) case (a, b, c) => triplet } def isPythagorean(triplet: (Int, Int, Int)): Boolean = { val (a, b, c) = sort(triplet) sqr(a) + sqr(b) == sqr(c) } def pythagoreanTriplets(minFactor: Int, maxFactor:Int): Seq[(Int, Int, Int)] = for {a <- minFactor to maxFactor b <- a to maxFactor c2 = sqr(a) + sqr(b) c = Math.sqrt(c2).asInstanceOf[Int] if c <= maxFactor && c2 == sqr(c)} yield (a, b, c) }
Version data entries
396 entries across 396 versions & 1 rubygems