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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Harmonic series step by step in the Forth 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 Forth programming language

Source code in the forth programming language

warnings off

1.000.000.000.000.000 drop constant 1.0fx  \ fractional part is 15 decimal digits.

: .h  ( n -- )
    s>d <#  14 for # next  [char] . hold #s #> type space ;

1.0fx 1 2constant first-harmonic

: round  5 + 10 / ;

: next-harmonic ( h n -- h' n' )
    1+ tuck [ 1.0fx 10 * ] literal swap / round + swap ;

: task1
    first-harmonic  19 for  over cr .h next-harmonic  next 2drop ;

: task2
    first-harmonic
    11 1 do
        begin over i 1.0fx * <= while
            next-harmonic
        repeat
        dup .
    loop 2drop ;

." The first 10 harmonic numbers: " task1 cr cr
." The nth index of the first harmonic number that exceeds the nth integer: " cr task2 cr
bye


  

You may also check:How to resolve the algorithm Bernoulli numbers step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Literals/String step by step in the Groovy programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the Elixir programming language
You may also check:How to resolve the algorithm Program termination step by step in the JavaScript programming language
You may also check:How to resolve the algorithm SHA-256 Merkle tree step by step in the Haskell programming language