How to resolve the algorithm Harshad or Niven series step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Harshad or Niven series step by step in the Haskell programming language

Table of Contents

Problem Statement

The Harshad or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits. For example,   42   is a Harshad number as   42   is divisible by   (4 + 2)   without remainder. Assume that the series is defined as the numbers in increasing order.

The task is to create a function/method/procedure to generate successive members of the Harshad sequence. Use it to:

Show your output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Harshad or Niven series step by step in the Haskell programming language

The code in Haskell finds all Harshad numbers up to a given number. A Harshad number is a number that is divisible by the sum of its digits.

The first part of the code uses the Data.Char library to convert a character to an integer. The digitToInt function takes a character and returns the corresponding integer. The show function converts a number to a string. The map function applies a function to each element of a list. The sum function adds up all the elements of a list. The filter function removes all the elements of a list that do not satisfy a predicate. The mod function returns the remainder of the division of two numbers. The <*> operator is the infix version of the liftM2 function, which applies a function to two values.

The second part of the code uses the Data.List library to create an infinite list of numbers. The unfoldr function takes a function that takes a value and returns a Maybe pair of the next value and the next state. The swap function swaps the two elements of a pair. The quotRem function returns the quotient and remainder of the division of two numbers. The flip function flips the order of the arguments of a function. The bool function returns the first argument if the second argument is True, and the second argument if the second argument is False.

The digitSum function takes a number and returns the sum of its digits. The rem function returns the remainder of the division of two numbers.

The main function prints the first 20 Harshad numbers and the first Harshad number greater than 1000.

Source code in the haskell programming language

import Data.Char (digitToInt)

harshads :: [Int]
harshads =
  let digsum = sum . map digitToInt . show
  in filter ((0 ==) . (mod <*> digsum)) [1 ..]

main :: IO ()
main = mapM_ print [take 20 harshads, [(head . filter (> 1000)) harshads]]


import Data.List (unfoldr)
import Data.Tuple (swap)
import Data.Bool (bool)

harshads :: [Int]
harshads = filter ((0 ==) . (rem <*> digitSum)) [1 ..]

digitSum :: Int -> Int
digitSum =
  sum . unfoldr ((bool Nothing . Just . swap . flip quotRem 10) <*> (0 <))

main :: IO ()
main = mapM_ print $ [take 20, take 1 . dropWhile (<= 1000)] <*> [harshads]


  

You may also check:How to resolve the algorithm Graph colouring step by step in the Julia programming language
You may also check:How to resolve the algorithm Sort stability step by step in the REXX programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the PHP programming language
You may also check:How to resolve the algorithm Tau number step by step in the Python programming language
You may also check:How to resolve the algorithm Floyd's triangle step by step in the TXR programming language