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

Version Path
trackler-2.2.1.180 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.179 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.178 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.177 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.176 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.175 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.174 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.173 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.172 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.171 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.170 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.169 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.167 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.166 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.165 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.164 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.163 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.162 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.161 tracks/scala/exercises/pythagorean-triplet/example.scala
trackler-2.2.1.160 tracks/scala/exercises/pythagorean-triplet/example.scala