Haskell program to find the gcd of two numbers

This tutorial will discuss writing a program to find the LCM of two numbers in the Haskell Programming Language. Haskell is a functional programming language.

The GCD of two numbers is the Greatest Common Divisor/ Number that divides both numbers Can also be called as Highest Common Factor.

In this tutorial, we discuss five ways to implement a program to find the GCD of two numbers.

• Using the inbuilt function gcd.

• Using the inbuilt function lcm.

• Computing GCD using list comprehension.

• Computing GCD using a recursive function with three arguments.

• Computing GCD using a recursive function with two arguments.

Algorithm steps

• Take the two integers as input.

• Implement the GCD computation logic.

• Display the output.

Fidning GCD Using Inbuilt Function Gcd

Example

Program to find GCD using inbuilt function gcd

main :: IO()
main = do
-- declaring and initializing variables
let a = 10
let b = 25
-- print the gcd by invoking the inbuilt function lcm
putStr("GCD of "++ (show a) ++ " and "++ (show b) ++" is:")
print (gcd a b)


Output

GCD of 10 and 25 is:5


In the above program, we declared and initialized variables with numbers 10 and 23, which we want to find GCD. We used an inbuilt function gcd, which takes two integers as arguments and returns the GCD of that pair of numbers. Finally, we printed the output of the function using the print function. As function putStr prints only string data type we convert integers to strings using function show.

find GCD using inbuilt function lcm

Example

Program to find GCD using inbuilt function lcm

main :: IO()
main = do
let a = 10
let b = 45
-- print the gcd by invoking the inbuilt function gcd
putStr("GCD of "++ (show a) ++ " and "++ (show b) ++" is:")
print (div (a*b) (lcm a b))


Output

GCD of 10 and 45 is:5


In the above program we declared and initialized the variable for which GCD should be calculated. We used an inbuilt function lcm which takes two integers as arguments and returns the LCM of that numbers. We know that the LCM*GCD of a pair of integers equals the product of that pair of integers. So we computed the product divided with LCM to generate GCD and Finally, we printed the computed output.

Find GCD Using List Comprehensions

Example

Program to find GCD using List comprehensions

-- function declaration
gcd3 :: Int->Int->Int
-- function definition
gcd3 a b = head (reverse [x|x<-[1..b],(mod a x)==0,(mod b x)==0])

main :: IO()
main = do
let a = 20
let b = 75
-- displaying the output returned from function lcm2
putStr("GCD of "++ (show a) ++ " and "++ (show b) ++" is:")
print (gcd3 a b)


Output

GCD of 10 and 25 is:5


In the above program, we declared function gcd3 which takes two integers as arguments and returns an Integer. In the function definition, we are taking integers a and b as arguments and generating a list from 1 to b, filtering numbers that divide a and b. Finally, we return the last number in the list with the function head after reversing the list using the reverse function. As the list contains all the elements that divide a and b less than or equal to a and b in increasing order. The last element will be the Greatest Common Divisor. We are invoking the function in the main function and printing the returned output.

Note− The function names in Haskell should start with lowercase letters. In the above program, we named the function gcd3 because as gcd is an inbuilt keyword for function gcd we cannot use that keyword.

Find GCD Using A Recursive Function With Three Arguments

Example

Program to find GCD using a recursive function with three arguments.

-- function declaration
gcd4 :: Int->Int->Int->Int
-- function definition
gcd4 a b c = if ((mod a c)==0&&(mod b c)==0)
then c
else gcd4 a b (c-1)

main :: IO()
main = do
let a = 10
let b = 25
let c = b
-- printing the output by invoking gcd4 function
putStr("GCD of "++ (show a) ++ " and "++ (show b) ++" is:")
print (gcd4 a b c)


Output

GCD of 10 and 25 is:5


In the Above program we declared function gcd4 which takes three integers as an input and returns an integer.In the function definition, we are taking three integers as arguments in which c’s initial value is b. In the function, we are checking whether c divides both a and b. If yes we are returning the integer c else we are recursively calling the gcd4 function by decrementing the value of c by 1 i.e The function recursively iterating starting from b and returning the first number that divides both a and b which is the Greatest Common Divisor(GCD) of that two numbers.

Find GCD Using A Recursive Function With Two Arguments

Example

Program to find GCD using a recursive function with two arguments.

-- function declaration
gcd5 :: Int->Int->Int

-- function definition
gcd5 a 0 = a
gcd5 a b = gcd5 b (mod a b)

main :: IO()
main = do
let a = 10
let b = 20

-- printing the output by invoking the gcd5 function
putStr("GCD of "++ (show a) ++ " and "++ (show b) ++" is:")
print (gcd5 a b)


Output

GCD of 10 and 20 is:10


In the above program we declared a function gcd5 which takes two integers as argument and returns an integer. In the function definition, the function gcd5 takes integers a and b as arguments and returns a recursive call to itself with parameters b and (mod a b) until the base case where the second argument becomes zero where the function returns the first argument. I.e this recursive function returns the GCD of two arguments. Finally, the function is invoked from the main function and the returned output is printed.

Conclusion

In this tutorial, we discussed five ways to implement programs to find the GCD of two numbers in the Haskell Programming Language using inbuilt functions and custom function implementations.