
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 Haskell
In Haskell, we can find the the GCD of two given numbers by using recursion along with gcd function and tail-recursion. In the first and second examples, we are going to use base case, (gcd a 0 = a) and recursive case, gcd a b = gcd b (a `mod` b)) and in the third example, we are going to use tail-recursive function.
In the following examples we define a gcd function that takes two Int arguments a and b. The function uses pattern matching to handle two cases ?
If b is 0, the function returns a as the GCD of a and 0 is a.
If b is not 0, the function recursively calls itself with b and a modulo b as the arguments.
Algorithm
Step 1 ? The Prelude library is imported for hiding the gcd function.
Step 2 ? The user defined gcd function is defined with base and recursive case as,
For example 1 and 2 ?
gcd a 0 = a gcd a b = gcd b (a `mod` b).
For example 3 ?
| a < b = gcd b a | b == 0 = a | otherwise = gcd b (a `mod` b).
Step 3 ? Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, we define two variables a and b with the values 36 and 63, respectively. Finally, we print the result of gcd a b to the console.
Step 4 ? The variables named, "a" and "b" are being initialized. It will hold the numbers whose gcd is to be computed.
Step 5 ? The resultant gcd of the given two numbers is printed to the console using ?print' function after the function is called.
Example 1
In this example, the GCD of two numbers a and b is found using the gcd function to calculate the greatest common divisor of the two numbers.
import Prelude hiding(gcd) gcd :: Int -> Int -> Int gcd a 0 = a gcd a b = gcd b (a `mod` b) main :: IO () main = do let a = 36 let b = 63 print (gcd a b)
Output
9
Example 2
In this example, the subtraction and division operations are used to calculate the GCD. The idea behind this approach is to use the fact that gcd(a, b) = gcd(b, a - b * floor(a / b)) and keep subtracting b * floor(a / b) from a until b becomes 0, at which point a will be the GCD.
import Prelude hiding(gcd) gcd :: Int -> Int -> Int gcd a 0 = a gcd a b = gcd b (a - (a `div` b) * b) main :: IO () main = do let a = 36 let b = 63 print (gcd a b)
Output
9
Example 3
In this example, the GCD of two numbers a and b is found using the tail recursive approach of gcd function to calculate the greatest common divisor of the two numbers.
import Prelude hiding(gcd) gcd :: Int -> Int -> Int gcd a b | a < b = gcd b a | b == 0 = a | otherwise = gcd b (a `mod` b) main :: IO () main = do let a = 36 let b = 63 print (gcd a b)
Output
9
Conclusion
In Haskell, the gcd function is used to calculate the GCD of two numbers. There are several ways to implement the gcd function, and one of the most common approaches is to use the Euclidean algorithm, which states that the GCD of two numbers a and b is equal to the GCD of b and a modulo b. And it could also be implemented using recursion.