- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Haskell Program to find the LCM of two given numbers using recursion
- How to find the GCD of Two given numbers using Recursion in Golang?
- Golang Program to Find the Product of Two Numbers Using Recursion
- Haskell Program to find the GCD of two given numbers using recursion
- Swift program to find the GCD of two given numbers using recursion
- How to find the Reverse of a given number using Recursion in Golang?
- Golang Program to Find the Sum of Natural Numbers using Recursion
- Golang Program to Find the Sum of N Numbers using Recursion
- Python Program to Find the Product of two Numbers Using Recursion
- Java Program to Find the Product of Two Numbers Using Recursion
- Haskell Program to Find the Product of Two Numbers Using Recursion
- C++ Program to Find the Product of Two Numbers Using Recursion
- Find LCM of two numbers
- What is LCM and how do we find the LCM of given numbers?
- Write a Golang program to find the factorial of a given number (Using Recursion)
