# Haskell Program to find the given number is PRIME or not using recursion

In Haskell, we can find the given number is PRIME or not by using recursion along with helper function. In the first example, we are going to use (isPrime n | n <= 1 = False | n == 2 = True | otherwise = isPrimeHelper n 2) function and in the second example, we are going to use (isPrime n = isPrimeHelper n 2) function. And in the third example, we are going to use (isPrime n = isPrimeHelper n 2 (floor . sqrt . fromIntegral $n)). ## Algorithm • Step 1 − The user defined isPrime function is defined with the help of helper function and its definition is written as, isPrime n | n <= 1 = False | n == 2 = True | otherwise = isPrimeHelper n 2 .  • Step 2 − The user-defined helper function is defined as, isPrimeHelper n d | d > (n div 2) = True | n mod d == 0 = False | otherwise = isPrimeHelper n (d + 1).  • 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. In the main function, the user defined recursive function is being called by passing argument to it. • Step 4 − The variable named, “num” is being initialized. It will hold the number which is to be checked as if it is prime or not. • Step 5 − The result whether the number is prime or not is printed to the console using ‘show’ function after the function is called. ## Example 1 In this example, the two functions, isPrime and isPrimeHelper, are used to check if a number is prime. The isPrime function serves as a wrapper that calls isPrimeHelper with n as the first argument and 2 as the second argument. isPrime :: Integer -> Bool isPrime n | n <= 1 = False | n == 2 = True | otherwise = isPrimeHelper n 2 isPrimeHelper :: Integer -> Integer -> Bool isPrimeHelper n d | d > (n div 2) = True | n mod d == 0 = False | otherwise = isPrimeHelper n (d + 1) main :: IO () main = do let num = 7 let isPrimeNum = isPrime num if isPrimeNum then putStrLn (show num ++ " is a prime number.") else putStrLn (show num ++ " is not a prime number.")  ## Output 7 is a prime number.  ## Example 2 In this example, two functions isPrime and isPrimeHelper are used, to check if a number is prime. The isPrime function calls isPrimeHelper with n as the first argument and 2 as the second argument. isPrime :: Integer -> Bool isPrime n = isPrimeHelper n 2 isPrimeHelper :: Integer -> Integer -> Bool isPrimeHelper n d | n < 2 = False | d == n = True | n mod d == 0 = False | otherwise = isPrimeHelper n (d + 1) main :: IO () main = do let num = 7 let isPrimeNum = isPrime num if isPrimeNum then putStrLn (show num ++ " is a prime number.") else putStrLn (show num ++ " is not a prime number.")  ## Output 7 is a prime number.  ## Example 3 In this example, two functions, isPrime and isPrimeHelper are used to check if a number is prime. The isPrime function calls isPrimeHelper with n as the first argument, 2 as the second argument, and the square root of n as the third argument. isPrime :: Integer -> Bool isPrime n = isPrimeHelper n 2 (floor . sqrt . fromIntegral$ n)

isPrimeHelper :: Integer -> Integer -> Integer -> Bool
isPrimeHelper n d sqrtN
| n < 2      = False
| d > sqrtN  = True
| n mod d == 0 = False
| otherwise       = isPrimeHelper n (d + 1) sqrtN

main :: IO ()
main = do
let num = 7
then putStrLn (show num ++ " is a prime number.")
else putStrLn (show num ++ " is not a prime number.")


## Output

7 is a prime number.


## Conclusion

In Haskell, prime numbers can be checked using various algorithms, including trial division, Sieve of Eratosthenes, and Fermat's Little Theorem. To check if a number is prime, one can write a function that iterates through all the numbers from 2 to the square root of the number being checked and checks if the number is divisible by any of them. If the number is not divisible by any of these numbers, then it is prime. This can also be checked using recursion.

Updated on: 27-Mar-2023

573 Views 