Haskell Program to Find G.C.D Using Recursion


In Haskell,we can find G.C.D by using recursion and also by using recursive case statements. In the first example we are going to use recursion along with base and recursive case and in the second example, we are going to use (case (x, y) of) statement.

Algorithm

  • Step 1 − The user defined recursive gcd’ function is defined as,

  • For example 1 −

gcd' x y
   | y == 0    = x
   | otherwise = gcd' y (x `mod` y).
  • For example 2 −

gcd' x y = case (x, y) of
   (x, 0) -> x
   (x, y) -> gcd' y (x `mod` y).
  • Step 2 − 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, the program sets x and y to the numbers whose GCD is to be found and then calls the gcd' function with these numbers as arguments. The result is then printed to the console.

  • Step 3 − The variables named, “x” and “y” are being initialized. It will hold the numbers whose gcd is to be computed.

  • Step 4 − The resultant gcd value is printed to the console using ‘print’’ function, after the function is called.

Example 1

In this example, the gcd' function takes in two integers x and y, and uses recursion to find their GCD. The base case for the recursion is when y is equal to 0, in which case the GCD is simply x. In all other cases, the function calls itself with the arguments y and x mod y, which calculates the remainder of x divided by y. This is done until the remainder is 0, at which point the function will return the GCD.

gcd' :: Int -> Int -> Int
gcd' x y
   | y == 0    = x
   | otherwise = gcd' y (x `mod` y)

main :: IO ()
main = do
   let x = 48
   let y = 18
   print (gcd' x y)

Output

6

Example 2

In this example, the gcd' function takes in two integers, x and y, as parameters. The function uses a case statement to check the values of x and y. If y is 0, the function returns x as the GCD. If y is not 0, the function calls itself with the values of y and the remainder of x divided by y as the new parameters. This continues until y is 0, at which point the function returns x as the GCD.

gcd' :: Int -> Int -> Int
gcd' x y = case (x, y) of
   (x, 0) -> x
   (x, y) -> gcd' y (x `mod` y)

main :: IO ()
main = do
   let x = 60
   let y = 48
   print (gcd' x y)

Output

12

Conclusion

The greatest common divisor (GCD) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers without a remainder. It is also known as the greatest common factor (GCF) or highest common factor (HCF). The GCD of two integers can be calculated using the Euclidean algorithm, which is a recursive method that can also be made tail-recursive. The GCD of more than two integers can be found by applying the Euclidean algorithm iteratively. The GCD is an important concept in number theory and has many applications in computer science and mathematics. In Haskell, to find the G.C.D of numbers using recursion, we can use user-defined functions, along with case statements.

Updated on: 27-Mar-2023

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements