euclidean algorithm


See On Github

Data

Tags

Source Code

import scala.annotation.tailrec

object euclidean_algorithm {

  @tailrec
  def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
}
import org.scalatest.{ Matchers, FlatSpec }
import euclidean_algorithm._

class euclidean_algorithm_test extends FlatSpec with Matchers {
  "The greatest common denominator of 210 and 45" should "be 15" in {
    val a = 210
    val b = 45

    gcd(a, b) shouldBe 15
  }
}