Calculating the value of e: Difference between revisions

Content deleted Content added
Rdm (talk | contribs)
m →‎{{header|J}}: Remove "well, duh" statement.
Thundergnat (talk | contribs)
m syntax highlighting fixup automation
Line 13:
=={{header|11l}}==
{{trans|Python}}
<langsyntaxhighlight lang=11l>V e0 = 0.0
V e = 2.0
V n = 0
Line 26:
print(‘Real e = ’math:e)
print(‘Error = ’(math:e - e))
print(‘Number of iterations = ’n)</langsyntaxhighlight>
{{out}}
<pre>
Line 38:
The 'include' file FORMAT, to format a floating point number, can be found in:
[[360_Assembly_include|Include files 360 Assembly]].
<langsyntaxhighlight lang=360asm>* Calculating the value of e - 21/07/2018
CALCE PROLOG
LE F0,=E'0'
Line 65:
PG DC CL80' ' buffer
REGEQU
END CALCE </langsyntaxhighlight>
{{out}}
<pre>
Line 74:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight lang=Action!>INCLUDE "H6:REALMATH.ACT"
 
PROC Euler(REAL POINTER e)
Line 108:
Print("calculated e=") PrintRE(calc)
Print(" error=") PrintRE(diff)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Calculating_the_value_of_e.png Screenshot from Atari 8-bit computer]
Line 119:
=={{header|Ada}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=ada>with Ada.Text_IO; use Ada.Text_IO;
with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO;
 
Line 143:
New_Line;
 
end Euler;</langsyntaxhighlight>
{{out}}
<pre>
Line 151:
=={{header|ALGOL 68}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=algol68>BEGIN
# calculate an approximation to e #
LONG REAL epsilon = 1.0e-15;
Line 165:
DO SKIP OD;
print( ( "e = ", fixed( e, -17, 15 ), newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 174:
For the purposes of 32 bit floating point, the value seems to stabilise after summing c. 16 terms.
 
<langsyntaxhighlight lang=applescript>--------------- CALCULATING THE VALUE OF E ----------------
on run
Line 271:
foldl(add, 0, xs)
end sum</langsyntaxhighlight>
{{Out}}
<pre>2.718281828459</pre>
 
Or, as a single fold:
<langsyntaxhighlight lang=AppleScript>------------- APPROXIMATION OF THE VALUE OF E ------------
 
-- eApprox :: Int -> Float
Line 340:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<pre>2.718281828459</pre>
 
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang=text>?"E = "EXP(1)</langsyntaxhighlight>
{{Out}}
<pre>E = 2.71828183</pre>
Line 351:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>fact: 1
e: 2.0
e0: 0.0
Line 364:
]
 
print e</langsyntaxhighlight>
 
{{out}}
Line 371:
 
=={{header|Asymptote}}==
<langsyntaxhighlight lang=Asymptote>real n, n1;
real e1, e;
Line 385:
n *= n1;
}
write("The value of e = ", e);</langsyntaxhighlight>
{{out}}
<pre>The value of e = 2.71828182845905</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f CALCULATING_THE_VALUE_OF_E.AWK
BEGIN {
Line 406:
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 414:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight lang=freebasic>n = 1 : n1 = 1
e1 = 0 : e = 1 / 1
 
Line 425:
 
print "The value of e = "; e
end</langsyntaxhighlight>
{{out}}
<pre>The value of e = 2.71828182829</pre>
Line 432:
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<langsyntaxhighlight lang=QBasic>n = 1: n1 = 1
 
e! = 1 / 1
Line 443:
LOOP
 
PRINT "The value of e ="; e</langsyntaxhighlight>
{{out}}
<pre>The value of e = 2.718282</pre>
Line 449:
==={{header|True BASIC}}===
{{works with|QBasic}}
<langsyntaxhighlight lang=qbasic>LET n = 1
LET n1 = 1
 
Line 462:
 
PRINT "The value of e ="; e
END</langsyntaxhighlight>
{{out}}
<pre>The value of e = 2.7182818</pre>
Line 469:
{{works with|QBasic}}
{{works with|FreeBASIC| with #lang "qb"}}
<langsyntaxhighlight lang=yabasic>n = 1 : n1 = 1
e1 = 0 : e = 1 / 1
 
Line 479:
wend
 
print "The value of e = ", e</langsyntaxhighlight>
{{out}}
<pre>The value of e = 2.71828</pre>
Line 486:
=={{header|Befunge}}==
Befunge has no decimal capabilities, evaluates as fractions of 10^17
<langsyntaxhighlight lang=befunge>52*92*1->01p:01g1-:v v *_$101p011p54*21p>:11g1+:01g*01p:11p/21g1-:v v <
^ _$$>\:^ ^ p12_$>+\:#^_$554**/@</langsyntaxhighlight>
{{out}}
<pre>2718281828459045</pre>
Line 493:
 
=={{header|Burlesque}}==
<langsyntaxhighlight lang=burlesque>
blsq ) 70rz?!{10 100**\/./}ms36.+Sh'.1iash
2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274
</syntaxhighlight>
</lang>
 
=={{header|C}}==
===solution 1===
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <math.h>
 
Line 539:
 
return 0;
}</langsyntaxhighlight>
{{output}}
<pre>
Line 552:
===solution 2===
{{trans|Kotlin}}
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <math.h>
 
Line 569:
printf("e = %.15f\n", e);
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 577:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang=csharp>using System;
 
namespace CalculateE {
Line 596:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>e = 2.718281828459050</pre>
Line 602:
 
===Using Decimal type===
<langsyntaxhighlight lang=csharp>using System;
 
class Calc_E
Line 619:
Console.WriteLine(CalcE()); // Decimal precision result
}
}</langsyntaxhighlight>
{{out}}
<pre>2.71828182845905
Line 626:
{{libheader|System.Numerics}}
Automatically determines number of padding digits required for the arbitrary precision output. Can calculate a quarter million digits of '''e''' in under half a minute.
<langsyntaxhighlight lang=csharp>using System; using System.Numerics;
using static System.Math; using static System.Console;
 
Line 648:
WriteLine("partial: {0}...{1}", es.Substring(0, 46), es.Substring(es.Length - 45));
}
}</langsyntaxhighlight>
{{out}}
<pre>2.71828182845905
Line 658:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang=cpp>#include <iostream>
#include <iomanip>
#include <cmath>
Line 677:
cout << "e = " << setprecision(16) << e << endl;
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 685:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang=clojure>
;; Calculating the number e, euler-napier number.
;; We will use two methods
Line 717:
(time (with-precision 110 (method-e 200M)))
 
</syntaxhighlight>
</lang>
 
<pre>
Line 726:
=={{header|COBOL}}==
{{trans|C}}
<langsyntaxhighlight lang=COBOL> >>SOURCE FORMAT IS FIXED
IDENTIFICATION DIVISION.
PROGRAM-ID. EULER.
Line 751:
DISPLAY RESULT-MESSAGE.
STOP RUN.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 759:
=={{header|Commodore BASIC}}==
Commodore BASIC floats have a 40-bit mantissa, so we get max precision after just 11 iterations:
<langsyntaxhighlight lang=basic>
100 REM COMPUTE E VIA INVERSE FACTORIAL SUM
110 N = 11:REM NUMBER OF ITERATIONS
Line 768:
160 : E = E + 1/F
170 NEXT I
180 PRINT "AFTER" N "ITERATIONS, E =" E</langsyntaxhighlight>
 
{{Out}}
Line 778:
Common Lisp performs exact rational arithmetic, but printing those results out in decimal form is challenging; the built-in options require conversion to floating point values of relatively low precision. The QuickLisp module <tt>computable-reals</tt> makes printing out precise decimal representations easy, though:
 
<langsyntaxhighlight lang=lisp>(ql:quickload :computable-reals :silent t)
(use-package :computable-reals)
 
Line 788:
(setq e (+ e (/ 1 f))))
(format t "After ~a iterations, e = " *iterations*)
(print-r e 2570))</langsyntaxhighlight>
 
{{Out}}
Line 797:
 
=={{header|D}}==
<langsyntaxhighlight lang=d>import std.math;
import std.stdio;
 
Line 813:
} while (abs(e - e0) >= EPSILON);
writefln("e = %.15f", e);
}</langsyntaxhighlight>
{{out}}
<pre>e = 2.718281828459046</pre>
Line 822:
Here's the 1000-iteration code, which keeps 2574 digits of precision to ensure that it gets the answer right to 2570 (and then only prints it out to 2570):
 
<langsyntaxhighlight lang=dc>1000 sn [ n = number of iterations ]sx
2574k [ precision to use during computation ]sx
1 d se sf [ set e and f to 1 ]sx
Line 840:
2570k [ now reset precision to match correct digits ]sx
le 1 / [ get result truncated to that precision ]sx
n 10P [ and print it out, again with n + 10P ]sx</langsyntaxhighlight>
 
{{Out}}
Line 889:
 
{{Trans|C}}
<langsyntaxhighlight lang=Delphi>
program Calculating_the_value_of_e;
 
Line 931:
end.
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 942:
{{trans|Swift}}
 
<langsyntaxhighlight lang=dyalect>func calculateE(epsilon = 1.0e-15) {
func abs(n) {
if n < 0 {
Line 970:
}
print(calculateE())</langsyntaxhighlight>
 
{{out}}
Line 977:
 
=={{header|EasyLang}}==
<syntaxhighlight lang=text>numfmt 0 5
fact = 1
n = 2
Line 987:
e += 1 / fact
.
print e</langsyntaxhighlight>
 
=={{header|EDSAC order code}}==
Line 997:
by calculating e - 2 and printing the result with '2' in front.
It will be seen that the answer is 3 out in the 10th decimal place.
<langsyntaxhighlight lang=edsac>
[Calculate e]
[EDSAC program, Initial Orders 2]
Line 1,082:
E14Z [relative address of entry]
PF [enter with accumulator = 0]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,101:
 
The program executes about 38 million orders (16 hours on the original EDSAC) and prints e to 1324 decimal places. Because of rounding errors, the last two places are wrong (should be 61, not 44).
<langsyntaxhighlight lang=edsac>
[Calculate e by multilength variables.
EDSAC program, Initial Orders 2.
Line 1,392:
E 184 Z [define entry point]
P F [acc = 0 on entry]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,427:
===Spigot algorithm===
The EDSAC program to calculate pi by means of a spigot algorithm is easily modified to calculate e. It will then output 2070 correct digits of e (though it outputs only 252 correct digits of pi). Details will be found under the task "Pi".
<langsyntaxhighlight lang=edsac>
[Code not given, as it is almost the same as the EDSAC spigot algorithm for pi.
See the task "Pi" for the EDSAC program and the changes needed to calculate e.]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,457:
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight lang=lisp>(defun factorial (i)
"Compute factorial of i."
(apply #'* (number-sequence 1 i)))
Line 1,466:
(mapcar #'factorial (number-sequence 1 iter)))))
 
(compute-e 20)</langsyntaxhighlight>
{{output}}
<pre>
Line 1,472:
 
=={{header|Epoxy}}==
<langsyntaxhighlight lang=epoxy>fn CalculateE(P)
var E:1,F:1
loop I:1;I<=P;I+:1 do
Line 1,482:
 
log(CalculateE(100))
log(math.e)</langsyntaxhighlight>
{{out}}
<pre>2.7182818284590455
Line 1,495:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang=lisp>eApprox
=LAMBDA(n,
INDEX(
Line 1,518:
1
)
)</langsyntaxhighlight>
 
and also assuming the following generic bindings in the Name Manager for the WorkBook:
 
<langsyntaxhighlight lang=lisp>FOLDROW
=LAMBDA(op,
LAMBDA(a,
Line 1,568:
)
)
)</langsyntaxhighlight>
 
{{Out}}
Line 1,671:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang=fsharp>
// A function to generate the sequence 1/n!). Nigel Galloway: May 9th., 2018
let e = Seq.unfold(fun (n,g)->Some(n,(n/g,g+1N))) (1N,1N)
</syntaxhighlight>
</lang>
Which may be used:
<langsyntaxhighlight lang=fsharp>
printfn "%.14f" (float (e |> Seq.take 20 |> Seq.sum))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,686:
=={{header|Factor}}==
{{works with|Factor|0.98}}
<langsyntaxhighlight lang=factor>USING: math math.factorials prettyprint sequences ;
IN: rosetta-code.calculate-e
 
CONSTANT: terms 20
 
terms <iota> [ n! recip ] map-sum >float .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,699:
=={{header|FOCAL}}==
{{trans|ZX Spectrum Basic}}
<langsyntaxhighlight lang=focal>1.1 S K=1; S E=0
1.2 F X=1,10; D 2
1.3 Q
Line 1,705:
2.1 S E=E+1/K
2.2 S K=K*X
2.3 T %2,X,%6.5,E,!</langsyntaxhighlight>
{{output}}
<pre>*G
Line 1,736:
* Output the next digit: The final quotient is the next digit of e.
 
<langsyntaxhighlight lang=forth>100 constant #digits
: int-array create cells allot does> swap cells + ;
 
Line 1,756:
0 .r
LOOP ;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,766:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang=fortran >
Program eee
implicit none
Line 1,782:
write(*,*) ' polynomial ', ee
end Program eee</langsyntaxhighlight>
{{out}}
<pre>
Line 1,794:
=={{header|FreeBASIC}}==
===Normal basic===
<langsyntaxhighlight lang=freebasic>' version 02-07-2018
' compile with: fbc -s console
 
Line 1,815:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>The value of e = 2.718281828459046</pre>
===GMP version===
{{libheader|GMP}}
<langsyntaxhighlight lang=freebasic>' version 02-07-2018
' compile with: fbc -s console
 
Line 1,865:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>The value of e = 2.71828182845904523536028747135266249775724709369996</pre>
Line 1,878:
 
=={{header|Furor}}==
<langsyntaxhighlight lang=Furor>
###sysinclude math.uh
1.0e-15 sto EPSILON
Line 1,896:
{ „e0” }
{ „n” }
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,907:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,930:
}
fmt.Printf("e = %.15f\n", e)
}</langsyntaxhighlight>
 
{{out}}
Line 1,942:
 
Since the difference between partial sums is always the "last" term, it suffices to ensure that the "last" term is less than the tolerance.
<langsyntaxhighlight lang=groovy>def ε = 1.0e-15
def φ = 1/ε
 
Line 1,958:
}
 
def e = generateAddends().sum()</langsyntaxhighlight>
'''Test: '''
<langsyntaxhighlight lang=groovy>printf "%17.15f\n%17.15f\n", e, Math.E</langsyntaxhighlight>
'''Output: '''
<pre>2.718281828459045
Line 1,968:
For the purposes of 64 bit floating point precision, the value seems to stabilise after summing c. 17-20 terms.
 
<langsyntaxhighlight lang=haskell>------ APPROXIMATION OF E OBTAINED AFTER N ITERATIONS ----
 
eApprox :: Int -> Double
Line 1,976:
--------------------------- TEST -------------------------
main :: IO ()
main = print $ eApprox 20</langsyntaxhighlight>
{{Out}}
<pre>2.7182818284590455</pre>
Line 1,982:
Or equivalently, in a single fold:
 
<langsyntaxhighlight lang=haskell>------ APPROXIMATION OF E OBTAINED AFTER N ITERATIONS ----
 
eApprox n =
Line 1,995:
--------------------------- TEST -------------------------
main :: IO ()
main = print $ eApprox 20</langsyntaxhighlight>
{{Out}}
<pre>2.7182818284590455</pre>
Line 2,001:
Or in terms of iterate:
 
<langsyntaxhighlight lang=haskell>{-# LANGUAGE TupleSections #-}
 
------------------- APPROXIMATIONS TO E ------------------
Line 2,016:
--------------------------- TEST -------------------------
main :: IO ()
main = print $ approximatEs !! 17</langsyntaxhighlight>
{{Out}}
<pre>2.7182818284590455</pre>
Line 2,026:
For Icon, the EPSILON preprocessor constant would need to be an inline constant where tested, or set in a variable.
 
<langsyntaxhighlight lang=unicon>$define EPSILON 1.0e-15
 
procedure main()
Line 2,043:
write("computed e ", e)
write("keyword &e ", &e)
end</langsyntaxhighlight>
 
{{out}}
Line 2,051:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight lang=IS-BASIC>100 PROGRAM "e.bas"
110 LET E1=0:LET E,N,N1=1
120 DO WHILE E<>E1
Line 2,057:
140 LET N1=N1+1:LET N=N*N1
150 LOOP
160 PRINT "The value of e =";E</langsyntaxhighlight>
{{Out}}
<pre>The value of e = 2.71828183</pre>
Line 2,138:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=java>public class CalculateE {
public static final double EPSILON = 1.0e-15;
 
Line 2,153:
System.out.printf("e = %.15f\n", e);
}
}</langsyntaxhighlight>
{{out}}
<pre>e = 2.718281828459046</pre>
Line 2,159:
=={{header|Javascript}}==
Summing over a scan
<langsyntaxhighlight lang=javascript>(() => {
"use strict";
 
Line 2,213:
// MAIN ---
return main();
})();</langsyntaxhighlight>
<pre>2.7182818284590455</pre>
 
Or as a single fold/reduce:
<langsyntaxhighlight lang=javascript>(() => {
"use strict";
 
Line 2,248:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>2.7182818284590455</pre>
 
=={{header|jq}}==
<syntaxhighlight lang=text>1|exp #=> 2.718281828459045</langsyntaxhighlight>
<syntaxhighlight lang=text>def e:
[null,0,1,1]
| until(.[0] == .[1]; .[0]=.[1] | .[1]+=1/.[2] | .[2] *= .[3] | .[3]+=1)
| .[0];
e #=> 2.7182818284590455</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 2,264:
 
'''Module''':
<langsyntaxhighlight lang=julia>module NeperConstant
 
export NeperConst
Line 2,288:
end
 
end # module NeperConstant</langsyntaxhighlight>
 
'''Main''':
<langsyntaxhighlight lang=julia>for F in (Float16, Float32, Float64, BigFloat)
println(NeperConst{F}())
end</langsyntaxhighlight>
 
{{out}}
Line 2,302:
 
=={{header|K}}==
<syntaxhighlight lang=K>
<lang K>
/ Computing value of e
/ ecomp.k
Line 2,309:
evalue:{1 +/(1.0%)'fact' 1+!20}
evalue[]
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,318:
 
=={{header|Klingphix}}==
<syntaxhighlight lang=text>%e0 %e %n %fact %v
 
0 !e0 2 !e 0 !n 1 !fact
Line 2,341:
"Number of iterations = " $n printOp
 
" " input</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang=scala>// Version 1.2.40
 
import kotlin.math.abs
Line 2,361:
while (abs(e - e0) >= EPSILON)
println("e = %.15f".format(e))
}</langsyntaxhighlight>
 
{{output}}
Line 2,369:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang=scheme>
1) straightforward
 
Line 2,395:
{euler 1 17}
-> 2.7182818284590455
</syntaxhighlight>
</lang>
 
=={{header|langur}}==
{{trans|Go}}
<langsyntaxhighlight lang=langur>mode divMaxScale = 104
 
val .epsilon = 1.0e-104
Line 2,415:
 
# compare to built-in constant e
writeln " e = ", e</langsyntaxhighlight>
 
{{out}}
Line 2,425:
 
=={{header|Lua}}==
<langsyntaxhighlight lang=lua>EPSILON = 1.0e-15;
 
fact = 1
Line 2,439:
until (math.abs(e - e0) < EPSILON)
 
io.write(string.format("e = %.15f\n", e))</langsyntaxhighlight>
{{out}}
<pre>e = 2.718281828459046</pre>
Line 2,446:
Using @ for Decimal, and ~ for Float, # for Currency (Double is the default type for M2000)
 
<langsyntaxhighlight lang=M2000 Interpreter>
Module FindE {
Function comp_e (n){
Line 2,464:
}
FindE
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,475:
 
As a lambda function (also we use a faster For, using block {})
<langsyntaxhighlight lang=M2000 Interpreter>
comp_e=lambda (n)->{n/=28:For i=27to 1 {n=1+n/i}:=n}
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
 
<langsyntaxhighlight lang=maple>evalf[50](add(1/n!,n=0..100));
# 2.7182818284590452353602874713526624977572470937000
 
evalf[50](exp(1));
# 2.7182818284590452353602874713526624977572470937000</langsyntaxhighlight>
 
With [https://en.wikipedia.org/wiki/Continued_fraction continued fractions]:
 
<langsyntaxhighlight lang=maple>with(NumberTheory):
e:=ContinuedFraction(exp(1)):
Convergent(e,100);
Line 2,495:
 
# 13823891428306770374331665289458907890372191037173036666131/5085525453460186301777867529962655859538011626631066055111
# 2.7182818284590452353602874713526624977572470937000</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>1+Fold[1.+#1/#2&,1,Range[10,2,-1]]</langsyntaxhighlight>
{{output}}
<pre>
Line 2,504:
</pre>
 
<langsyntaxhighlight lang=Mathematica>Sum[1/x!, {x, 0, ∞}]</langsyntaxhighlight>
<langsyntaxhighlight lang=Mathematica>Limit[(1+1/x)^x,x->∞]</langsyntaxhighlight>
<syntaxhighlight lang =Mathematica>Exp[1]</langsyntaxhighlight>
or even just
<syntaxhighlight lang =Mathematica>𝕖</langsyntaxhighlight>
input as <syntaxhighlight lang =Mathematica>≡ee≡</langsyntaxhighlight>
{{output}}
<pre>𝕖</pre>
Line 2,515:
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang=min>(:n (n 0 ==) ((0)) (-1 () ((succ dup) dip append) n times) if) :iota
(iota 'succ '* map-reduce) :factorial
 
20 iota (factorial 1 swap /) '+ map-reduce print</langsyntaxhighlight>
{{out}}
<pre>
Line 2,525:
 
=={{header|МК-61/52}}==
<langsyntaxhighlight lang=mk-61>П0 П1 0 П2 1 П2 1 П3
ИП3 ИП2 ИП1 ИП0 - 1 + * П2 1/x + П3
ИП0 x#0 25 L0 08 ИП3 С/П</langsyntaxhighlight>
 
At n = 10, the value is 2.7182819.
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang=modula2>MODULE CalculateE;
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,568:
 
ReadChar
END CalculateE.</langsyntaxhighlight>
 
=={{header|Myrddin}}==
<langsyntaxhighlight lang=Myrrdin>use std
 
const main = {
Line 2,586:
;;
std.put("e: {}\n", e)
}</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
{{trans|Python}}
<langsyntaxhighlight lang=Nanoquery>e0 = 0
e = 2
n = 0
Line 2,602:
 
println "Computed e = " + e
println "Number of iterations = " + n</langsyntaxhighlight>
{{out}}
<pre>
Line 2,610:
 
=={{header|Nim}}==
<langsyntaxhighlight lang=nim>const epsilon : float64 = 1.0e-15
var fact : int64 = 1
var e : float64 = 2.0
Line 2,622:
e = e + 1.0 / fact.float64
 
echo e</langsyntaxhighlight>
 
=={{header|Pascal}}==
Like delphi and many other.Slightly modified to calculate (1/n!) not n! and then divide to (1/n!)
<langsyntaxhighlight lang=pascal>program Calculating_the_value_of_e;
{$IFDEF FPC}
{$MODE DELPHI}
Line 2,659:
{$IFDEF WINDOWS}readln;{$ENDIF}
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>calc e = 2.718281828459045 intern e= 2.718281828459045</pre>
Line 2,665:
=={{header|Perl}}==
With the <code>bignum</code> core module in force, Brother's algorithm requires only 18 iterations to match the precision of the built-in value, <code>e</code>.
<langsyntaxhighlight lang=perl>use bignum qw(e);
 
$e = 2;
Line 2,677:
 
print "Computed " . substr($e, 0, 41), "\n";
print "Built-in " . e, "\n";</langsyntaxhighlight>
{{out}}
<pre>Computed 2.718281828459045235360287471352662497757
Line 2,686:
Here, 71 terms of the Taylor series yield 𝑒 to 101 digits.
 
<langsyntaxhighlight lang=perl>use bigrat;
use Math::Decimal qw(dec_canonise dec_mul dec_rndiv_and_rem);
 
Line 2,711:
}
 
printf "\n%s\n", subset $e, 0,102;</langsyntaxhighlight>
{{out}}
<pre>numerator: 32561133701373476427912330475884581607687531065877567210421813247164172713574202714721554378508046501
Line 2,720:
=={{header|Phix}}==
{{trans|Python}}
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</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;">-- (builtin constant E renamed as EULER)</span>
Line 2,734:
<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;">" Error = %g\n"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">EULER</span><span style="color: #0000FF;">-</span><span style="color: #000000;">e</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;">"Number of iterations = %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,745:
=={{header|Phixmonti}}==
{{trans|Python}}
<langsyntaxhighlight lang=Phixmonti>0 var e0 2 var e 0 var n 1 var fact
1e-15 var v
 
Line 2,768:
"Real e = " rE tostr printOp
"Error = " rE e - printOp
"Number of iterations = " n printOp</langsyntaxhighlight>
{{out}}
<pre>
Line 2,778:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang=PicoLisp>(scl 15)
(let (F 1 E 2.0 E0 0 N 2)
(while (> E E0)
Line 2,784:
(inc 'E (*/ 1.0 F))
(inc 'N) )
(prinl "e = " (format E *Scl)) )</langsyntaxhighlight>
{{out}}
<pre>e = 2.718281828459046</pre>
Line 2,790:
=={{header|PowerShell}}==
{{trans|Python}}
<langsyntaxhighlight lang=powershell>$e0 = 0
$e = 2
$n = 0
Line 2,804:
Write-Host " Real e = $([Math]::Exp(1))"
Write-Host " Error = $([Math]::Exp(1) - $e)"
Write-Host "Number of iterations = $n"</langsyntaxhighlight>
{{out}}
<pre>Computed e = 2.71828182845904
Line 2,813:
=={{header|Processing}}==
Updated to show that computed value matches library value after 21 iterations.
<langsyntaxhighlight lang=java>
void setup() {
double e = 0;
Line 2,838:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,854:
===Floating-point solution===
Uses Newton's method to solve ln x = 1
<langsyntaxhighlight lang=prolog>
% Calculate the value e = exp 1
% Use Newton's method: x0 = 2; y = x(2 - ln x)
Line 2,876:
 
?- main.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,884:
 
===Arbitrary-precision solution===
<langsyntaxhighlight lang=prolog>
% John Devou: 26-Nov-2021
% Simple program to calculate e up to n decimal digits.
Line 2,906:
 
?- main.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,922:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang=PureBasic>Define f.d=1.0, e.d=1.0, e0.d=e, n.i=1
LOOP:
f*n : e+1.0/f
If e-e0>=1.0e-15 : e0=e : n+1 : Goto LOOP : EndIf
Debug "e="+StrD(e,15)</langsyntaxhighlight>
{{out}}
<pre>e=2.718281828459046</pre>
Line 2,932:
=={{header|Python}}==
===Imperative===
<langsyntaxhighlight lang=python>import math
#Implementation of Brother's formula
e0 = 0
Line 2,947:
print "Real e = "+str(math.e)
print "Error = "+str(math.e-e)
print "Number of iterations = "+str(n)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,960:
 
{{Works with|Python|3.7}}
<langsyntaxhighlight lang=python>'''Calculating an approximate value for e'''
 
from itertools import (accumulate, chain)
Line 3,007:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>2.7182818284590455</pre>
 
Or in terms of a single fold/reduce:
<langsyntaxhighlight lang=python>'''Approximation of E'''
 
from functools import reduce
Line 3,043:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>2.7182818284590455</pre>
 
=={{header|R}}==
<syntaxhighlight lang=R>
<lang R>
options(digits=22)
cat("e =",sum(rep(1,20)/factorial(0:19)))
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,062:
Using the Quackery bignum rational arithmetic library.
 
<langsyntaxhighlight lang=Quackery> $ "bigrat.qky" loadfile
 
[ swap number$
Line 3,086:
3 times drop temp release ] is approximate-e ( n n --> )
 
55 70 approximate-e</langsyntaxhighlight>
 
{{Out}}
Line 3,151:
 
=={{header|Racket}}==
<langsyntaxhighlight lang=racket>#lang racket
(require math/number-theory)
 
Line 3,161:
(displayln e)
(displayln (real->decimal-string e 20))
(displayln (real->decimal-string (- (exp 1) e) 20))))</langsyntaxhighlight>
{{out}}
<pre>82666416490601/30411275102208
Line 3,170:
(formerly Perl 6)
{{works with|Rakudo|2018.03}}
<syntaxhighlight lang=raku perl6line># If you need high precision: Sum of a Taylor series method.
# Adjust the terms parameter to suit. Theoretically the
# terms could be ∞. Practically, calculating an infinite
Line 3,183:
 
# Or, if you don't need high precision, it's a built-in.
say e;</langsyntaxhighlight>
{{out}}
<pre>2.718281828459045235360287471352662497757247093699959574966967627724076630353547
Line 3,217:
If the argument (digs) is negative, a running number of decimal digits of &nbsp; <big>''e''</big> &nbsp; is
shown.
<langsyntaxhighlight lang=rexx>/*REXX pgm calculates e to a # of decimal digits. If digs<0, a running value is shown.*/
parse arg digs . /*get optional number of decimal digits*/
if digs=='' | digs=="," then digs= 101 /*Not specified? Then use the default.*/
Line 3,231:
end /*#*/ /* -1 is for the decimal point────┘ */
say /*stick a fork in it, we're all done. */
say '(with' abs(digs) "decimal digits) the value of e is:"; say e</langsyntaxhighlight>
Programming note: &nbsp; the factorial of the &nbsp; '''do''' &nbsp; loop index is calculated by &nbsp; ''division'', &nbsp; not by the usual &nbsp; ''multiplication'' &nbsp; (for optimization).
 
Line 3,321:
===version 2===
Using the series shown in version 1 compute e to the specified precision.
<langsyntaxhighlight lang=rexx>/*REXX pgm calculates e to nn of decimal digits */
Parse Arg dig /* the desired precision */
Numeric Digits (dig+3) /* increase precision */
Line 3,336:
Numeric Digits dig /* the desired precision */
e=e/1 /* the desired approximation */
Return left(e,dig+1) '('n 'iterations required)'</langsyntaxhighlight>
{{out}}
<pre>J:\>rexx eval compey(66)
compey(66)=2.71828182845904523536028747135266249775724709369995957496696762772 (52 iterations required)</pre>
Check the function's correctness
<langsyntaxhighlight lang=rexx> /*REXX check the correctness of compey */
e_='2.7182818284590452353602874713526624977572470936999595749669676277240'||,
'766303535475945713821785251664274274663919320030599218174135966290435'||,
Line 3,356:
Else ok=ok+1
End
Say ok 'comparisons are ok' </langsyntaxhighlight>
{{out}}
<pre>J:\>rexx compez
Line 3,362:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
# Project : Calculating the value of e
 
Line 3,386:
return n * factorial(n-1)
ok
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,397:
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang=ruby>
fact = 1
e = 2
Line 3,411:
 
puts e
</syntaxhighlight>
</lang>
Built in:
<langsyntaxhighlight lang=ruby>require "bigdecimal/math"
 
puts BigMath.E(50).to_s # 50 decimals
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,423:
 
=={{header|Rust}}==
<langsyntaxhighlight lang=Rust>const EPSILON: f64 = 1e-15;
 
fn main() {
Line 3,439:
}
println!("e = {:.15}", e);
}</langsyntaxhighlight>
{{out}}
<pre>e = 2.718281828459046</pre>
Line 3,445:
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/gLmNcH2/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/WSvNG9xMT5GcugVTvqogVg Scastie (remote JVM)].
<langsyntaxhighlight lang=Scala>import scala.annotation.tailrec
 
object CalculateE extends App {
Line 3,459:
 
println(f"ℯ = ${iter(1L, 2.0, 2, 0)}%.15f")
}</langsyntaxhighlight>
=={{header|Scheme}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang=Scheme>
(import (rnrs))
 
Line 3,500:
 
(display (e))
(newline)</langsyntaxhighlight>
===High Precision===
{{works with|Chez Scheme}}
Line 3,508:
exactness of the calculation. So, calling exp with the integer 1 will return an exact rational
value, allowing for higher precision than floating point would.
<langsyntaxhighlight lang=scheme>; Use series to compute approximation to exp(z) (using N terms of series).
; n-1
; exp(z) ~ SUM ( z^k / k! )
Line 3,525:
(if (<= n 0)
1
(* n (fact (1- n))))))</langsyntaxhighlight>
'''Convert for Display'''
<langsyntaxhighlight lang=scheme>; Convert the given Rational number to a Decimal string.
; If opt contains an integer, show to that many places past the decimal regardless of repeating.
; If opt contains 'nopar, do not insert the parentheses indicating the repeating places.
Line 3,568:
(if (null? frc-list)
int-part
(format "~a.~a" int-part (list->string frc-list))))))</langsyntaxhighlight>
'''The Task'''
<langsyntaxhighlight lang=scheme>; Use the series approximation to exp(z) to compute e (= exp(1)) to 100 places.
 
(let*
Line 3,581:
(printf "the computed exact rational:~%~a~%" e)
(printf "converted to decimal (~a places):~%~a~%" p (rat->dec-str e p))
(printf "converted to an inexact (float):~%~a~%" (exact->inexact e)))</langsyntaxhighlight>
{{out}}
<pre>Computing exp(1) using 75 terms...
Line 3,596:
The program below computes e:
 
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
include "float.s7i";
 
Line 3,615:
until abs(e - e0) < EPSILON;
writeln("e = " <& e digits 15);
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,624:
=={{header|Sidef}}==
 
<langsyntaxhighlight lang=ruby>func calculate_e(n=50) {
sum(0..n, {|k| 1/k! })
}
 
say calculate_e()
say calculate_e(69).as_dec(100)</langsyntaxhighlight>
{{out}}
<pre>
Line 3,638:
For finding the number of required terms for calculating ''e'' to a given number of decimal places, using the formula '''Sum_{k=0..n} 1/k!''', we have:
 
<langsyntaxhighlight lang=ruby>func f(n) {
var t = n*log(10)
(n + 10).bsearch_le { |k|
Line 3,648:
var n = f(10**k)
say "Sum_{k=0..#{n}} 1/k! = e correct to #{10**k->commify} decimal places"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,664:
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang=sml>fun calcEToEps() =
let
val eps = 1.0e~15
Line 3,679:
in
calcToEps'(2.0, 1.0, 1.0, 2.0)
end;</langsyntaxhighlight>
 
{{out}}
Line 3,693:
{{trans|C}}
 
<langsyntaxhighlight lang=swift>import Foundation
 
 
Line 3,711:
}
 
print(String(format: "e = %.15f\n", arguments: [calculateE()]))</langsyntaxhighlight>
 
{{out}}
Line 3,719:
=={{header|Tcl}}==
=== By the power series of exp(x) ===
<langsyntaxhighlight lang=tcl>
set ε 1.0e-15
set fact 1
Line 3,732:
set e [expr $e + 1.0/$fact]
}
puts "e = $e"</langsyntaxhighlight>
{{Out}}
<pre>
Line 3,739:
=== By the continued fraction for e ===
This has the advantage, that arbitrary large precision can be achieved, should that become a demand. Also, full precision for the floating point value can be guaranteed.
<langsyntaxhighlight lang=tcl>
## We use (regular) continued fractions, step by step.
## The partial CF is coded (a n b m) for (a+nr) / (b+mr) for rest r.
Line 3,788:
 
calcE 17
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,797:
=={{header|TI-83 BASIC}}==
Guided by the Awk version.
<langsyntaxhighlight lang=ti83b>0->D
2->N
2->E
Line 3,809:
End
Disp E
</syntaxhighlight>
</lang>
{{Out}}
<pre>2.718281828</pre>
Line 3,815:
=={{header|VBScript}}==
{{Trans|Python}}
<langsyntaxhighlight lang=vb>e0 = 0 : e = 2 : n = 0 : fact = 1
While (e - e0) > 1E-15
e0 = e
Line 3,826:
WScript.Echo "Real e = " & Exp(1)
WScript.Echo "Error = " & (Exp(1) - e)
WScript.Echo "Number of iterations = " & n</langsyntaxhighlight>
{{Out}}
<pre>Computed e = 2.71828182845904
Line 3,834:
 
=={{header|Verilog}}==
<langsyntaxhighlight lang=Verilog>module main;
real n, n1;
real e1, e;
Line 3,853:
$finish ;
end
endmodule</langsyntaxhighlight>
{{out}}
<pre>The value of e = 2.71828</pre>
Line 3,860:
{{trans|C#}}
{{libheader|System.Numerics}}Automatically determines number of padding digits required for the arbitrary precision output. Can calculate a quarter million digits of '''''e''''' in under a half a minute.
<langsyntaxhighlight lang=vbnet>Imports System, System.Numerics, System.Math, System.Console
 
Module Program
Line 3,878:
WriteLine("partial: {0}...{1}", es.Substring(0, 46), es.Substring(es.Length - 45))
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre>2.71828182845905
Line 3,887:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang=vlang>import math
const epsilon = 1.0e-15
Line 3,904:
}
println("e = ${e:.15f}")
}</langsyntaxhighlight>
 
{{out}}
Line 3,913:
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight lang=ecmascript>var epsilon = 1e-15
var fact = 1
var e = 2
Line 3,924:
if ((e - e0).abs < epsilon) break
}
System.print("e = %(e)")</langsyntaxhighlight>
 
{{out}}
Line 3,932:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>real N, E, E0, F; \index, Euler numbers, factorial
[Format(1, 16); \show 16 places after decimal point
N:= 1.0; E:= 1.0; F:= 1.0;
Line 3,943:
RlOut(0, E); CrLf(0);
IntOut(0, fix(N)); Text(0, " iterations");
]</langsyntaxhighlight>
 
{{out}}
Line 3,953:
=={{header|Zig}}==
This uses the continued fraction method to generate the maximum ratio that can be computed using 64 bit math. The final ratio is correct to 35 decimal places.
<langsyntaxhighlight lang=Zig>
const std = @import("std");
const math = std.math;
Line 4,023:
}
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 4,071:
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang=zkl>const EPSILON=1.0e-15;
fact,e,n := 1, 2.0, 2;
do{
Line 4,078:
e+=1.0/fact;
}while((e - e0).abs() >= EPSILON);
println("e = %.15f".fmt(e));</langsyntaxhighlight>
{{out}}
<pre>
Line 4,086:
=={{header|ZX Spectrum Basic}}==
 
<langsyntaxhighlight lang=zxbasic>10 LET p=13: REM precision, or the number of terms in the Taylor expansion, from 0 to 33...
20 LET k=1: REM ...the Spectrum's maximum expressible precision is reached at p=13, while...
30 LET e=0: REM ...the factorial can't go any higher than 33
Line 4,094:
70 NEXT x
80 PRINT e
90 PRINT e-EXP 1: REM the Spectrum ROM uses Chebyshev polynomials to evaluate EXP x = e^x</langsyntaxhighlight>
 
{{Out}}