Euler's constant 0.5772...: Difference between revisions

Added Easylang
(Added Easylang)
 
(13 intermediate revisions by 8 users not shown)
Line 858:
Gives: 0.5772156649484047
Compared to the true value: 0.5772156649015328...
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
This programs demonstrates the basic method of calculating the Euler number. It illustrates how the number of iterations increases the accuracy.
<syntaxhighlight lang="Delphi">
 
function ComputeEuler(N: int64): double;
{Compute Eurler number with N-number of iterations}
var I: integer;
var A: double;
begin
Result:=0;
for I:=1 to N-1 do
Result:=Result + 1 / I;
A:=Ln(N + 0.5 + 1/(24.0*N));
Result:=Result-A;
end;
 
 
 
procedure ShowEulersNumber(Memo: TMemo);
{Show Euler numbers at various levels of precision}
var Euler,G,A,Error: double;
var N: integer;
const Correct =0.57721566490153286060651209008240243104215933593992;
 
procedure ShowEulerError(N: int64);
{Show Euler number and Error}
begin
Euler:=ComputeEuler(N);
Error:=Correct-Euler;
Memo.Lines.Add('N = '+FloatToStrF(N,ffNumber,18,0));
Memo.Lines.Add('Euler='+FloatToStrF(Euler,ffFixed,18,18));
Memo.Lines.Add('Error='+FloatToStrF(Error,ffFixed,18,18));
Memo.Lines.Add('');
end;
 
begin
{Compute Euler number with iterations ranging 10 to 10^9}
for N:=1 to 9 do
begin
ShowEulerError(Trunc(Power(10,N)));
end;
 
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
N = 10
Euler=0.477196250122325250
Error=0.100019414779207616
 
N = 100
Euler=0.567215644212103243
Error=0.010000020689429618
 
N = 1,000
Euler=0.576215664880711742
Error=0.001000000020821119
 
N = 10,000
Euler=0.577115664901475256
Error=0.000100000000057605
 
N = 100,000
Euler=0.577205664901386584
Error=0.000010000000146277
 
N = 1,000,000
Euler=0.577214664900591146
Error=0.000001000000941715
 
N = 10,000,000
Euler=0.577215564898391875
Error=0.000000100003140985
 
N = 100,000,000
Euler=0.577215654895336883
Error=0.000000010006195978
 
N = 1,000,000,000
Euler=0.577215664060567235
Error=0.000000000840965626
 
Elapsed Time: 4.716 Sec.
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc gethn n .
i = 1
while i <= n
hn += 1 / i
i += 1
.
return hn
.
e = 2.718281828459045235
n = 10e8
numfmt 9 0
print gethn n - log10 n / log10 e
</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn Euler
long n = 10000000, k
double a, h = 1.0
for k = 2 to n
h += 1 / k
next
a = log( n + 0.5 + 1 / ( 24 * n ) )
printf @"From the definition, err. 3e-10"
printf @"Hn %.15f", h
printf @"gamma %.15f", h - a
printf @"k = %ld\n", n
printf @"C %.15f", 0.5772156649015328
end fn
 
CFTimeInterval t
t = fn CACurrentMediaTime
fn Euler
printf @"\vCompute time: %.3f ms",(fn CACurrentMediaTime-t)*1000
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
From the definition, err. 3e-10
Hn 16.695311365857272
gamma 0.577215664898954
k = 10000000
 
C 0.577215664901533
Compute time: 67.300 ms
</pre>
 
Line 883 ⟶ 1,026:
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
<syntaxhighlight>
/**
* Using a simple formula derived from Hurwitz zeta function,
* as described on https://en.wikipedia.org/wiki/Euler%27s_constant,
* gives a result accurate to 1112 decimal places.
*/
public class EulerConstant {
Line 895 ⟶ 1,037:
System.out.println(gamma(1_000_000));
}
 
private static double gamma(int N) {
double gamma = 0.0;
Line 910 ⟶ 1,052:
}
</syntaxhighlight>
{{ out }}
<pre>0.5772156649007147</pre>
 
=={{header|jq}}==
Line 1,010 ⟶ 1,154:
8328690001558193988042707411542227819716523011073565833967348717650491941812300040654693142999297779
569303100503086303418569803231083691640025892970890985486825777364288253954925873629596133298574739302</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
%gamma,numer;
</syntaxhighlight>
{{out}}
<pre>
0.5772156649015329
</pre>
 
=={{header|Nim}}==
Line 1,431 ⟶ 1,584:
C = 0.57721566490153286...
</pre>
=={{header|Python}}==
<syntaxhighlight lang="python">
# /**************************************************
# Subject: Computation of Euler's constant 0.5772...
# with Euler's Zeta Series.
# tested : Python 3.11
# -------------------------------------------------*/
 
from scipy import special as s
 
def eulers_constant(n):
k = 2
euler = 0
while k <= n:
euler += (s.zeta(k) - 1)/k
k += 1
return 1 - euler
 
print(eulers_constant(47))
</syntaxhighlight>
{{out}}
<pre>
0.577215664901533
</pre>
=={{header|Racket}}==
 
Line 1,477 ⟶ 1,653:
<pre>
0.5772149198434515
</pre>
 
=={{header|RPL}}==
{{trans|FreeBASIC}}
'''From the definition, with Negoi's convergence improvement'''
≪ → n
≪ 1
2 n '''FOR''' k k INV + '''NEXT'''
n .5 + 24 n * INV + LN -
≫ ≫ ‘<span style="color:blue">GAMMA1</span>’ STO
'''Sweeney method'''
≪ 0 OVER R→C 1E-6 → n s eps
≪ n 1
'''DO'''
1 + SWAP
OVER / n * SWAP
DUP2 /
IF OVER 2 MOD THEN (0,1) * END 's' STO+
'''UNTIL''' OVER eps < '''END'''
DROP2
s C→R SWAP - n LN -
≫ ≫ ‘<span style="color:blue">GAMMA2</span>’ STO
 
400 <span style="color:blue">GAMMA1</span>
21 <span style="color:blue">GAMMA2</span>
{{out}}
<pre>
2: .57721566456
1: .57717756228
</pre>
 
Line 1,513 ⟶ 1,718:
{{out}}
<pre>0.5772149198434514</pre>
 
=={{header|Scala}}==
<syntaxhighlight lang="Scala">
/**
* Using a simple formula derived from Hurwitz zeta function,
* as described on https://en.wikipedia.org/wiki/Euler%27s_constant,
* gives a result accurate to 11 decimal places: 0.57721566490...
*/
 
object EulerConstant extends App {
 
println(gamma(1_000_000))
 
private def gamma(N: Int): Double = {
val sumOverN = (1 to N).map(1.0 / _).sum
sumOverN - Math.log(N) - 1.0 / (2 * N)
}
 
}
</syntaxhighlight>
{{out}}
<pre>
0.5772156649007153
</pre>
 
=={{header|Scheme}}==
{{works with|Chez Scheme}}
Line 1,537 ⟶ 1,767:
Euler's gamma constant: 0.5772156649015328
</pre>
 
 
 
=={{header|Sidef}}==
Line 1,721 ⟶ 1,953:
{{libheader|Wren-fmt}}
Note that, whilst internally double arithmetic is carried out to the same precision as C (Wren is written in C), printing doubles is effectively limited to a maximum of 14 decimal places.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var eps = 1e-6
Line 1,841 ⟶ 2,073:
{{libheader|Wren-gmp}}
The display is limited to 100 digits for all four examples as I couldn't see much point in showing them all.
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpf
 
var euler = Fn.new { |n, r, s, t|
Line 1,900 ⟶ 2,132:
</pre>
===The easy way (Embedded)===
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpf
 
var prec = (101/0.30103).round
1,983

edits