🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Pythagorean Triplet in the Scala Track

Published at Aug 24 2019 · 0 comments
Instructions
Test suite
Solution

A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for which,

a**2 + b**2 = c**2

For example,

3**2 + 4**2 = 9 + 16 = 25 = 5**2.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product a * b * c.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

Source

Problem 9 at Project Euler http://projecteuler.net/problem=9

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

PythagoreanTripletTest.scala

import org.scalatest.{FunSuite, Matchers}

/** @version created manually **/
class PythagoreanTripletTest extends FunSuite with Matchers {
  test("isPythagorean") {
    PythagoreanTriplet.isPythagorean((3, 4, 5)) should be (true)
    PythagoreanTriplet.isPythagorean((3, 5, 4)) should be (true)
    PythagoreanTriplet.isPythagorean((4, 3, 5)) should be (true)
    PythagoreanTriplet.isPythagorean((4, 5, 3)) should be (true)
    PythagoreanTriplet.isPythagorean((5, 3, 4)) should be (true)
    PythagoreanTriplet.isPythagorean((5, 4, 3)) should be (true)
    PythagoreanTriplet.isPythagorean((3, 3, 3)) should be (false)
    PythagoreanTriplet.isPythagorean((5, 6, 7)) should be (false)
  }

  test("pythagoreanTriplets 1 to 10") {
    pending
    PythagoreanTriplet.pythagoreanTriplets(1, 10) should be (Seq((3, 4, 5), (6, 8, 10)))
  }

  test("pythagoreanTriplets 11 to 20") {
    pending
    PythagoreanTriplet.pythagoreanTriplets(11, 20) should be (Seq((12, 16, 20)))
  }

  test("pythagoreanTriplets 56 to 95") {
    pending
    PythagoreanTriplet.pythagoreanTriplets(56, 95) should be (Seq((57, 76, 95), (60, 63, 87)))
  }
}
object PythagoreanTriplet {
  def pythagoreanTriplets(min: Int, max: Int): Seq[(Int, Int, Int)] =
    (min to max).combinations(3).toList.map(l => (l(0), l(1), l(2))).filter(isPythagorean)

  def isPythagorean(trip: (Int, Int, Int)): Boolean = {
    //    val list = trip.productIterator.toList.map(x => Math.pow(x.toString.toInt, 2)).sorted
    //    list(0) + list(1) == list(2)
    val a :: b :: c :: Nil = trip.productIterator.toList.map(x => Math.pow(x.toString.toInt, 2)).sorted
    a + b == c
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?