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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "./big" for BigRat
import "./fmt" for Fmt

var harmonic = Fn.new { |n| (1..n).reduce(BigRat.zero) { |sum, i| sum + BigRat.one/i } }

BigRat.showAsInt = true
System.print("The first 20 harmonic numbers and the 100th, expressed in rational form, are:")
var numbers = (1..20).toList
numbers.add(100)
for (i in numbers) Fmt.print("$3d : $s", i, harmonic.call(i))

System.print("\nThe first harmonic number to exceed the following integers is:")
var i = 1
var limit = 10
var n = 1
var h = 0
while (true) {
    h = h + 1/n
    if (h > i) {
        Fmt.print("integer = $2d  -> n = $,6d  ->  harmonic number = $9.6f (to 6dp)", i, n, h)
        i = i + 1
        if (i > limit) return
    }
    n = n + 1
}


  

You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the C programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the Perl programming language
You may also check:How to resolve the algorithm Huffman coding step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Subleq step by step in the C# programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Uxntal programming language