Haskell program to check whether the input number is a Prime number


This tutorial discusses writing a program to check whether the input number is a Prime number in the Haskell programming language. Haskell is a Declarative, Strongly Typed, and Functional programming Language. The computations in Haskell are mathematical functions.

A prime number is a number having only 1 and the number itself as a factor. Note 1 is neither a prime nor a composite number because it has only one factor. For example, 3 is a prime number because it has only two factors 1 and the number 3 itself.

In this tutorial we see,

  • Program to check whether a number is a prime number using a recursive function.
  • Program to check whether a number is a prime number using list comprehension.

Example 1

Program to check whether a number is a prime number using a recursive function

-- function declaration
isPrime :: Int->Int->Bool

-- function definition
-- base case 
isPrime 1 i = False
isPrime n i = if(i==n)
   then True
   else if ((mod n i) == 0)
      then False
      else isPrime n (i+1)
      
main :: IO()
main = do
-- declare and initialize variables
   let n = 23
-- invoking the function isPrime
   let status = isPrime n 2
-- print the status
   if (status==True)
   then print ("The number " ++ show n ++ " is a prime number")
   else print ("The number " ++ show n ++ " is not a prime number")

Output

"The number 23 is a prime number"

In the above program,

We declared a function isPrime as such it takes two integer arguments and returns a boolean value. In its function definition, it accepts two integer arguments n and i, where n is the number to which the status of the prime should be computed and i is an iterative variable used to recurse.

The function checks the value of n to that of i. If the value of the n is equal to i, the control is transferred to the then block where it returns the value False.

If the value of the n is not equal to i, the control is transferred to the else block where it checks the number n is divided with i. If n is divided by i then the function returns false i.e the number n has a divisor between 1 and n so it returns false. If n is not divided by i then the function returns a recursive call to itself with the arguments n and (i+1).

The recursive calls are invoked until the iterative variable I reaches the value of n denotes that the number is a prime. The base case for the function is defined as if the value of n is 1 then the function returns False as 1 is neither a prime nor a composite number.

In the main function, a variable is initialized for n, and the function isPrime is invoked with arguments n and 1 where the second argument is iterative values used to recurse. The returned output is loaded into a variable status. Finally, the value of the status is checked, If the value of the status is true the program prints “the number is a prime” else the program prints “the number is not a prime”.

Note − The function show takes the argument of a number and returns the parsed string of a number. “++” is an operator to concatenate strings in Haskell.

Example 2

Program to check whether a number is a prime number using List Comprehensions

-- function declaration
isPrime :: Int->Bool

-- function definition
-- base case 
isPrime 1 = False
isPrime n = and [(mod n i)/=0 | i<-[2..(n-1)]]

main :: IO()
main = do
-- declare and initialize variables
   let n = 23
-- invoking the function isPrime
   let status = isPrime n
-- print the status
   if (status==True)
   then print ("The number " ++ show n ++ " is a prime number")
   else print ("The number " ++ show n ++ " is not a prime number")

Output

"The number 23 is a prime number"

In the above program,

We declared a function isPrime as such it takes an integer as an argument and returns a boolean value. In its function definition, the function accepts an argument n where n is the number to which the status of the prime is to be checked. In the function, a list of boolean values is generated by generating numbers from 2 to n-1 and returning the number as boolean values by dividing the number n with i and checking the computed value with 0.

The list comprehension generates a list of boolean values representing numbers between 1 and n-1. If the number can be divided with i then the respective boolean value is False else the respective boolean value is True.

We invoked a function, which takes a list of boolean values as arguments and returns the cumulative operation of all boolean values in the list.

This function returns true if all the values in the list are true i.e the number has no divisors between 1 and n. Concluding that a number is a prime number. Else if any of the values in the list is False then the function returns true i.e the number had a divisor in the range between 1 and n.

The base case for the function is defined as if the value of n is 1 then the function returns False as 1 is neither a prime nor a composite number.

In the main function, a variable is initialized for n, and the function isPrime is invoked with argument n. The returned output is loaded into a variable status. Finally, the value of the status is checked, If the value of the status is true the program prints “the number is a prime” else the program prints “the number is not a prime”.

Conclusion

In this tutorial, we discussed implementing a program to check whether the input number is a prime number in the Haskell programming Language.

Updated on: 14-Dec-2022

325 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements