Can you find Euler–Mascheroni constant in lua?

simple question
is it possible to calculate Euler–Mascheroni constant in lua?
or if it isn’t possible then I will just resort to 0.577215664901 ig

A post on math stack exchange shows a pretty simple way to calculate it with a margin of error of n^(-4) using the harmonic number sequence and some fancy math tricks

function H(N) --harmonic number sequence
    local sum = 1
    for n = 2,N do
        sum = sum + 1/n
    end
    return sum
end

function gamma(n)
    return 2*H(n) - H(n^2+n-1)/6 - 5*H(n^2+n)/6 -- + O(n^(-4)) margin of error
end

This is super slow, inaccurate and inefficient but is pretty cool ig
There are way better ways to calculate it and its a very interesting topic you can google about which are absolutely possible to calculate using lua

I would personally just use a known value like the one you said above (0.577215664901) if you need it for anything, but otherwise its a cool thing to explore

Yea I think I’ll stick to this unless someone finds a faster solution
I tried looking up how to calculate it before posting this but nothing yet

1 Like