g | x | w | all
Bytes Lang Time Link
029GolfScript240106T231641Znoodle p
100Python240106T174002Znoodle p
054Python 3240107T034823Zdingledo
058Python 3.8240106T174804Zm90
01305AB1E240111T094618ZKevin Cr
018SageMath240109T161650Z12431234
023Charcoal240106T180641ZNeil
090R240108T172257Zqwr
512Python 2240107T194635ZAlbert.L
076APLNARS240106T214937ZRosario
110Retina240109T131135ZNeil
089Scala 3240109T061311Z138 Aspe
017Wolfram Language Mathematica240107T015552ZGreg Mar
026WolframAlpha240107T010301Zvengy
109C240108T182201Zqwr
045JavaScript ES11240106T213602ZArnauld
011Jelly240107T141831ZNick Ken
2040APL NARS2000240106T222806ZAdá
025Wolfram Language Mathematica240106T175640ZZaMoC
011Vyxal240106T173417Zemanresu

GolfScript, 29 bytes

2 4000:a?:b){.*b/}a*5a?*+500<

Try it online!

Ports Albert.Lang's wonderful Python 2 solution to GolfScript. Check that answer for a better understanding of the math.

Code explanation:

2 4000:a?:b)
2 4000        # Push 2, 4000 to the stack.
      :a      # Store 4000 in variable a.
        ?     # Power; 2 ^ 4000
         :b   # Store 2 ^ 4000 in variable b.
           )  # Add one.

{.*b/}a*5a?*
{             # Open a block:
 .*           #   Duplicate and multiply (squaring
              #   the top of the stack.)
   b/         #   Integer divide by b.
     }a*      # Run this block a times.
        5a?   # Push 5 ^ a.
           *  # Multiply.

+500<
+      # Append this result to the implicit input,
       #   which is the empty string, therefore
       #   coercing the number to a string.
 500<  # Leave just the first 500 characters of
       #   this string.

GolfScript, 30 bytes

"2."128 10 499?{2+/.@+\}251,/;

Try it online!

Based on m90's Python solution, since GolfScript has big integers but no floats.

Python, 100 bytes

from decimal import*
getcontext().prec=500
e=d=Decimal(1)
for x in range(2,254):e+=1/d;d*=x
print(e)

Attempt This Online!

-2 bytes thanks to RubenVerg

254 is the lowest number which gives the correct result, but this doesn't get slow until you start looping for thousands of iterations.

This uses the definition:

\$ \displaystyle e = \sum_{n=1} ^{\infty} \frac{1}{n!} = \frac{1}{1} + \frac{1}{1 \cdot 2} + \frac{1}{1 \cdot 2 \cdot 3} + \cdots \$

The only optimization used is to, rather than calculating the factorial of each n from 1 to 254, store the factorial of \$ n-1 \$ in the variable d, and in each iteration multiply d by the loop number x.

This uses Python's decimal library with getcontext() in order to calculate the number to 500 digits of precision.

Python 3, 54 bytes

n=x=z=499
while~-n:x=(x+10**z)//n;n-=1
print(f'2.{x}')

Try it online!

Python 3, 44 bytes

Outputs e without the ., courtesy of @xnor.

n=x=z=499
while n:x=x//n+10**z;n-=1
print(x)

Try it online!

Python 3.8, 58 bytes

x=10**499
print(x+sum((x:=x//i)for i in range(1,253))+128)

Try it online!


Original version, before it was clearly specified that the decimal point could be omitted:

Python 3.8, 63 bytes

x=10**499
print(f'2.{sum((x:=x//i)for i in range(2,253))+128}')

Try it online!

05AB1E, 13 bytes

₅L₄°šÅ»÷}O₄;£

Port of @NickKennedy's Jelly answer, which in turn is based on @m90's Python answer, so make sure to upvote those answers as well.

Outputs the first 500 digits as a single concatted integer.

Try it online.

Explanation:"

₅L             # Push a list in the range [1,255]
  ₄°š          # Prepend 10**1000
     Å» }      # Cumulative left-reduce, keeping intermediate results
       ÷       #  by doing integer-division
         O     # Sum all those results together
          ₄;   # Push 500 (1000 halved)
            £  # Keep only the first 500 digits
               # (after which this integer is output implicitly as result)

SageMath, 20 18 Byte

Sehr unkreativ:

n(i^(2/i/pi),1664)

Alte Antwort:

n((-1)^(-i/pi),1664)

Onlinetest

i^(2/i/pi) =e : e^(πi) = -1 => e^(πi/2) = i => e = i^(2/πi)

n(....) : Kontentiere zu Dezimal

1664 : Mit einer Genauigkeit von 1664 bit

Charcoal, 23 bytes

≔⁰θF⊖φ≔÷⁺Xχ⁴⁹⁹θ⁻φιθ2.Iθ

Try it online! Link is to verbose version of code. Explanation:

≔⁰θ

Start with a running total of 0.

F⊖φ≔÷⁺Xχ⁴⁹⁹θ⁻φιθ

For each integer from 1000 down to 2, add 10**499 to the running total, then divide by that integer. This is equivalent to summing the reciprocals of the factorials from 2 to 1000 but without any loss of precision.

2.Iθ

Output e to 500 digits.

20 bytes to output without the decimal point:

≔⁰θFφ≔⁺Xχ⁴⁹⁹÷θ⁻φιθIθ

Try it online! Link is to verbose version of code. Explanation: As above but counts down to 1 and also adds the 10**499 after dividing so that the result includes the 0! and 1! terms.

R, 104 90 bytes

Since R doesn't have arbitrary precision arithmetic, and other answers already have used the limit definition and Taylor series of e, I use a decimal spigot algorithm to spice things up. I don't usually write loop-based code in R, so there is plenty of room for improvement. (divmod would be useful).

-7 bytes thanks to pajonk's better looping, and another -7 because I remembered R's colon operator can go backwards.

C=rep(1,254)
d=2
for(i in 1:500){cat(d)
d=0
for(j in 254:2)C[j]=(t=10*C[j]+d)-(d=t%/%j)*j}

Attempt This Online!

Based on The Calculation of e to Many Significant Digits by AHJ Sale (1968). The basic idea is to use an \$m\$th order Taylor polynomial, extract an integer part and fractional part, multiply the fractional part by 10, and repeat on the fractional part. Here's an example extracting the first decimal digit using 4 terms: (the 0 coefficients are coincidence)

\begin{alignat}{4} \newcommand\pp{\phantom{0}} e &\approx 2 + {} && \biggl[ &\frac 1 2 \biggl(\pp1 + {} &&\frac 1 3 \biggl(\pp1 + {} &\frac 1 4 (\pp1) \biggr) \biggr) \biggr]\\ &= 2 + \frac{1}{10} && \biggl[ &\frac 1 2 \biggl( 10 + {} &&\frac 1 3 \biggl( 10 + {} &\frac 1 4 (10) \biggr) \biggr) \biggr]\\ &= 2 + \frac{1}{10} && \biggl[ &\frac 1 2 \biggl( 10 + {} &&\frac 1 3 \biggl( 12 + {} &\frac 1 4 (\pp2) \biggr) \biggr) \biggr]\\ &= 2 + \frac{1}{10} && \biggl[ &\frac 1 2 \biggl( 14 + {} &&\frac 1 3 \biggl(\pp0 + {} &\frac 1 4 (\pp2) \biggr) \biggr) \biggr]\\ &= 2 + \frac{1}{10} && \biggl[7 + {} &\frac 1 2 \biggl(\pp0 + {} &&\frac 1 3 \biggl(\pp0 + {} &\frac 1 4 (\pp2) \biggr) \biggr) \biggr] \end{alignat}

Thanks to Steven B. Segletes for helping with MathJax formatting.

I believe the integers involved never exceed \$10m\$. The fractional part is always less than 1, so the computed digit can't affect the previous digit. \$e\$ is special because the coefficients of the Maclaurin series of \$e^x\$ are all 1. Trig functions for \$\pi\$ would have larger coefficients or produce negative digits, which would require amending previous digits. For example, here's Newton's arctan formula for \$\pi\$:

\begin{align} \frac \pi 2 &\approx 1 + \frac 1 3 \biggl( 1 + \frac 2 5 \biggl( 1 + \frac 3 7 (1) \biggr) \biggr) \\ &= 1 + \frac 1 3 \biggl( 1! + \frac 1 5 \biggl( 2! + \frac 1 7 (3!) \biggr) \biggr) \end{align}

There is no clean way to transform \$\frac 3 7 (10)\$ into an integer part while the fractional part is a multiple of \$\frac 3 7\$ and less than 1. The solution is modifying the spigot to maintain held predigits that are released once we are sure the digits are correct. See A Spigot Algorithm for the Digits of π by Rabinowitz and Wagon (1995).

\$m\$ can be calculated beforehand from the error of the finite sum using Stirling's approximation. For \$n+1\$ correct digits, find least \$m\$ satisfying \$m! > 10^{n+1}\$, equivalently $$\frac 1 2 \ln(2\pi m) + m(\ln m - 1) > (n+1) \ln 10$$

Ungolfed algorithm:

n = 500
m = 254
C = rep(1,m)
cat(2)
for(i in 2:n){
  d = 0
  for(j in m:2){
    t = 10*C[j] + d
    d = t %/% j
    C[j] = t - d*j
  }
  cat(d)
}

Python 2, 51 bytes (-2 thanks @xnor)

m=p=~2**3333
exec"m=m*m/~p;"*3333
print-m*10**499/p

Attempt This Online!

former Python 2, 53 bytes

p=4000
m=2**p+1
exec"m=m*m>>p;"*p
print`m*5**p`[:500]

Attempt This Online!

Outputs the digits as a single string, no decimal point.

How?

Approximately computes \$(1+1/n)^n\$ for \$n=2^{4000}\$ using repeated squaring.

APL(NARS), 76 chars

Q;r;d;k;v;⎕FPC
r←d←1⋄k←÷10x*501⋄v←0x
r+←d×←÷v+←1⋄→2×⍳k<∣d
⎕FPC←4×501⋄⎕←499⍕r

14+21+20+18+3=76

Calculus in rationals, and print with 499 digits after the point (499+1=500) the last digit rounded (but not changed because the 500th digit after the point should be 2<6)

2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320
  0305992181741359662904357290033429526059563073813232862794349076323382988075319525101901157383418793070215408
  9149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000
  7520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844
  250569536967707854499699679468644549059879316368892300987931

Retina, 110 bytes


252*
L$`_
$>`,_
^
2.¶
749{`$
;
(_*);(_*)
10*$1$2;
¶(_+),(\1)*(_*);
;$#2*_¶$1,$3
)`^(.*)(¶.*);(_*)
$1$.3$2
1G`

Try it online! Too slow to produce 500 digits on TIO so link just produces 108. Explanation: Port of @qwr's R answer.


252*
L$`_
$>`,_

Generate the working array. (Note: This is an estimate of its size, but increasing it just requires additionally increasing the iteration count of the loop below to compensate.)

^
2.¶

Start with 2..

749{`
)`

Loop 749 times, which produces 499 digits for some reason.

$
;

Start another parallel iteration.

(_*);(_*)
10*$1$2;

Multiply each value by 10 and add the carry from the next element's previous iteration.

¶(_+),(\1)*(_*);
;$#2*_¶$1,$3

Divmod each value by its index and propagate the carry to the previous element for its next iteration.

^(.*)(¶.*);(_*)
$1$.3$2

Generate the next digit from the first element.

1G`

Remove all of the elements leaving just the final decimal expansion.

Scala 3, 89 bytes

A port of @m90's Python answer in Scala.


Golfed version. Attempt This Online!

var x=BigInt(10) pow 499
println(s"2.${(BigInt(0)/:(2 to 252))((s,i) =>s+{x/=i;x})+128}")

Ungolfed version. Attempt This Online!

object Main {
  def main(args: Array[String]): Unit = {
    var x = BigInt(10).pow(499)
    val result = (for (i <- 2 to 252) yield {
      val div = x / i
      x = div
      div
    }).sum + 128
    println(s"2.$result")
  }
}

Wolfram Language (Mathematica), 17 bytes

Cosh@1+Sinh@1`500

Try it online! A direct port of emanresu A's nice answer.

WolframAlpha, 26 bytes

N[Sum[1/k!,{k,0,∞}],500]

Try it on WolframAlpha

C, 126 116 109 bytes

Port of my R answer.

-10 bytes by rearranging loop logic printing leading 2, inspired by pajonk, and some small optimizations.

-7 bytes by using C with zeros instead of ones, credit chux - Reinstate Monica. Previously, the initialization of array C to 1s is a GNU extension.

i=500,d=2,j,t,C[255];main(){while(i--){putchar(d+48);d=0;j=254;while(j-->2)t=10*C[j]+d+10,d=t/j,C[j]=t%j-1;}}

Attempt This Online!

JavaScript (ES11), 45 bytes

-5 thanks to @l4m2

A port of Neil's method, as suggested by Neil himself.

f=(q=i=999n)=>i?f((q+10n**499n)/-~i--):"2."+q

Try it online!

Jelly, 11 bytes

ȷ⁵*;R:\SDḣH

Try it online!

A Jelly version of m90’s Python answer. This is a niladic link which returns the first 500 digits of e as a list of decimal digits.

Explanation

ȷ           | 1000
 ⁵*         | 10 ** this
   ;R       | Concatenate to 1..1000
     :\     | Reduce using integer division, collecting intermediate results
       S    | Sum
        D   | Decimal digits
         ḣH | First 500 (since half of 1000 is 500)

APL (NARS2000), 20 chars = 40 bytes

499⍕+/5 6x○≢⎕FPC←1E4

⎕FPC←1E4 set Floating Point Control to use ten thousand bit floats

 count that (gives 1)

5 6x○ compute sinh and cosh of that (while indicating with x that we want extended precision)

+/ sum

499⍕ output with 499 decimals, i.e. 500 digits in total

Wolfram Language (Mathematica), 25 bytes

Sum[1/k!,{k,0,∞}]~N~500

Try it online!

Wolfram Language (Mathematica), 29 bytes

Limit[(1+1/n)^n,n->∞]~N~500

Try it online!

Vyxal, 11 bytes

1∆ȯ1∆ṡ+500Ḟ

Try it Online!

Calculates cosh(1)+sinh(1) to 500 decimal places.

1∆ȯ         # cosh(1)
      +     # + 
   1∆ṡ      # sinh(1)
       500Ḟ # to 500 decimal places