How to resolve the algorithm Aliquot sequence classifications step by step in the Haskell programming language
How to resolve the algorithm Aliquot sequence classifications step by step in the Haskell programming language
Table of Contents
Problem Statement
An aliquot sequence of a positive integer K is defined recursively as the first member being K and subsequent members being the sum of the Proper divisors of the previous term.
Show all output on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Aliquot sequence classifications step by step in the Haskell programming language
This Haskell program classifies integers based on their aliquot sequences, which are sequences of positive integers that can be obtained by summing the proper divisors of a given number.
Divisors:
- The
divisors
function takes an integern
and returns a list of its divisors, excludingn
itself. It does this by filtering the list of integers from 1 ton div 2
(inclusive) for numbers that dividen
without remainder.
Aliquot:
- The
aliquot
function generates the aliquot sequence for an integern
.- If
n
is 0, it returns a list containing only 0. - Otherwise, it adds
n
to the result of recursively callingaliquot
on the sum of the divisors ofn
.
- If
Classification:
- The
classify
function takes an aliquot sequence and classifies it into one of seven classes:- Terminating: The aliquot sequence eventually reaches 0.
- Perfect: The aliquot sum is equal to the original number.
- Amicable: The aliquot sum of the original number is equal to another number, and vice versa.
- Sociable: The aliquot sum of the original number is equal to the aliquot sum of another number, but not vice versa.
- Aspiring: The aliquot sequence starts with a perfect number.
- Cyclic: The aliquot sequence forms a cycle.
- Nonterminating: The aliquot sequence does not fit into any of the other categories.
Main Function:
- The
main
function calculates the aliquot sequences for a list of integers and prints the classification for each sequence. - It uses the
cls
function to compute the classification and the first 16 terms of the aliquot sequence for each integer. - It prints the classification for the integers from 1 to 10, as well as some additional notable integers.
Source Code Overview
The provided Haskell code defines functions and data structures to classify integers based on the aliquot sequence (sum of the divisors of a number) and print the classifications along with a sample of the aliquot sequence.
Data Structures
Class
data structure: Represents the classification of an integer.Terminating
: The aliquot sequence ends in 0.Perfect
: The aliquot sequence ends with the number itself (e.g., 6).Amicable
: Two numbers have the same aliquot sequence (e.g., 220 and 284).Sociable
: More than two numbers have the same aliquot sequence.Aspiring
: The aliquot sequence ends with a Perfect number (e.g., 12).Cyclic
: The aliquot sequence repeats back to itself (e.g., 562).Nonterminating
: The aliquot sequence does not terminate in 0 or repeat back to itself.
Functions
-
divisors
: Generates a list of the divisors of a number up to half of the number. -
aliquot
: Calculates the aliquot sequence for a number by iteratively summing the divisors. -
classify
: Classifies an integer based on its aliquot sequence.
Main Function
main
:- Calculates the first 16 elements of the aliquot sequence for a range of integers.
- Classifies each integer and prints the classification along with a sample of the aliquot sequence.
Example Output
The output will be a list of pairs for each integer, where the first element is the classification and the second element is a list of the first 16 elements of the aliquot sequence:
(Perfect,[1,6])
(Terminating,[1,0])
(Nonterminating,[1,28])
(Perfect,[1,644])
(Nonterminating,[1,220])
(Perfect,[1,6144])
(Nonterminating,[1,12496])
(Perfect,[1,660445])
(Nonterminating,[1,790])
(Nonterminating,[1,909])
(Nonterminating,[1,562])
(Nonterminating,[1,1064])
(Perfect,[1,6312])
...
Source code in the haskell programming language
divisors :: (Integral a) => a -> [a]
divisors n = filter ((0 ==) . (n `mod`)) [1 .. (n `div` 2)]
data Class
= Terminating
| Perfect
| Amicable
| Sociable
| Aspiring
| Cyclic
| Nonterminating
deriving (Show)
aliquot :: (Integral a) => a -> [a]
aliquot 0 = [0]
aliquot n = n : (aliquot $ sum $ divisors n)
classify :: (Num a, Eq a) => [a] -> Class
classify [] = Nonterminating
classify [0] = Terminating
classify [_] = Nonterminating
classify [a,b]
| a == b = Perfect
| b == 0 = Terminating
| otherwise = Nonterminating
classify x@(a:b:c:_)
| a == b = Perfect
| a == c = Amicable
| a `elem` (drop 1 x) = Sociable
| otherwise =
case classify (drop 1 x) of
Perfect -> Aspiring
Amicable -> Cyclic
Sociable -> Cyclic
d -> d
main :: IO ()
main = do
let cls n = let ali = take 16 $ aliquot n in (classify ali, ali)
mapM_ (print . cls) $ [1..10] ++
[11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488]
You may also check:How to resolve the algorithm Flipping bits game step by step in the C programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the Haskell programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Ada programming language
You may also check:How to resolve the algorithm Population count step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the Nial programming language