Haskell Program to find the GCD using library function


In Haskell, we will find the GCD using library function like gcd, div function and recursion. In the first example, we are going to use gcd (a b) function and in the second example, we are going to use (a `div` b) function. In third example, we are going to use recursion.

Method 1: Finding the GCD using gcd function

In this method, the gcd function takes two integers as input and returns the greatest common divisor of these two numbers. This function is defined in the Prelude library.

Algorithm

  • Step 1 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. It calls the gcd function with the values and prints the greatest common divisor of the two.

  • Step 2 − The variables named, “a” and “b” are being initialized. It will hold the value to find the gcd of both.

  • Step 3 − The gcd function is called as, gcd a b.

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

Example 1

In this example, we are going to see that how we can find the gcd of given numbers. This can be done by using gcd function.

main :: IO ()
main = do
   let a = 5
   let b = 25
   let gcdVal = gcd a b
   putStrLn $ "The GCD of " ++ show a ++ " and " ++ show b ++ " is: " ++ show gcdVal

Output

The GCD of 5 and 25 is: 5

Method 2: Finding the GCD using div function

In this method, the div function repeatedly divides the numbers until the remainder is 0, at which point the GCD is equal to the last non-zero quotient and returns the greatest common divisor of the two numbers.

Algorithm

  • Step 1 − The gcd’’’ function is defined using div function as,

gcd''' a b
| b == 0    = a
| otherwise = gcd''' b (a `div` b).
  • 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. It calls the gcd’’’ function with the values and prints the greatest common divisor of the two.

  • Step 3 − The variables named, “a” and “b” are being initialized. It will hold the value to find the gcd of both.

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

Example 1

In this example, we are going to see that how we can find the gcd of given numbers. This can be done by using div function.

gcd''' :: Integral a => a -> a -> a
gcd''' a b
   | b == 0    = a
   | otherwise = gcd''' b (a `div` b)

main :: IO ()
main = do
   let a = 5
   let b = 20
   let gcdVal = gcd''' a b
   putStrLn $ "The GCD of " ++ show a ++ " and " ++ show b ++ " is: " ++ show gcdVal

Output

The GCD of 5 and 20 is: 20

Method 3: Finding the GCD using mod function and recursion

In this method, recursion is used to repeatedly apply the modulus operator until the remainder is 0, at which point the GCD is equal to the last non-zero remainder and returns the greatest common divisor of the two numbers.

Algorithm

  • Step 1 − The gcd’ function is defined using mod function as, gcd' a b = gcd' b (a `mod` b).

  • 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. It calls the gcd’ function with the values and prints the greatest common divisor of the two.

  • Step 3 − The variables named, “a” and “b” are being initialized. It will hold the value to find the gcd of both.

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

Example 1

In this example, we are going to see that how we can find the gcd of given numbers. This can be done by using mod function recursion.

gcd' :: Integral a => a -> a -> a
gcd' a 0 = a
gcd' a b = gcd' b (a `mod` b)

main :: IO ()
main = do
   let a = 5
   let b = 10
   let gcdVal = gcd' a b
   putStrLn $ "The GCD of " ++ show a ++ " and " ++ show b ++ " is: " ++ show gcdVal

Output

The GCD of 5 and 10 is: 5

Conclusion

The GCD, or greatest common divisor, is the largest positive integer that divides two or more numbers without leaving a remainder. In other words, it is the largest number that divides two or more given numbers.To find the gcd of the given numbers, we can use the gcd, div or mod functions in Haskell. It can also be obtained using recursion.

Updated on: 13-Mar-2023

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements