How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Haskell programming language

Table of Contents

Problem Statement

These define three classifications of positive integers based on their   proper divisors. Let   P(n)   be the sum of the proper divisors of   n   where the proper divisors are all positive divisors of   n   other than   n   itself.

6   has proper divisors of   1,   2,   and   3. 1 + 2 + 3 = 6,   so   6   is classed as a perfect number.

Calculate how many of the integers   1   to   20,000   (inclusive) are in each of the three classes. Show the results here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Haskell programming language

The provided Haskell code defines several functions to analyze divisors and classify numbers as deficient, perfect, or abundant and calculate the distribution of these types of numbers.

  1. divisors:

    • Takes an integer n as input.
    • Returns a list of all positive divisors of n (including 1 and n itself).
    • It filters out non-divisors by checking if the remainder of n when divided by each candidate divisor is 0.
    • The range of candidate divisors is [1, n/2] because divisors of n must be less than or equal to n/2.
  2. classOf:

    • Takes an integer n as input.
    • Determines the class of n based on the comparison between the sum of its divisors and itself:
      • If the sum of divisors is less than n, n is classified as "deficient."
      • If the sum of divisors is equal to n, n is classified as "perfect."
      • If the sum of divisors is greater than n, n is classified as "abundant."
  3. main (first definition):

    • Defines three functions: printRes, classOf, and divisors.
    • Calculates the class of numbers from 1 to 20,000 and stores the results in the classes list.
    • Uses printRes to print the count of deficient, perfect, and abundant numbers.
  4. deficientPerfectAbundantCountsUpTo:

    • Takes an integer n as input.
    • Returns a tuple containing the count of deficient, perfect, and abundant numbers up to n.
    • Uses the go function to iterate over numbers from 1 to n, calculating the class of each number and updating the counts accordingly.
  5. properDivisors:

    • Takes an integer x as input.
    • Returns a list of proper divisors of x (all positive divisors of x except x itself).
    • Uses the primeFactors function to factor x into prime numbers.
    • Groups the prime factors by their powers and calculates the divisors by combining these groups using the go function.
  6. main (second definition):

    • Prints the tuple returned by deficientPerfectAbundantCountsUpTo 20000, which contains the count of deficient, perfect, and abundant numbers up to 20,000.

Source code in the haskell programming language

divisors :: (Integral a) => a -> [a]
divisors n = filter ((0 ==) . (n `mod`)) [1 .. (n `div` 2)]

classOf :: (Integral a) => a -> Ordering
classOf n = compare (sum $ divisors n) n

main :: IO ()
main = do
  let classes = map classOf [1 .. 20000 :: Int]
      printRes w c = putStrLn $ w ++ (show . length $ filter (== c) classes)
  printRes "deficient: " LT
  printRes "perfect:   " EQ
  printRes "abundant:  " GT


import Data.Numbers.Primes (primeFactors)
import Data.List (group, sort)

deficientPerfectAbundantCountsUpTo :: Int -> (Int, Int, Int)
deficientPerfectAbundantCountsUpTo = foldr go (0, 0, 0) . enumFromTo 1
  where
    go x (deficient, perfect, abundant)
      | divisorSum < x = (succ deficient, perfect, abundant)
      | divisorSum > x = (deficient, perfect, succ abundant)
      | otherwise = (deficient, succ perfect, abundant)
      where
        divisorSum = sum $ properDivisors x

properDivisors :: Int -> [Int]
properDivisors = init . sort . foldr go [1] . group . primeFactors
  where
    go = flip ((<*>) . fmap (*)) . scanl (*) 1

main :: IO ()
main = print $ deficientPerfectAbundantCountsUpTo 20000


  

You may also check:How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the F# programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Archimedean spiral step by step in the C++ programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Comments step by step in the Babel programming language