Haskell Program To Find The Perfect Number


In haskell we can use list comprehension and brute-force method to find the perfect number.

What is a Perfect Number?

Perfect numbers are positive integers that are equal to the sum of their proper divisors. A divisor of a positive integer n is a positive integer that divides n exactly, leaving no remainder. A proper divisor is a divisor of n that is less than n itself.

For example, the proper divisors of 6 are 1, 2, and 3, and the sum of these divisors is 1 + 2 + 3 = 6. Therefore, 6 is a perfect number..

Algorithm

  • Step 1 − The perfectNumbers 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 limit is passed up to which the perfect number is calculated.

  • Step 3 − The variable named, “limit” is being initialized. It will hold the integer up to which the perfect number is to be calculated.

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

Example 1

In this example, the perfect numbers up to the given limit (in this case, 1000) is printed. The perfectNumbers function uses a list comprehension to generate a list of all positive integers up to the limit that are equal to the sum of their proper divisors, as determined by the properDivisors function. The main function then calculates the perfect numbers and prints the result to the console.

perfectNumbers :: Int -> [Int]
perfectNumbers limit = [x | x <- [2..limit], x == sum (properDivisors x)]

properDivisors :: Int -> [Int]
properDivisors n = [x | x <- [1..n-1], n `mod` x == 0]

main :: IO ()
main = do
   let limit = 1000
   let perfects = perfectNumbers limit
   putStrLn $ "The perfect numbers up to " ++ show limit ++ " are: " ++ show perfects

Output

The perfect numbers up to 1000 are: [6,28,496]

Example 2

In this example, the function perfectNumbers and properDivisors are defined using Sieve of Eratosthenes algorithm to calculate the perfect numbers.

import Data.Array

properDivisors :: Int -> [Int]
properDivisors n = [x | x <- [1..n `div` 2], n `mod` x == 0]

perfectNumbers :: Int -> [Int]
perfectNumbers limit = [x | x <- [2..limit], x == sum (properDivisors x)]

main :: IO ()
main = do
   let limit = 1000
   let perfects = perfectNumbers limit
   putStrLn $ "The perfect numbers up to " ++ show limit ++ " are: " ++ show perfects

Output

The perfect numbers up to 1000 are: [6,28,496]

Example 3

In this example, the isPerfect function takes an integer n as input and returns True if n is a perfect number, and False otherwise. The perfectNumbers function uses a list comprehension to generate a list of all positive integers up to the limit that are perfect numbers, as determined by the isPerfect function. The main function then calculates the perfect numbers and prints the result to the console.

isPerfect :: Int -> Bool
isPerfect n = n == sum [x | x <- [1..n-1], n `mod` x == 0]

perfectNumbers :: Int -> [Int]
perfectNumbers limit = [x | x <- [2..limit], isPerfect x]

main :: IO ()
main = do
   let limit = 1000
   let perfects = perfectNumbers limit
   putStrLn $ "The perfect numbers up to " ++ show limit ++ " are: " ++ show perfects

Output

The perfect numbers up to 1000 are: [6,28,496]

Conclusion

In Haskell, we can find the perfect numbers up to certain limit using some user-defined functions along with mod function or by using list comprehension, or by using brute-force method.

Updated on: 24-Apr-2023

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements