Rosetta Code- suggested task Harmonic Series
' In mathematics, the n-th harmonic number is the sum of the reciprocals of the first n natural numbers: ' Hn = 1 + 1/2 + 1/3 + ... + 1/n ' The harmonic series is divergent, albeit quite slowly, and grows toward infinity. ' Task ' Write code to generate harmonic numbers. ' Show the values of the first 20 harmonic numbers. ' Find and show the position in the series of the first value greater than the integers 1 through 10 ' In most languages single precision, 23 bits are used for mantissa. ' In double precision, 52 bits are used for mantissa. ' In LB there is no distinction, and the underlying Smalltalk implements essentially unlimited integers ' You can also use strings to hold essentially unlimited digits.
print "The first twenty harmonic numbers are:" h =0 for n = 1 to 20 h = h +1 /n print h, " by adding "; 1 /n timer 1000, [o] wait [o] timer 0 next n h = 1 : n = 2 for i =2 to 10 while h <i h =h +1 /n n =n +1 wend print "The first harmonic number greater than "; i; " is "; h; ", at position "; n -1 next i end
Resulting output
The first twenty harmonic numbers are: 1 by adding 1 1.5 by adding 0.5 1.83333333 by adding 0.33333333 2.08333333 by adding 0.25 2.28333333 by adding 0.2 2.45 by adding 0.16666667 2.59285714 by adding 0.14285714 2.71785714 by adding 0.125 2.82896825 by adding 0.11111111 2.92896825 by adding 0.1 3.01987734 by adding 0.90909091e-1 3.10321068 by adding 0.83333333e-1 3.18013376 by adding 0.76923077e-1 3.25156233 by adding 0.71428571e-1 3.31822899 by adding 0.66666667e-1 3.38072899 by adding 0.0625 3.43955252 by adding 0.58823529e-1 3.49510808 by adding 0.55555556e-1 3.54773966 by adding 0.52631579e-1 3.59773966 by adding 0.05 The first harmonic number greater than 2 is 2.08333333, at position 4 The first harmonic number greater than 3 is 3.01987734, at position 11 The first harmonic number greater than 4 is 4.0272452, at position 31 The first harmonic number greater than 5 is 5.00206827, at position 83 The first harmonic number greater than 6 is 6.00436671, at position 227 The first harmonic number greater than 7 is 7.0012741, at position 616 The first harmonic number greater than 8 is 8.00048557, at position 1674 The first harmonic number greater than 9 is 9.00020806, at position 4550 The first harmonic number greater than 10 is 10.000043, at position 12367
LB can be asked to show more places by using the function 'using('. You can also implement essentially unlimited precision by storing as long strings. Fun for mathematical challenges, but not needed for real-life precision. The fine-structure constant alpha has a value of 1/137.035999206. A measurement that has an accuracy of 81 parts per trillion.
The first twenty harmonic numbers are: 1.000000000000000 by adding 1.000000000000000 1.500000000000000 by adding 0.500000000000000 1.833333333333333 by adding 0.333333333333333 2.083333333333333 by adding 0.250000000000000 2.283333333333333 by adding 0.200000000000000 2.450000000000000 by adding 0.166666666666667 2.592857142857143 by adding 0.142857142857143 2.717857142857143 by adding 0.125000000000000 2.828968253968254 by adding 0.111111111111111 2.928968253968254 by adding 0.100000000000000 3.019877344877345 by adding 0.090909090909091 3.103210678210678 by adding 0.083333333333333 3.180133755133755 by adding 0.076923076923077 3.251562326562327 by adding 0.071428571428571 3.318228993228994 by adding 0.066666666666667 3.380728993228994 by adding 0.062500000000000 3.439552522640758 by adding 0.058823529411765 3.495108078196313 by adding 0.055555555555556 3.547739657143682 by adding 0.052631578947368 3.597739657143682 by adding 0.050000000000000 The first harmonic number greater than 2 is 2.083333333333333, at position 4 The first harmonic number greater than 3 is 3.019877344877345, at position 11 The first harmonic number greater than 4 is 4.027245195436520, at position 31 The first harmonic number greater than 5 is 5.002068272680166, at position 83 The first harmonic number greater than 6 is 6.004366708345568, at position 227 The first harmonic number greater than 7 is 7.001274097134162, at position 616 The first harmonic number greater than 8 is 8.000485571995781, at position 1674 The first harmonic number greater than 9 is 9.000208062931114, at position 4550 The first harmonic number greater than 10 is 10.000043008275778, at position 12367