How to resolve the algorithm 9 billion names of God the integer step by step in the Haskell programming language
How to resolve the algorithm 9 billion names of God the integer step by step in the Haskell programming language
Table of Contents
Problem Statement
This task is a variation of the short story by Arthur C. Clarke. (Solvers should be aware of the consequences of completing this task.) In detail, to specify what is meant by a “name”:
Display the first 25 rows of a number triangle which begins: Where row
n
{\displaystyle n}
corresponds to integer
n
{\displaystyle n}
, and each column
C
{\displaystyle C}
in row
m
{\displaystyle m}
from left to right corresponds to the number of names beginning with
C
{\displaystyle C}
. A function
G ( n )
{\displaystyle G(n)}
should return the sum of the
n
{\displaystyle n}
-th row. Demonstrate this function by displaying:
G ( 23 )
{\displaystyle G(23)}
,
G ( 123 )
{\displaystyle G(123)}
,
G ( 1234 )
{\displaystyle G(1234)}
, and
G ( 12345 )
{\displaystyle G(12345)}
.
Optionally note that the sum of the
n
{\displaystyle n}
-th row
P ( n )
{\displaystyle P(n)}
is the integer partition function. Demonstrate this is equivalent to
G ( n )
{\displaystyle G(n)}
by displaying:
P ( 23 )
{\displaystyle P(23)}
,
P ( 123 )
{\displaystyle P(123)}
,
P ( 1234 )
{\displaystyle P(1234)}
, and
P ( 12345 )
{\displaystyle P(12345)}
.
If your environment is able, plot
P ( n )
{\displaystyle P(n)}
against
n
{\displaystyle n}
for
n
1 … 999
{\displaystyle n=1\ldots 999}
.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 9 billion names of God the integer step by step in the Haskell programming language
Haskell Code Overview:
This Haskell code defines functions for generating a set of triangular matrix rows, accumulating them into a cumulative matrix (cumu
), and calculating the sum of the elements along the main diagonal of the matrix.
Creating Triangular Matrix Rows (rows
Function):
- The
rows
function uses themapAccumL
function to generate a list of triangular matrix rows. - It accumulates the rows in the list, starting with an empty list.
- The accumulator function
f
takes two arguments: the current row (row
) and the accumulated rows (r
). - It generates a new row
new_row
by taking the heads of the accumulated rows. - It also generates a list
rr
by removing the heads from the accumulated rows. - The
tailKeepOne
function is used to remove the first element from a list, or to keep the entire list if it has only one element.
Creating Cumulative Matrix (cumu
Function):
- The
cumu
function creates a cumulative matrix by applying thescanl
function to the rows matrix. scanl
accumulates the elements of the rows matrix using the addition operator, starting with an initial value of 0.
Calculating Diagonal Sums (sums
Function):
- The
sums
function calculates the sum of the elements along the main diagonal of the cumulative matrix. - It accesses the appropriate element of the cumulative matrix using the index
n
. - An alternative, but faster, implementation is provided as a comment.
Main Function:
- The
main
function:- Prints the first 10 rows of the triangular matrix.
- Prints the sums of the main diagonal elements for specific values of
n
.
Source code in the haskell programming language
import Data.List (mapAccumL)
cumu :: [[Integer]]
cumu = [1] : map (scanl (+) 0) rows
rows :: [[Integer]]
rows = snd $ mapAccumL f [] cumu where
f r row = (rr, new_row) where
new_row = map head rr
rr = map tailKeepOne (row:r)
tailKeepOne [x] = [x]
tailKeepOne (_:xs) = xs
sums n = cumu !! n !! n
--curiously, the following seems to be faster
--sums = sum . (rows!!)
main :: IO ()
main = do
mapM_ print $ take 10 rows
mapM_ (print.sums) [23, 123, 1234, 12345]
You may also check:How to resolve the algorithm Accumulator factory step by step in the Delphi programming language
You may also check:How to resolve the algorithm Animation step by step in the J programming language
You may also check:How to resolve the algorithm Church numerals step by step in the J programming language
You may also check:How to resolve the algorithm Egyptian division step by step in the Phix programming language
You may also check:How to resolve the algorithm Archimedean spiral step by step in the Mathematica/Wolfram Language programming language