Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to calculate the LCM (Least Common Multiple) of 3 numbers in Scala

2 Answers

0 votes
object LCMOfThreeNumbers_Scala {
  def gcd(a: Int, b: Int): Int = {
    if (b == 0) a else gcd(b, a % b)
  }

  def lcm(a: Int, b: Int): Int = {
    (a * b).abs / gcd(a, b)
  }

  def lcmOfThreeNumbers(a: Int, b: Int, c: Int): Int = {
    lcm(lcm(a, b), c)
  }

  def main(args: Array[String]): Unit = {
    val a = 12
    val b = 15
    val c = 40
    
    println(s"LCM of $a, $b, and $c is: " + lcmOfThreeNumbers(a, b, c))
  }
}



/*
run:

LCM of 12, 15, and 40 is: 120

*/

 



answered Oct 4, 2024 by avibootz
0 votes
def getLMC(a: Int, b: Int): Int = {
  var lmc = if (a > b) a else b

  var calc = 1
  while (calc == 1) {
    if (lmc % a == 0 && lmc % b == 0) {
        calc = 0
    }
    lmc += 1
  }
  
  return lmc - 1
}

val a = 12
val b = 15
val c = 40

println(s"The LCM (Least Common Multiple) is: ${getLMC(getLMC(a, b), c)}")



/*
run:

LCM of 12, 15, and 40 is: 120

*/

 



answered Oct 4, 2024 by avibootz

Related questions

...