Haskell Program to calculate the Lowest Common Multiple


This tutorial will help us in calculating the Lowest Common Multiple in Haskell Programming. The least common multiple (LCM) is the smallest positive integer that is a multiple of two or more integers. It can be found by listing out the multiples of each number and finding the smallest multiple that is common to all of them. For example, the LCM of 4 and 6 is 12 because 12 is the smallest number that is a multiple of both 4 and 6.

Method 1: Using user-defined lcm’ function

In this method, the gcd function is used from the Data.List library to calculate the greatest common divisor (GCD) of two integers, and then uses that value to calculate the LCM by taking the absolute value of the product of the two integers and dividing it by the GCD.

Algorithm

  • Step 1 − The Data.List module is imported to use gcd function.

  • Step 2 − The user-defined lcm’ function is defined using gcd function as, lcm' a b = abs (a * b) `div` (gcd a b). The greatest common divisor (GCD) of two integers is calculated, and then this value is used to calculate the LCM by taking the absolute value of the product of the two integers and dividing it by the GCD.

  • 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. It takes two integers as input and prints the output of lcm’ function.

  • Step 4 − The variable named, “x” is being initialized. It will call the lcm’ function and pass the two integers as parameter to this function.

  • Step 5 − The result is printed to the console using ‘print’ function.

Example

In this example, we are going to see that how we can calculate the Lowest Common Multiple. This can be done by using user-defined lcm’ function.

import Data.List
import Text.Printf
lcm' :: Integer -> Integer -> Integer
lcm' a b = abs (a * b) `div` (gcd a b)
main = do
   let  a = 15
   let  b = 20
   let x = lcm' a b
   printf"LCM of %d and %d is:" a b
   print x

Output

LCM of 15 and 20 is:60

Method 2: Using div function

In this method, the lcm function takes in two integers as arguments and calculates their LCM using the formula (a * b) div (gcd a b).

Algorithm

  • Step 1 − The lcm function is defined using div and gcd function as, lcm a b = (a * b) `div` (gcd a 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 takes two integers as input and prints the output of lcm function.

  • Step 3 − The variable named, “x” is being initialized. It will call the lcm function from Prelude module, which is an inbuilt function in Prelude library and pass the two integers as parameter to this function.

  • Step 4 − The result is printed to the console using ‘print’ function.

Example

In this example, we are going to see that how we can calculate the Lowest Common Multiple. This can be done by using div function.

lcm :: Integer -> Integer -> Integer
lcm a b = (a * b) `div` (gcd a b)

main = do
   let x = Prelude.lcm 15 20
   print x

Output

60

Method 3: Using foldl1 function

In this method, the list of integers is passed to the lcmFold function, which uses the foldl1 function to repeatedly apply the lcm function to the elements of the list, starting with the first two elements and continuing with the result and the next element, until the entire list has been processed.

Algorithm

  • Step 1 − The Data.List module is imported.

  • Step 2 − The lcmFold function is defined using foldl1 to the elements of list as, lcmFold = foldl1 lcm. And the result is returned as the Lowest Common Multiple.

  • 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. It takes a list of integers as input and prints the output of lcmFold function defined.

  • Step 4 − The variable named, “x” is being initialized to call the lcmFold function and pass the list of integers to it.

  • Step 5 − The result is printed to the console using ‘print’ function.

Example

In this example, we are going to see that how we can calculate the the Lowest Common Multiple. This can be done by using foldl1 function.

import Data.List

lcmFold :: [Integer] -> Integer
lcmFold = foldl1 lcm

main = do
   let x = lcmFold [15, 20]
   print x

Output

60

Conclusion

The Lowest Common Multiple for the numbers passed in Haskell can be calculated by using the user-defined lcm’ function, by using foldl1 function or by using div function. The result is printed to the console using ‘print’ function on calling the function defined.

Updated on: 01-Mar-2023

332 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements