How to resolve the algorithm Disarium numbers step by step in the Haskell programming language
How to resolve the algorithm Disarium numbers step by step in the Haskell programming language
Table of Contents
Problem Statement
A Disarium number is an integer where the sum of each digit raised to the power of its position in the number, is equal to the number.
135 is a Disarium number: 11 + 32 + 53 == 1 + 9 + 125 == 135 There are a finite number of Disarium numbers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Disarium numbers step by step in the Haskell programming language
The Haskell code defines a module named Disarium
that contains functions for working with Disarium numbers. A Disarium number is a number whose sum of its digits raised to their respective positions is equal to the number itself.
Here's a breakdown of the code:
-
Import Statement:
import Data.Char (digitToInt)
This statement imports the
digitToInt
function from theData.Char
module, which converts a character to its corresponding digit value. -
isDisarium :: Int -> Bool
Function:isDisarium n = (sum $ map (\(c, i) -> (digitToInt c) ^ i) $ zip (show n) [1, 2 ..]) == n
This function takes an integer
n
as input and returnsTrue
ifn
is a Disarium number, andFalse
otherwise.show n
: Converts the integern
to a string.[1, 2 ..]
: Creates an infinite list of natural numbers starting from 1.zip (show n) [1, 2 ..]
: Combines the characters of the string representation ofn
with the corresponding natural numbers. For example, ifn
is 123, the result would be[('1', 1), ('2', 2), ('3', 3)]
.map (\(c, i) -> (digitToInt c) ^ i)
: Applies a mapping function to each element of the zipped list. This function converts each character to its digit value, raises it to the power of its position, and returns the result. For example,('1', 1)
would become1^1 = 1
.sum
: Calculates the sum of the transformed list.- The result of the sum is then compared to the original number
n
. If they are equal, the function returnsTrue
, indicating thatn
is a Disarium number. Otherwise, it returnsFalse
.
-
solution :: [Int]
:solution = take 18 $ filter isDisarium [0, 1 ..]
This line defines a list named
solution
that contains the first 18 Disarium numbers starting from 0.[0, 1 ..]
: Creates an infinite list of non-negative integers.filter isDisarium
: Filters the list, keeping only the numbers that satisfy theisDisarium
predicate.take 18
: Takes the first 18 elements from the filtered list.
In summary, this code provides a way to check if a given integer is a Disarium number (isDisarium
function) and generates a list of the first 18 Disarium numbers (solution
list).
Source code in the haskell programming language
module Disarium
where
import Data.Char ( digitToInt)
isDisarium :: Int -> Bool
isDisarium n = (sum $ map (\(c , i ) -> (digitToInt c ) ^ i )
$ zip ( show n ) [1 , 2 ..]) == n
solution :: [Int]
solution = take 18 $ filter isDisarium [0, 1 ..]
You may also check:How to resolve the algorithm Integer comparison step by step in the Brat programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the Wren programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the Nanoquery programming language