How to find the LCM of two given numbers using Recursion in Golang?


In this tutorial, we are going to find the Least common multiple of two numbers in Golang using recursion. To find the LCM recursively we are going to use the relation of LCM with the Greatest common divisible i.e GCD of both the numbers. LCM stands for least common multiple is the smallest number divisible by two numbers.

For example,

Suppose the two numbers are 10 and 4. The smallest number that is divisible by both numbers evenly is 20.

Finding LCM Using the Relation between LCM and GCD

In this example, we are going to find the LCM using the relation between LCM and GCD of two numbers.

Explanation

The relationship between LCM and GCD is
LCM = (number1 * number2) / GCD
number1 = 20
Number2 = 15
GCD = 5
LCM = ( 20 * 15 ) / 5
   = 300 / 5
   = 60

Algorithm

  • Step 1 - var number1, number2, gcd, minNumber int - In this line of code we are declaring two variables to store the numbers of which we have found the LCM, a variable to store the GCD of both numbers, and a variable to store the minimum of both numbers.

  • Step 2 - number1 = 20 number2 = 15 - Initialize both the numbers with the values you want to find LCM of.

  • Step 3 - if number1 < number2 { } - Finding minimum among the numbers and storing in minNumber variable.

  • Step 4 - gcdOfTwoNumbers(number1, number2, minNumber) - Call the recursive function to find the GCD of both numbers.

  • Step 5 - LCM:= (number1 * number2) / gcd - Finding LCM using the relation of GCD and LCM.

Example

package main

// fmt package provides the function to print anything
import (
   "fmt"
)

// this function is finding the GCD of two numbers with three parameters
// of int type and have a return type of int type
func gcdOfTwoNumbers(number1, number2, minNumber int) int {

   // checking if the number minNumber can divide both number1 and number2
   if minNumber == 1 || (number1%minNumber == 0 && number2%minNumber == 0) {
      return minNumber
   }

   // returning the GCD
   return gcdOfTwoNumbers(number1, number2, minNumber-1)
}
func main() {

   // declaring the variable to store the value of two numbers
   // and a variable to store an answer
   var number1, number2, gcd, minNumber int

   // initializing both the variables
   number1 = 20
   number2 = 15
   fmt.Println("Program to find the LCM of two numbers using the relation with GCD using recursion.")
   if number1 < number2 {
      minNumber = number1
   } else {
      minNumber = number2
   }

   // calling a function to find the GCD of two number
   // and passing a minimum of number1 and number2
   gcd = gcdOfTwoNumbers(number1, number2, minNumber)
   LCM := (number1 * number2) / gcd

   // printing the result
   fmt.Println("The LCM of", number1, "and", number2, "is", LCM)
}

Output

Program to find the LCM of two numbers using the relation with GCD using recursion.
The LCM of 20 and 15 is 60

Description

In this approach, we are first finding the minimum number and then finding the greatest common divisor of both the numbers recursively using which we find the LCM by applying the GCD and LCM relation.

Conclusion

This is the recursive method to find the LCM of two numbers using a relation with GCD in Golang. This is one of the efficient approaches to finding LCM. To learn more about go you can explore these tutorials.

Updated on: 11-Jan-2023

466 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements