27 lines
709 B
Text
27 lines
709 B
Text
require "bignum"
|
|
require "table2"
|
|
local fmt = require "fmt"
|
|
|
|
local one <const> = bigrat.int(1)
|
|
|
|
local harmonic = |n| -> range(1, n):map(|i| -> one / i):sum()
|
|
|
|
print("The first 20 harmonic numbers and the 100th, expressed in rational form, are:")
|
|
local numbers = range(1, 20)
|
|
numbers:insert(100)
|
|
for numbers as i do fmt.print("%3d : %s", i, harmonic(i)) end
|
|
|
|
print("\nThe first harmonic number to exceed the following integers is:")
|
|
local i = 1
|
|
local limit <const> = 10
|
|
local n = 1
|
|
local h = 0
|
|
while true do
|
|
h += 1 / n
|
|
if h > i then
|
|
fmt.print("integer = %2d -> n = %,6s -> harmonic number = %9.6f (to 6dp)", i, n, h)
|
|
i += 1
|
|
if i > limit then break end
|
|
end
|
|
n += 1
|
|
end
|