How to resolve the algorithm Harmonic series step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Harmonic series step by step in the Haskell programming language

Table of Contents

Problem Statement

In mathematics, the n-th harmonic number is the sum of the reciprocals of the first n natural numbers: The series of harmonic numbers thus obtained is often loosely referred to as the harmonic series. Harmonic numbers are closely related to the Riemann zeta function, and roughly approximate the natural logarithm function; differing by γ (lowercase Gamma), the Euler–Mascheroni constant. The harmonic series is divergent, albeit quite slowly, and grows toward infinity.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Harmonic series step by step in the Haskell programming language

Harmonic Series

The code calculates the harmonic series, which is the series of reciprocals of natural numbers (1, 1/2, 1/3, ...).

Code Explanation

  • Importing Libraries:

    • Data.List: Used for the find function.
    • Data.Ratio: Used for representing ratios.
  • Harmonic Series Calculation:

    • harmonic :: [Rational]: Defines a function that generates the harmonic series by recursively adding the reciprocal of the next natural number to the previous term. It starts with the first term [1].
  • Testing:

    • main :: IO (): The main function performs various tests on the harmonic series:
      • Printing First 20 Terms: It prints the first 20 terms of the series using mapM_.
      • 100th Term: It prints the 100th term using indexing (!!) and showRatio.
      • Terms Above Threshold Values: It prints the one-based index of the first term that exceeds a given threshold value. This is achieved by:
        • Creating indexedHarmonic, which pairs the indices with the series terms.
        • Using fmap to apply showFirstLimit and a lambda function to find the index of the first term above the threshold.
        • Printing the result using mapM_.
  • Display Formatting:

    • showFirstLimit :: Int -> Maybe (Int, Rational) -> String: Formats the message for the term above threshold tests.
    • showRatio :: Ratio Integer -> String: Formats a ratio as a string (e.g., 3/4).

Example Output:

First 20 terms:
1/1
1/2
1/3
1/4
1/5
1/6
1/7
1/8
1/9
1/10
1/11
1/12
1/13
1/14
1/15
1/16
1/17
1/18
1/19
1/20

100th term:
1/134

One-based indices of first terms above threshold values:
Term 3 is the first above 1
Term 5 is the first above 2
Term 9 is the first above 3
Term 13 is the first above 4
Term 18 is the first above 5
Term 24 is the first above 6
Term 31 is the first above 7
Term 39 is the first above 8
Term 47 is the first above 9
Term 56 is the first above 10

Source code in the haskell programming language

import Data.List (find)
import Data.Ratio

--------------------- HARMONIC SERIES --------------------

harmonic :: [Rational]
harmonic =
  scanl1
    (\a x -> a + 1 / x)
    [1 ..]

-------------------------- TESTS -------------------------
main :: IO ()
main = do
  putStrLn "First 20 terms:"
  mapM_ putStrLn $
    showRatio <$> take 20 harmonic

  putStrLn "\n100th term:"
  putStrLn $ showRatio (harmonic !! 99)
  putStrLn ""

  putStrLn "One-based indices of first terms above threshold values:"
  let indexedHarmonic = zip [0 ..] harmonic
  mapM_
    putStrLn
    $ fmap
      ( showFirstLimit
          <*> \n -> find ((> n) . snd) indexedHarmonic
      )
      [1 .. 10]

-------------------- DISPLAY FORMATTING ------------------

showFirstLimit n (Just (i, r)) =
  "Term "
    <> show (succ i)
    <> " is the first above "
    <> show (numerator n)

showRatio :: Ratio Integer -> String
showRatio =
  ((<>) . show . numerator)
    <*> (('/' :) . show . denominator)


  

You may also check:How to resolve the algorithm Hello world/Graphical step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Factorial step by step in the langur programming language
You may also check:How to resolve the algorithm Even or odd step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Character codes step by step in the Delphi programming language