Haskell Program to calculate the Highest Common Factor


This tutorial will help us in calculating the highest common factor using Haskell Programming. The highest common factor (HCF), also known as the greatest common divisor (GCD), is the largest positive integer that divides two or more integers without leaving a remainder. It is a measure of the largest positive integer that can divide two or more integers without leaving a remainder.

Method 1: Using user-defined hcf function

In this method, the function hcf’ is defined that takes two integers as input, and uses recursion and the modulo operator to repeatedly calculate the remainder of the larger number divided by the smaller number until the smaller number becomes 0. At that point, the larger number is returned as the greatest common divisor.

Algorithm

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

  • Step 2 − The user-defined hcf’ function is defined using recursion and the modulo operator to repeatedly calculate the remainder of the larger number divided by the smaller number until the smaller number becomes 0 as, hcf' a b = if b == 0 then a else hcf' b (a `mod` b). And the larger number is returned as the highest common factor.

  • 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 hcf’ function.

  • Step 4 − The variables named, “a” and “b” are initialized. Initially, it will have the garbage value. Then, a constant value is being assigned to it to find the highest common factor of them.

  • Step 5 − The result is printed to the console using ‘print’ function on calling user-defined hcf’ function.

Example

In this example, we are going to see that how we can calculate the highest common factor. This can be done by using user-defined hcf’ function.

import Data.Function
import Text.Printf

hcf' :: Int -> Int -> Int
hcf' a b = if b == 0 then a else hcf' b (a `mod` b)

main :: IO ()
main = do
   let a = 36
   let b = 63
   printf "HCF of %d and %d is:" a b
   print (hcf' a b)

Output

HCF of 36 and 63 is:9

Method 2: Using foldl1 function

This method takes a list of integers as input and uses the foldl1 function to repeatedly apply the gcd function to the elements of the list, starting with the first two elements and then the result of that and the next element, and so on.

Algorithm

  • Step 1 − The hcf function is defined using foldl1 and gcd to the elements of list as, hcf = foldl1 gcd. And the result is returned as the highest common factor.

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

  • Step 3 − The variable named, “number” is being initialized to hold the list of integers.

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

Example

In this example, we are going to see that how we can calculate the highest common factor. This can be done by using foldl1 function.

import Data.Function
import Text.Printf

hcf :: [Int] -> Int
hcf = foldl1 gcd

main :: IO ()
main = do
   let numbers = [36, 63, 9]
   printf("HCF of ")
   print(numbers)
   print (hcf numbers)

Output

HCF of [36,63,9]
9

Method 3: Using fix function

This method uses the fix function to define a self-referential function that calculates the highest common factor using recursion and the modulo operator.

Algorithm

  • Step 1 − The Data.Function.fix and gcd is used from the Prelude module.

  • Step 2 − The hcf function is defined using fix function as, hcf a b = fix (\f x y -> if y == 0 then x else f y (x `mod` y)) a b. And the result is returned as the highest common factor.

  • 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 hcf function.

  • Step 4 − The variables named, “a” and “b” are initialized. A value is being assigned to it to find the highest common factor of them.

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

Example

In this example, we are going to see that how we can calculate the highest common factor. This can be done by using fix function.

import Prelude hiding (gcd)
import Data.Function
import Text.Printf
hcf :: Int -> Int -> Int
hcf a b = fix (\f x y -> if y == 0 then x else f y (x `mod` y)) a b

main :: IO ()
main = do
   let a = 36
   let b = 63
   printf"HCF of %d and %d is:" a b
   print (hcf a b)

Output

HCF of 36 and 63 is:9

Conclusion

The highest common factor for the numbers passed in Haskell can be calculated by using the user-defined hcf’ function, by using foldl1 function or by using fix function. The result is printed to the console using ‘print’ function on calling the function defined.

Updated on: 01-Mar-2023

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements