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

m
syntax highlighting fixup automation
(Added a Scheme implementation.)
m (syntax highlighting fixup automation)
Line 34:
=={{header|Burlesque}}==
Subject to double rounding error, evaluates 33 partial terms in a series
<langsyntaxhighlight lang="burlesque">33ro{JroJJJL[\/JL[\/nr\/{-1}\/.*FLJL[ro?^?*\/JL[{2}\/.*FL\/?^?*\/{1.+}m[J{lg}m[\/?/?*++\/1 3.0./\/?^.*}m[++-2 3 2lg.*./.*2lg2./.+</langsyntaxhighlight>
 
{{out}}
Line 42:
=={{header|C}}==
===Single precision===
<langsyntaxhighlight lang="c">/*********************************************
Subject: Comparing five methods for
computing Euler's constant 0.5772...
Line 162:
 
printf("\n\nC = 0.57721566490153286...\n");
}</langsyntaxhighlight>
 
{{out|output}}
Line 196:
From first principles
 
<langsyntaxhighlight lang="c">/**************************************************
Subject: Computation of Euler's constant 0.5772...
with the Brent-McMillan algorithm B1,
Line 373:
tim = clock() - tim;
printf("time: %.7f s\n",((double)tim)/CLOCKS_PER_SEC);
}</langsyntaxhighlight>
 
{{out|output}}
Line 382:
 
===The easy way===
<langsyntaxhighlight lang="c">/*******************************************
Subject: Euler's constant 0.5772...
tested : tcc-0.9.27 with mpfr 4.1.0
Line 409:
tim = clock() - tim;
gmp_printf ("time: %.7f s\n",((double)tim)/CLOCKS_PER_SEC);
}</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
===Single precision===
<langsyntaxhighlight lang="freebasic">'**********************************************
'Subject: Comparing five methods for
' computing Euler's constant 0.5772...
Line 533:
?
? "C = 0.57721566490153286..."
end</langsyntaxhighlight>
 
{{out|output}}
Line 566:
===Multi precision===
From first principles
<langsyntaxhighlight lang="freebasic">'***************************************************
'Subject: Computation of Euler's constant 0.5772...
' with the Brent-McMillan algorithm B1,
Line 737:
 
gmp_printf (!"time: %.7f s\n", TIMER - tim)
end</langsyntaxhighlight>
 
{{out|output}}
Line 746:
 
===The easy way===
<langsyntaxhighlight lang="freebasic">' ******************************************
'Subject: Euler's constant 0.5772...
'tested : FreeBasic 1.08.1 with mpfr 4.1.0
Line 768:
 
gmp_printf (!"time: %.7f s\n", TIMER - tim)
end</langsyntaxhighlight>
 
 
Line 776:
'''Works with gojq, the Go implementation of jq'''
 
<langsyntaxhighlight lang="jq"># Bailey, 1988
def bailey($n; $eps):
pow(2; $n) as $n2
Line 786:
| .b = .a
| .a += (.r * .h) )
| (.a * $n2 / ($n2|exp) ) - ($n * (2|log)) ;</langsyntaxhighlight>
{{out}}
<pre>
Line 795:
=={{header|Julia}}==
{{trans|PARI/GP}}
<langsyntaxhighlight lang="julia">display(MathConstants.γ) # γ = 0.5772156649015...
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">N[EulerGamma, 1000]</langsyntaxhighlight>
{{out}}
<pre>0.57721566490153286060651209008240243104215933593992359880576723488486772677766467093694706329174674
Line 813:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import std/math
 
const n = 1e6
Line 822:
 
echo result - ln(n)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 830:
=={{header|PARI/GP}}==
built-in:
<langsyntaxhighlight lang="parigp">\l "euler_const.log"
\p 100
print("gamma ", Euler);
\q</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://en.wikipedia.org/wiki/Euler%27s_constant
Line 842:
use List::Util qw( sum );
 
print sum( map 1 / $_, 1 .. 1e6) - log 1e6, "\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 851:
===part 1 (max 12dp)===
Translation of Perl, with the same accuracy limitation
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Eulers_constant.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">C</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e6</span><span style="color: #0000FF;">)))-</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e6</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"gamma %.12f (max 12d.p. of accuracy)\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">C</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 863:
===part 2 (first principles)===
Translation of C, from first principles.
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- no mpfr_get_d_2exp() in mpfr.js as yet</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- mpfr_get_d_2exp(), mpfr_addmul_si()</span>
Line 951:
<span style="color: #004080;">string</span> <span style="color: #000000;">su</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">u</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e10</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"gamma %s (maxerr. 1e-%d)\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">su</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">e10</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 957:
</pre>
===part 3 (the easy way)===
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- no mpfr_const_euler() in mpfr.js as yet</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.1"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- mpfr_const_euler()</span>
Line 964:
<span style="color: #000000;">mpfr_const_euler</span><span style="color: #0000FF;">(</span><span style="color: #000000;">gamma</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"gamma %s (mpfr_const_euler)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">gamma</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
<small>(matches part 2 except for the 100th decimal place)</small>
{{out}}
Line 973:
=={{header|Picat}}==
===List comprehension===
<langsyntaxhighlight Picatlang="picat">main =>
Gamma = 0.57721566490153286060651209008240,
println(Gamma),
Line 981:
end.
 
e(N) = [1.0/I : I in 1..N].sum-log(N).</langsyntaxhighlight>
 
{{out}}
Line 995:
 
===Loop===
<langsyntaxhighlight Picatlang="picat">e2(N) = E-log(N) =>
E = 1,
foreach(I in 2..N)
E := E + 1/I
end.</langsyntaxhighlight>
 
 
{{trans|Rust}}
<langsyntaxhighlight Picatlang="picat">main =>
Gamma = 0.577215664901532860606512090082402,
println(gamma=Gamma),
Line 1,023:
end,
Gamma := Gamma + I*Term
end.</langsyntaxhighlight>
 
{{out}}
Line 1,053:
=={{header|Processing}}==
{{trans|C}}
<syntaxhighlight lang="processing">
<lang Processing>
/*********************************************
Subject: Comparing five methods for
Line 1,199:
println("C = 0.57721566490153286...\n");
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,235:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket/base
 
(require math/number-theory
Line 1,253:
(/ (bernoulli-number 2k) (* (expt n 2k) 2k)))))
 
(g)</langsyntaxhighlight>
 
{{out}}
Line 1,260:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line># 20211124 Raku programming solution
 
sub gamma (\N where N > 1) { # Vacca series https://w.wiki/4ybp
Line 1,274:
}
 
say gamma 23 ;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,281:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">n = 1e6
p (1..n).sum{ 1.0/_1 } - Math.log(n)</langsyntaxhighlight>
{{out}}
<pre>0.5772161649014507 #12 digits accurate
Line 1,289:
=={{header|Rust}}==
{{trans|Raku}}
<langsyntaxhighlight lang="rust">// 20220322 Rust programming solution
 
fn gamma(N: u32) -> f64 { // Vacca series https://w.wiki/4ybp
Line 1,311:
fn main() {
println!("{}", gamma(23));
}</langsyntaxhighlight>
{{out}}
<pre>0.5772149198434514</pre>
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<langsyntaxhighlight lang="scheme">; Procedure to compute factorial.
(define fact
(lambda (n)
Line 1,333:
 
; Show Euler's gamma constant computed at log(100).
(printf "Euler's gamma constant: ~a~%" (gamma 100))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,341:
=={{header|Sidef}}==
Built-in:
<langsyntaxhighlight lang="ruby"># 100 decimals of precision
local Num!PREC = 4*100
say Num.EulerGamma</langsyntaxhighlight>
{{out}}
<pre>
Line 1,350:
 
Several formulas:
<langsyntaxhighlight lang="ruby">const n = (ARGV ? Num(ARGV[0]) : 50) # number of iterations
 
define ℯ = Num.e
Line 1,400:
display(sum(1..n, {|n|
(-1)**n * floor(log2(n)) / n
}), γ)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,416:
=={{header|Vlang}}==
{{trans|C}}
<langsyntaxhighlight lang="vlang">import math
const eps = 1e-6
fn main() {
Line 1,513:
println("err ${a:0.16f}\ngamma ${h+a:0.16f}\nk = ${n+m}")
println("\nC = 0.57721566490153286...")
}</langsyntaxhighlight>
{{out}}
<pre>Same as C entry</pre>
Line 1,522:
{{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.
<langsyntaxhighlight lang="ecmascript">import "./fmt" for Fmt
 
var eps = 1e-6
Line 1,608:
}
Fmt.print("err $0.14f\ngamma $0.14f\nk = $d", a, h + a, n + m)
System.print("\nC = 0.57721566490153286...")</langsyntaxhighlight>
 
{{out}}
Line 1,642:
{{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.
<langsyntaxhighlight lang="ecmascript">import "./gmp" for Mpf
 
var euler = Fn.new { |n, r, s, t|
Line 1,685:
euler.call(9375, 91, 68, 40)
euler.call(18750, 98, 73, 43)
System.print("\nTook %(System.clock - start) seconds.")</langsyntaxhighlight>
 
{{out}}
Line 1,701:
</pre>
===The easy way (Embedded)===
<langsyntaxhighlight lang="ecmascript">import "./gmp" for Mpf
 
var prec = (101/0.30103).round
var gamma = Mpf.euler(prec)
System.print(gamma.toString(10, 100))</langsyntaxhighlight>
 
{{out}}
10,333

edits