euclidean algorithm


See On Github

Data

Tags

Source Code




class EuclideanAlgorithm {

    def calculateGCD(number1:Int, number2:Int): Int = {

      if(number2 == 0) number1
      else
        calculateGCD(number2, number1%number2)
    }
}



import org.scalatest.{Matchers, FunSuite}

class EuclideanAlgorithmTest extends FunSuite with Matchers {

  test("Euclidean Algorithm should give") {
    val objectForEuclideanAlgorithm = new EuclideanAlgorithm

    objectForEuclideanAlgorithm.calculateGCD(12,4) should be(4)
    objectForEuclideanAlgorithm.calculateGCD(8,2) should be(2)
    objectForEuclideanAlgorithm.calculateGCD(16,8) should be(8)
  }
}