How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

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.
  • hc Function:
    • The hc function takes an integer n as input and returns an Array of integers. The Array is constructed using the listArray 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 to n, the i-th element is calculated using the f function applied to the previous element of the array and i.
  • f Function:
    • The f function takes two integers a and i as input and returns an integer.
    • It calculates the value at i in the hc array using the value at i - 1 and the value at i - a(i - 1).
  • printMaxima Function:
    • The printMaxima function takes a tuple (n, (pos, m)) as input and prints the maximum value m of hc between 2^n and 2^(n + 1) at position pos.
  • main Function:
    • The main function is the entry point of the program.
    • It calculates the following:
      • hca: The hc array with a length of 2^20.
      • hc': A function that takes an integer n and returns the value of hc(n) divided by n.
      • maxima: A list of tuples (pos, m) where pos is the position of the maximum value and m is the maximum value of hc between 2^n and 2^(n + 1) for n from 0 to 19.
      • max: A function that takes a list of integers seq and returns the maximum value and its position in seq.
      • powers: A list of lists of integers where each sublist contains the powers of 2 from 2^n to 2^(n + 1) - 1 for n from 0 to 19.
      • mallows: The smallest integer n such that hc(n) < 0.55.
    • Finally, the program prints the maxima and Mallows's number.

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