How to resolve the algorithm Own digits power sum step by step in the Haskell programming language
How to resolve the algorithm Own digits power sum step by step in the Haskell programming language
Table of Contents
Problem Statement
For the purposes of this task, an own digits power sum is a decimal integer which is N digits long and is equal to the sum of its individual digits raised to the power N.
The three digit integer 153 is an own digits power sum because 1³ + 5³ + 3³ = 1 + 125 + 27 = 153.
Find and show here all own digits power sums for N = 3 to N = 8 inclusive. Optionally, do the same for N = 9 which may take a while for interpreted languages.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Own digits power sum step by step in the Haskell programming language
This Haskell program defines functions to find the power sums of a number's own digits. It also includes a generic function for generating combinations with repetitions, which is used in the main logic.
Explanation:
-
ownDigitsPowerSums
:- Takes an integer
n
. - Generates combinations of digits from 0 to 9 with repetition allowed (
ns
). - Applies the
go
function to each combination to check if its power sum matches the original number. - Returns a sorted list of these matching combinations.
- Takes an integer
-
go
:- Converts the list
xs
of digits into a single integerm
by summing up each digit raised to the power ofn
. - Checks if the digits of
m
match the input digitsxs
. If they do, it returnsm
; otherwise, it returns an empty list.
- Converts the list
-
digitsMatch
:- Converts the given value
n
to a string and extracts its individual digits. - Compares the sorted list of digits in
ds
with the sorted list of digits derived fromn
. ReturnsTrue
if they are equal, indicating that the power sum matches the original number.
- Converts the given value
-
combsWithRep
:- A generic function to generate combinations with replacement.
- Takes an integer
k
(number of elements in combination) and a list of elementsxs
. - Uses a recursive helper function
comb
to construct the combinations.
-
digits
:- Converts the given value
n
to a string and returns a list of its individual digits.
- Converts the given value
Example Usage:
The main
function demonstrates the program by:
- Printing the power sums of numbers in the range [3, 8].
- Printing the power sums of the number 9 (which has a special property).
Source code in the haskell programming language
import Data.List (sort)
------------------- OWN DIGITS POWER SUM -----------------
ownDigitsPowerSums :: Int -> [Int]
ownDigitsPowerSums n = sort (ns >>= go)
where
ns = combsWithRep n [0 .. 9]
go xs
| digitsMatch m xs = [m]
| otherwise = []
where
m = foldr ((+) . (^ n)) 0 xs
digitsMatch :: Show a => a -> [Int] -> Bool
digitsMatch n ds =
sort ds == sort (digits n)
--------------------------- TEST -------------------------
main :: IO ()
main = do
putStrLn "N ∈ [3 .. 8]"
mapM_ print ([3 .. 8] >>= ownDigitsPowerSums)
putStrLn ""
putStrLn "N=9"
mapM_ print $ ownDigitsPowerSums 9
------------------------- GENERIC ------------------------
combsWithRep ::
(Eq a) =>
Int ->
[a] ->
[[a]]
combsWithRep k xs = comb k []
where
comb 0 ys = ys
comb n [] = comb (pred n) (pure <$> xs)
comb n peers = comb (pred n) (peers >>= nextLayer)
where
nextLayer ys@(h : _) =
(: ys) <$> dropWhile (/= h) xs
digits :: Show a => a -> [Int]
digits n = (\x -> read [x] :: Int) <$> show n
You may also check:How to resolve the algorithm Higher-order functions step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Quadratic step by step in the Factor programming language
You may also check:How to resolve the algorithm RPG attributes generator step by step in the jq programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Dart programming language
You may also check:How to resolve the algorithm Average loop length step by step in the Wren programming language