Haskell program to find lcm 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.

LCM of two numbers is the least common multiple for that two numbers i.e smallest number that is divided by both numbers.

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

Using inbuilt function lcm.

  • Computing LCM using list comprehension.

  • Computing LCM using a recursive function.

  • Computing LCM using the HCF (Highest Common Factor) function.

Algorithm steps

  • Take the two integers as input.

  • Implement the LCM computation logic.

  • Display the output.

Using inbuilt function gcd.

Example

Program to find LCM using inbuilt function

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

Output

LCM of 10 and 23 is:230

In the above program we declared and initialized variables with numbers 10 and 23, which we want to find lcm for. We used an inbuilt function lcm, which takes two integers as arguments and returns the lcm of that pair of numbers. Finally, we printed the output of the function using the print function.

Finding LCM Using List Comprehensions

Example

-- function declaration lcm2 :: Int->Int->Int -- function definition lcm2 a b = head [x|x<-[1..(a*b)],(mod x a)==0,(mod x b)==0] main :: IO() main = do let a = 10 let b = 23 -- displaying the output returned from function lcm2 putStr("LCM of "++ (show a) ++ " and "++ (show b) ++" is:") print (lcm2 a b)

Output

LCM of 10 and 23 is:230

In the above program, we declared function lcm2 which takes two integers as arguments and returns an Integer. In the function definition, we are taking integers a and b as arguments and we are generating a list from 1 to a*b from which are filtering numbers that are divided by both a and b. Finally, we are returning the first number in the list with the function head. Which takes the list as an argument and returns the first element in the list. As the list contains all the elements that are divisible by both a and b in increasing order, The first element will be the Least Common Multiple. We are invoking the function in the main function and printing the returned output.

Finding LCM Using Recursive Function.

Example

Program to find LCM using recursive function.

-- function declaration lcm3 :: Int->Int->Int->Int -- function definition lcm3 a b c = if ((mod c a)==0&&(mod c b)==0) then c else lcm3 a b (c+1) main :: IO() main = do let a = 10 let b = 40 -- printing the output by invoking lcm3 function putStr("LCM of "++ (show a) ++ " and "++ (show b) ++" is:") print (lcm3 a b 1)

Output

LCM of 10 and 40 is:40

In the Above program we declared function lcm3 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 1. In the function, we are checking whether c is divisible by both a and b. If yes we are returning the integer c else we are recursively calling the lcm3 function by incrementing the value of c by 1 i.e, The function recursively iterating starting from 1 and returning the first number that is divided by both a and b which is Least Common Multiple(LCM) of that two numbers.

Finding LCM Using The HCF (Highest Common Factor) Function

Example

Program to find LCM using the HCF (Highest Common Factor) function.

-- function declaration hcf :: Int->Int->Int -- function definition hcf a 0 = a hcf a b = hcf b (mod a b) -- function declaration lcm4 :: Int->Int->Int -- function definition lcm4 a b = div (a*b) (hcf a b) main :: IO() main = do let a = 10 let b = 25 -- printing the output by invoking the lcm4 function putStr("LCM of "++ (show a) ++ " and "++ (show b) ++" is:") print (lcm4 a b)

Output

LCM of 10 and 25 is:50

In the above program implemented a function for finding the HCF of two numbers. As the LCM*HCF of a pair of numbers is equal to the product of the pair. So lcm is the product divided with HCF. We declared function hcf which takes two integers as arguments and returns an integer. In the function definition, we are taking integer arguments as a and b and we recursively call itself with arguments b and a%b until it reaches the base case where the second argument is zero we return the first argument which will be the HCF of two numbers. Now we got the HCF of two numbers we use the utility function lcm4 which takes two integers are returns the lcm by dividing the product of the number with the HCF of that two numbers. We are invoking the lcm4 function from the main and printing the output.

Finding LCM using the Inbuilt GCD Function.

Example

Program to find LCM using the inbuilt gcd function.

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

Output

LCM of 10 and 23 is:230

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

Conclusion

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

Updated on: 27-Oct-2022

337 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements