
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find GCD of Two Numbers Using Recursion in Golang
In this tutorial, we will see how to find the Greatest common divisor of two numbers using the Golang language using recursion. We will see two ways to find the GCD of two numbers recursively. First will take more time where we are reducing the minimum of both numbers by 1 and then check if both the numbers are divisible by the min number or not. The second approach will take less time where we are subtracting the larger number from the smaller number until both numbers become equal.
Algorithm
Step 1 - Declaring the variables to store the two numbers and answers.
Step 2 - Initializing the variables.
Step 3 - Call the function to find the GCD with the minimum number that will reduce each function call, do mod with both numbers, and return if the mod is zero.
Step 4 - Printing the result.
Method 1: Non-Efficient Approach by using the Recursion function.
In this example, we are reducing the minimum of both numbers by 1 and then checking if both the numbers are divisible by the min number or not.
Example
package main // fmt package provides the function to print anything import ( "fmt" ) // this function finds 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 be divided by 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, answer, minNumber int // initializing both the variables number1 = 20 number2 = 15 fmt.Println("Program to find the GCD of two numbers using the recursion function.") 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 answer = gcdOfTwoNumbers(number1, number2, minNumber) // printing the result fmt.Println("The GCD of", number1, "and", number2, "is", answer) }
Output
Program to find the GCD of two numbers using the recursion function. The GCD of 20 and 15 is 5
Method 2: Efficient Approach by using the Recursion function
In this example, we will take less time by subtracting the larger number from the smaller number until both numbers become equal.
Example
package main // fmt package provides the function to print anything import ( "fmt" ) // this function finds the GCD of two numbers with two parameters // of int type and have a return type of int type func gcdOfTwoNumbers(number1, number2 int) int { // returning if both the numbers become equal if number1 == number2 { return number1 } // reducing the lesser one with the greater one if number1 > number2 { number1 -= number2 } else { number2 -= number1 } // calling the function return gcdOfTwoNumbers(number1, number2) } func main() { // declaring the variable to store the value of two numbers // and a variable to store an answer var number1, number2, answer int // initializing both the variables number1 = 20 number2 = 15 fmt.Println("Program to find the GCD of two numbers in efficient way using the recursion function.") // calling a function to find the GCD of two number answer = gcdOfTwoNumbers(number1, number2) // printing the result fmt.Println("The GCD of", number1, "and", number2, "is", answer) }
Output
Program to find the GCD of two numbers in an efficient way using the recursion function. The GCD of 20 and 15 is 5
Conclusion
These are the different ways to find the GCD of two numbers using recursion. The second approach is more efficient than the first one. To learn more about go you can explore these tutorials.