- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
In Haskell, we will Check Whether a Number can be Expressed as Sum of Two Prime Numbers by using user-defined functions. In the first example we are going to use (isPrime) function along with primeSum function and in the second and third example, we are going to use (isPrime) function along with isSumOfTwoPrimes function.
In the following examples, the function isPrime checks whether a given number is prime or not. It first checks if the number is less than or equal to 1, in which case it returns False. It then checks if the number is 2, in which case it returns True. If the number is even, it returns False. Otherwise, it checks if the number is divisible by any integer between 3 and the square root of the number. If it is, it returns False, otherwise it returns True.
The function primeSum checks whether a given number can be expressed as the sum of two prime numbers. It uses the any function to check if any number between 2 and the given number minus 2, when added to another prime number, equals the given number.
Algorithm
Step 1 − The user defined isPrime function is defined as,
isPrime n | n <= 1 = False | n == 2 = True | even n = False | otherwise = not $ any (\x -> n `mod` x == 0) [3,5..(floor . sqrt . fromIntegral) n].
And the user defined primeSum function is defined as, primeSum n = any (\x -> isPrime x && isPrime (n-x)) [2..(n-2)].
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, we assign the value to a variable num and then call the primeSum function on that variable. It returns True if it can be represented using sum of two prime numbers.
Step 3 − The variable named, “num” is being initialized. It will hold the number value which is need to be represented as sum of two prime numbers.
Step 4 − The result is printed to the console using ‘print’’ function, after the function is called.
Example 1
In this example, we are going to see that how we can check whether a number can be expressed as sum of two prime numbers. This can be done by using user-defined function.
isPrime :: Integer -> Bool isPrime n | n <= 1 = False | n == 2 = True | even n = False | otherwise = not $ any (\x -> n `mod` x == 0) [3,5..(floor . sqrt . fromIntegral) n] primeSum :: Integer -> Bool primeSum n = any (\x -> isPrime x && isPrime (n-x)) [2..(n-2)] main :: IO () main = do let num = 28 print (primeSum num)
Output
True
Example 2
In this example, we are going to see that how we can whether a number can be expressed as sum of two prime numbers. This can be done by using user-defined function. Here, we have defined a helper function isPrime which will check if a number is prime or not and then using isSumOfTwoPrimes function we are checking if any number between 2 and n-2 can be added to a prime number to equal n.
isPrime :: Integer -> Bool isPrime n = n > 1 && all (\x -> n `mod` x /= 0) [2..n-1] isSumOfTwoPrimes :: Integer -> Bool isSumOfTwoPrimes n = any (\x -> isPrime x && isPrime (n - x)) [2..n-2] main :: IO () main = do let num = 28 print (isSumOfTwoPrimes num)
Output
True
Example 3
In this example, the function isSumOfTwoPrimes takes an integer n as an input and checks if any number between 2 and n-2 can be added to a prime number to equal n. The isPrime function is used to check if a number is prime. The any function is used to check if any element in a list satisfies a certain condition. If any such number is found, the function returns True, otherwise it returns False.
isPrime :: Integer -> Bool isPrime n = n > 1 && all (\x -> n `mod` x /= 0) [2..n-1] isSumOfTwoPrimes :: Integer -> Bool isSumOfTwoPrimes n = any (\x -> isPrime (n - x) && isPrime x) [2..n-2] main :: IO () main = do let num = 28 print (isSumOfTwoPrimes num)
Output
True
Conclusion
In Haskell, to check whether a number can be expressed as sum of two prime numbers, we can use the user defined functions with different approaches.