Calculating the value of e

From Rosetta Code
Calculating the value of e is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


Calculate the value of e. See details: Calculating the value of e



ALGOL 68[edit]

Translation of: Kotlin
BEGIN
# calculate an approximation to e #
LONG REAL epsilon = 1.0e-15;
LONG INT fact := 1;
LONG REAL e := 2;
LONG INT n := 2;
WHILE
LONG REAL e0 = e;
fact *:= n;
n +:= 1;
e +:= 1.0 / fact;
ABS ( e - e0 ) >= epsilon
DO SKIP OD;
print( ( "e = ", fixed( e, -17, 15 ), newline ) )
END
Output:
e = 2.718281828459045

AWK[edit]

 
# syntax: GAWK -f CALCULATING_THE_VALUE_OF_E.AWK
BEGIN {
epsilon = 1.0e-15
fact = 1
e = 2.0
n = 2
do {
e0 = e
fact *= n++
e += 1.0 / fact
} while (abs(e-e0) >= epsilon)
printf("e=%.15f\n",e)
exit(0)
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
 
Output:
e=2.718281828459046

C[edit]

Translation of: Kotlin
#include <stdio.h>
#include <math.h>
 
#define EPSILON 1.0e-15
 
int main() {
unsigned long long fact = 1;
double e = 2.0, e0;
int n = 2;
do {
e0 = e;
fact *= n++;
e += 1.0 / fact;
}
while (fabs(e - e0) >= EPSILON);
printf("e = %.15f\n", e);
return 0;
}
Output:
e = 2.718281828459046

F#[edit]

 
// 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)
 

Which may be used:

 
printfn "%.14f" (float (e |> Seq.take 20 |> Seq.sum))
 
Output:
2.71828182845905

Factor[edit]

USING: math math.factorials prettyprint sequences ;
IN: rosetta-code.calculate-e
 
CONSTANT: terms 20
 
terms iota [ n! recip ] map-sum >float .
Output:
2.718281828459045

Julia[edit]

Works with: Julia version 0.6

Module:

module NeperConstant
 
export NeperConst
 
struct NeperConst{T}
val::T
end
 
Base.show(io::IO, nc::NeperConst{T}) where T = print(io, "β„― (", T, ") = ", nc.val)
 
function NeperConst{T}() where T
local e::T = 2.0
local e2::T = 1.0
local den::(T ≑ BigFloat ? BigInt : Int128) = 1
local n::typeof(den) = 2
while e β‰  e2
e2 = e
den *= n
n += one(n)
e += 1.0 / den
end
return NeperConst{T}(e)
end
 
end # module NeperConstant

Main:

for F in (Float16, Float32, Float64, BigFloat)
println(NeperConst{F}())
end
Output:
(Float16) 2.717
(Float32) 2.718282
(Float64) 2.7182818284590455
(BigFloat) 2.718281828459045235360287471352662497757247093699959574966967627724076630353416

Kotlin[edit]

// Version 1.2.40
 
import kotlin.math.abs
 
const val EPSILON = 1.0e-15
 
fun main(args: Array<String>) {
var fact = 1L
var e = 2.0
var n = 2
do {
val e0 = e
fact *= n++
e += 1.0 / fact
}
while (abs(e - e0) >= EPSILON)
println("e = %.15f".format(e))
}
Output:
e = 2.718281828459046

Perl 6[edit]

Works with: Rakudo version 2018.03
# 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
# series takes an awfully long time so limit to 500.
 
sub postfix:<!> (Int $n) { (constant f = 1, |[\*] 1..*)[$n] }
sub 𝑒 (Int $terms) { sum map { FatRat.new(1,.!) }, ^$terms }
 
say 𝑒(500).comb(80).join: "\n";
 
say '';
 
# Or, if you don't need high precision, it's a built-in.
say e;
Output:
2.718281828459045235360287471352662497757247093699959574966967627724076630353547
59457138217852516642742746639193200305992181741359662904357290033429526059563073
81323286279434907632338298807531952510190115738341879307021540891499348841675092
44761460668082264800168477411853742345442437107539077744992069551702761838606261
33138458300075204493382656029760673711320070932870912744374704723069697720931014
16928368190255151086574637721112523897844250569536967707854499699679468644549059
87931636889230098793127736178215424999229576351482208269895193668033182528869398
49646510582093923982948879332036250944311730123819706841614039701983767932068328
23764648042953118023287825098194558153017567173613320698112509961818815930416903
51598888519345807273866738589422879228499892086805825749279610484198444363463244
96848756023362482704197862320900216099023530436994184914631409343173814364054625
31520961836908887070167683964243781405927145635490613031072085103837505101157477
04171898610687396965521267154688957035035402123407849819334321068170121005627880
23519303322474501585390473041995777709350366041699732972508868769664035557071622
684471625608

2.71828182845905

Racket[edit]

#lang racket
(require math/number-theory)
 
(define (calculate-e (terms 20))
(apply + (map (compose / factorial) (range terms))))
 
(module+ main
(let ((e (calculate-e)))
(displayln e)
(displayln (real->decimal-string e 20))
(displayln (real->decimal-string (- (exp 1) e) 20))))
Output:
82666416490601/30411275102208
2.71828182845904523493
0.00000000000000000000

REXX[edit]

This REXX version uses the following formula to calculate Napier's constant   e:

╔═══════════════════════════════════════════════════════════════════════════════════════╗
β•‘                                                                                       β•‘
β•‘           1         1         1         1         1         1         1               β•‘
β•‘   e  =   ───   +   ───   +   ───   +   ───   +   ───   +   ───   +   ───   +    βˆ™βˆ™βˆ™   β•‘
β•‘           0!        1!        2!        3!        4!        5!        6!              β•‘
β•‘                                                                                       β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

If the argument is negative, a running number of decimal digits of   e   is shown.

/*REXX pgm calculates  e  to any number of dec. digits.  If neg, 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.*/
numeric digits abs(digs) /*use the absolute value of digs. */
tell= (digs<0) /* <0? Then show on-going calculations*/
w=length( abs(digs) ) /*W: used for aligning the output. */
@nap= "digits found for e (Napier's constant)" /*literal used for a SAY instruction.*/
e=1 /*define the value of e's 1st term. */
r=1 /* " " " " " " divisor*/
do #=1 until e==old /*start calculations at the second term*/
old=e /*save current value of e for COMPARE*/
r=r / # /*calculate the divisor for this term. */
e=e + r /*add quotient to running e value. */
if tell then do /*if digits is negative, display info. */
$=compare(e, old) /*determine number of e digs computed*/
if #>3 & $>0 then say "with" right(#+1,w) 'terms,' right($-1,w) @nap
end /* ↑ */
end /*#*/ /*-1 is for the decimal pointβ”€β”€β”˜ */
 
say '(with' abs(digs) "decimal digits) the value of e is:"
say e /*stick a fork in it, we're all done. */
output   when using the default input:
(with 101 decimal digits)   the value of   e   is:
2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274
output   when using the input of:   -101


(Shown at three-quarter size.)

with   5 terms,   2 digits found for  e  (Napier's constant)
with   6 terms,   3 digits found for  e  (Napier's constant)
with   7 terms,   4 digits found for  e  (Napier's constant)
with   8 terms,   5 digits found for  e  (Napier's constant)
with   9 terms,   6 digits found for  e  (Napier's constant)
with  10 terms,   6 digits found for  e  (Napier's constant)
with  11 terms,   8 digits found for  e  (Napier's constant)
with  12 terms,   9 digits found for  e  (Napier's constant)
with  13 terms,  10 digits found for  e  (Napier's constant)
with  14 terms,  11 digits found for  e  (Napier's constant)
with  15 terms,  12 digits found for  e  (Napier's constant)
with  16 terms,  14 digits found for  e  (Napier's constant)
with  17 terms,  13 digits found for  e  (Napier's constant)
with  18 terms,  16 digits found for  e  (Napier's constant)
with  19 terms,  17 digits found for  e  (Napier's constant)
with  20 terms,  18 digits found for  e  (Napier's constant)
with  21 terms,  19 digits found for  e  (Napier's constant)
with  22 terms,  21 digits found for  e  (Napier's constant)
with  23 terms,  21 digits found for  e  (Napier's constant)
with  24 terms,  24 digits found for  e  (Napier's constant)
with  25 terms,  25 digits found for  e  (Napier's constant)
with  26 terms,  27 digits found for  e  (Napier's constant)
with  27 terms,  27 digits found for  e  (Napier's constant)
with  28 terms,  29 digits found for  e  (Napier's constant)
with  29 terms,  30 digits found for  e  (Napier's constant)
with  30 terms,  32 digits found for  e  (Napier's constant)
with  31 terms,  33 digits found for  e  (Napier's constant)
with  32 terms,  35 digits found for  e  (Napier's constant)
with  33 terms,  37 digits found for  e  (Napier's constant)
with  34 terms,  38 digits found for  e  (Napier's constant)
with  35 terms,  40 digits found for  e  (Napier's constant)
with  36 terms,  41 digits found for  e  (Napier's constant)
with  37 terms,  43 digits found for  e  (Napier's constant)
with  38 terms,  45 digits found for  e  (Napier's constant)
with  39 terms,  46 digits found for  e  (Napier's constant)
with  40 terms,  48 digits found for  e  (Napier's constant)
with  41 terms,  49 digits found for  e  (Napier's constant)
with  42 terms,  51 digits found for  e  (Napier's constant)
with  43 terms,  52 digits found for  e  (Napier's constant)
with  44 terms,  54 digits found for  e  (Napier's constant)
with  45 terms,  56 digits found for  e  (Napier's constant)
with  46 terms,  57 digits found for  e  (Napier's constant)
with  47 terms,  59 digits found for  e  (Napier's constant)
with  48 terms,  61 digits found for  e  (Napier's constant)
with  49 terms,  62 digits found for  e  (Napier's constant)
with  50 terms,  64 digits found for  e  (Napier's constant)
with  51 terms,  65 digits found for  e  (Napier's constant)
with  52 terms,  67 digits found for  e  (Napier's constant)
with  53 terms,  69 digits found for  e  (Napier's constant)
with  54 terms,  71 digits found for  e  (Napier's constant)
with  55 terms,  72 digits found for  e  (Napier's constant)
with  56 terms,  74 digits found for  e  (Napier's constant)
with  57 terms,  76 digits found for  e  (Napier's constant)
with  58 terms,  78 digits found for  e  (Napier's constant)
with  59 terms,  80 digits found for  e  (Napier's constant)
with  60 terms,  81 digits found for  e  (Napier's constant)
with  61 terms,  83 digits found for  e  (Napier's constant)
with  62 terms,  84 digits found for  e  (Napier's constant)
with  63 terms,  87 digits found for  e  (Napier's constant)
with  64 terms,  88 digits found for  e  (Napier's constant)
with  65 terms,  91 digits found for  e  (Napier's constant)
with  66 terms,  92 digits found for  e  (Napier's constant)
with  67 terms,  94 digits found for  e  (Napier's constant)
with  68 terms,  96 digits found for  e  (Napier's constant)
with  69 terms,  98 digits found for  e  (Napier's constant)
with  70 terms, 100 digits found for  e  (Napier's constant)
with  71 terms, 101 digits found for  e  (Napier's constant)
(with 101 decimal digits)   the value of   e   is:
2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274

Ring[edit]

 
# Project : Calculating the value of e
# Date  : 2018/04/21
# Author : Gal Zsolt [~ CalmoSoft ~]
# Email  : <[email protected]>
 
decimals(14)
 
for n = 1 to 100000
e = pow((1 + 1/n),n)
next
see "Calculating the value of e with method #1:" + nl
see "e = " + e + nl
 
e = 0
for n = 0 to 12
e = e + (1 / factorial(n))
next
see "Calculating the value of e with method #2:" + nl
see "e = " + e + nl
 
func factorial(n)
if n = 0 or n = 1
return 1
else
return n * factorial(n-1)
ok
 

Output:

Calculating the value of e with method #1:
e = 2.71826823719230
Calculating the value of e with method #2:
e = 2.71828182828617

Sidef[edit]

func calculate_e(prec=50) {
sum(^prec, {|n| 1/n! })
}
 
say calculate_e()
say calculate_e(100).as_dec(100)
Output:
2.7182818284590452353602874713526624977572470937
2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427

zkl[edit]

Translation of: C
const EPSILON=1.0e-15;
fact,e,n := 1, 2.0, 2;
do{
e0:=e;
fact*=n; n+=1;
e+=1.0/fact;
}while((e - e0).abs() >= EPSILON);
println("e = %.15f".fmt(e));
Output:
e = 2.718281828459046