How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Haskell programming language
How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Haskell programming language
Table of Contents
Problem Statement
The definition of the sequence is colloquially described as: Note that indexing for the description above starts from alternately the left and right ends of the list and starts from an index of one. A less wordy description of the sequence is: The sequence begins: Interesting features of the sequence are that:
The sequence is so named because John Conway offered a prize of $10,000 to the first person who could find the first position, p in the sequence where It was later found that Hofstadter had also done prior work on the sequence. The 'prize' was won quite quickly by Dr. Colin L. Mallows who proved the properties of the sequence and allowed him to find the value of n (which is much smaller than the 3,173,375,556 quoted in the NYT article).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Haskell programming language
The provided Haskell code is an implementation of Mallows's problem, which is about finding the maximum of a certain function hc
and its derivative. The code also calculates Mallows's number, which is the smallest integer n
such that hc(n) < 0.55
.
Let's go through the code step by step:
- Data Structures:
- The code uses several data structures from the Haskell standard library:
Data.List
: Provides functions for working with lists.Data.Ord
: Provides ordering functions.Data.Array
: Provides functions for working with arrays.Text.Printf
: Provides functions for formatted printing.
- The code uses several data structures from the Haskell standard library:
hc
Function:- The
hc
function takes an integern
as input and returns anArray
of integers. TheArray
is constructed using thelistArray
function, which creates an array from a list. - The list used to initialize the array is constructed as follows:
- The first element is 1.
- The second element is 1.
- For
i
from 3 ton
, thei
-th element is calculated using thef
function applied to the previous element of the array andi
.
- The
f
Function:- The
f
function takes two integersa
andi
as input and returns an integer. - It calculates the value at
i
in thehc
array using the value ati - 1
and the value ati - a(i - 1)
.
- The
printMaxima
Function:- The
printMaxima
function takes a tuple(n, (pos, m))
as input and prints the maximum valuem
ofhc
between2^n
and2^(n + 1)
at positionpos
.
- The
main
Function:- The
main
function is the entry point of the program. - It calculates the following:
hca
: Thehc
array with a length of 2^20.hc'
: A function that takes an integern
and returns the value ofhc(n)
divided byn
.maxima
: A list of tuples(pos, m)
wherepos
is the position of the maximum value andm
is the maximum value ofhc
between2^n
and2^(n + 1)
forn
from 0 to 19.max
: A function that takes a list of integersseq
and returns the maximum value and its position inseq
.powers
: A list of lists of integers where each sublist contains the powers of 2 from2^n
to2^(n + 1) - 1
forn
from 0 to 19.mallows
: The smallest integern
such thathc(n) < 0.55
.
- Finally, the program prints the maxima and Mallows's number.
- The
Source code in the haskell programming language
import Data.List
import Data.Ord
import Data.Array
import Text.Printf
hc :: Int -> Array Int Int
hc n = arr
where arr = listArray (1, n) $ 1 : 1 : map (f (arr!)) [3 .. n]
f a i = a (a $ i - 1) + a (i - a (i - 1))
printMaxima :: (Int, (Int, Double)) -> IO ()
printMaxima (n, (pos, m)) =
printf "Max between 2^%-2d and 2^%-2d is %1.5f at n = %6d\n"
n (n + 1) m pos
main = do
mapM_ printMaxima maxima
printf "Mallows's number is %d\n" mallows
where
hca = hc $ 2^20
hc' n = fromIntegral (hca!n) / fromIntegral n
maxima = zip [0..] $ map max powers
max seq = maximumBy (comparing snd) $ zip seq (map hc' seq)
powers = map (\n -> [2^n .. 2^(n + 1) - 1]) [0 .. 19]
mallows = last.takeWhile ((< 0.55) . hc') $ [2^20, 2^20 - 1 .. 1]
You may also check:How to resolve the algorithm Abbreviations, simple step by step in the Tcl programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Sleeping Beauty problem step by step in the Haskell programming language
You may also check:How to resolve the algorithm Move-to-front algorithm step by step in the Ada programming language
You may also check:How to resolve the algorithm Vigenère cipher step by step in the Kotlin programming language