# Haskell Program to Check Whether a Number is Prime or Not

To check whether a given number is prime or not we are going to use mod function and list comprehension method in Haskell.

## What is a Prime Number?

A prime number is a positive integer greater than 1 that is only divisible by 1 and itself. In other words, a prime number cannot be written as the product of two smaller positive integers, except for 1 and itself. For example, the first few prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

## Algorithm

• Step 1 − The isPrime function is defined.

• 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. In the main function, a number is passed to the isPrime function. The result of the function is used to print a message indicating whether the number is prime or not.

• Step 3 − The variable named, “n” is being initialized. It will hold the integer which is to be checked if it is prime or not.

• Step 4 − The result is printed to the console using ‘putStrLn’ statement after the function is called.

## Example 1

In this example, the isPrime function takes an integer n as input and returns a Boolean indicating whether the number is prime or not. The function uses the all function to check if all the numbers from 2 to the floor of the square root of n are not divisors of n. If all of these numbers are not divisors, then n is prime.

isPrime :: Integer -> Bool
isPrime n = n > 1 && all (\x -> n mod x /= 0) [2 .. floor (sqrt (fromIntegral n))]

main :: IO ()
main = do
let n = 7
if n > 0
then if isPrime n
then putStrLn "The number is prime."
else putStrLn "The number is not prime."
else putStrLn "Invalid input. Please enter a positive integer."


## Output

The number is prime.


## Example 2

In this example, a function isPrime is defined using mod and filter function to check if the passed integer is prime or not.

isPrime :: Int -> Bool
isPrime n = n > 1 && (filter (\x -> n mod x == 0) [2..n-1] == [])

main :: IO ()
main = do
let n = 7
if n > 0
then if isPrime n
then putStrLn "The number is prime."
else putStrLn "The number is not prime."
else putStrLn "Invalid input. Please enter a positive integer."


## Output

The number is prime.


## Example 3

In this example, for all other cases, we use a list comprehension to check if any number in the range [3, 5, ..., upperBound] divides n evenly. If no such number exists, n is prime, and we return True.

isPrime :: Int -> Bool
isPrime n
| n <= 1 = False
| n == 2 = True
| even n = False
| otherwise = all (\x -> n mod x /= 0) [3,5..upperBound]
where upperBound = floor $sqrt$ fromIntegral n

main :: IO ()
main = do
let n = 7
if n > 0
then if isPrime n
then putStrLn "The number is prime."
else putStrLn "The number is not prime."
else putStrLn "Invalid input. Please enter a positive integer."


## Output

The number is prime.


## Conclusion

In Haskell, we can check if the integer passed to the function is prime or not using mod function along with fromIntegral or filter function, or by using list comprehension.

Updated on: 24-Apr-2023

178 Views

##### Kickstart Your Career

Get certified by completing the course

Advertisements