How to resolve the algorithm Disarium numbers step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

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:

  1. Import Statement:

    import Data.Char (digitToInt)

    This statement imports the digitToInt function from the Data.Char module, which converts a character to its corresponding digit value.

  2. 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 returns True if n is a Disarium number, and False otherwise.

    • show n: Converts the integer n 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 of n with the corresponding natural numbers. For example, if n 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 become 1^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 returns True, indicating that n is a Disarium number. Otherwise, it returns False.
  3. 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 the isDisarium 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