Compute the first N digits of e

13

Challenge

Write a program to compute the the first N (<= 10^3) digits of e.

Your program should take an integer N as input.

Input:

100

Output:

2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427

Input:

7

Output:

2.718282

Input:

1000

Output:

2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035

Shortest solution wins!

Quixotic

Posted 2011-03-13T00:55:56.937

Reputation: 2 199

Answers

8

Mathematica (12 bytes)

N[E,Input[]]

Quixotic

Posted 2011-03-13T00:55:56.937

Reputation: 2 199

You can try it online: there is a e-digits command on yubnub.org e.g., http://yubnub.org/parser/parse?command=e-digits+1000 that uses wolframalpha http://www.wolframalpha.com/input/?i=N%5BE%2C7%5D&asynchronous=false&equal=Submit

– jfs – 2011-03-13T10:09:12.360

1-1 because you do not implement the calculation by your self. – FUZxxl – 2011-03-14T14:13:00.070

2@FUZxxl:The author didn't asked for an implementation,only shortest ones is preferred. – Quixotic – 2011-03-22T00:34:05.927

7@Quixotic The question does say "compute" not just output. Would you have argued the same thing if the question had said "calculate" instead of "compute"? – nitro2k01 – 2014-02-02T20:44:00.790

7

Python, 69

e=f=n=1;N=input()+2;exec"e+=10**N/f;f*=n;n+=1;"*N;print'2.'+`e`[1:-4]

Computes N+2 iterations of the standard power series for e.

hallvabo

Posted 2011-03-13T00:55:56.937

Reputation: 1 640

6

J, 20...ish.

Computational, but... Very inefficient. With n defined (20):

(0 j.<:n)":+/%!i.x:n+9

As a monadic function (32):

ge =. 3 : '(0 j.<:y)":+/%!i.x:y+9'

As a tacit function (33, fixed):

(0 j.<:)":(+/)&:(%&!&i.&x:&(9&+))

Jesse Millikan

Posted 2011-03-13T00:55:56.937

Reputation: 1 438

This converges very fast (for 100 digits you need "just" 70 terms) so you can drop that +9. – Eelvex – 2011-03-13T10:05:26.343

For 1000 digits this: (0 j.<:1000)":+/%!i.x:450 takes just a few seconds. – Eelvex – 2011-03-13T10:09:28.520

Would you care to explain your code? – aaaaaaaaaaaa – 2011-03-13T14:49:06.490

@Eelvex: But I need extra terms up until N=30 or so. (Any hints on reducing that mess of composes, or is that about right?) – Jesse Millikan – 2011-03-13T18:04:48.323

Ah, you're right, that's unfortunate. (That "mess" is shorter than anything else I could come up with - unless, off course, you use a fixed number for i. like: (0 j.<:n)":+/%!i.999x; then tacitly:(+/%!i.999x)":~0 j.<: ) – Eelvex – 2011-03-13T18:20:26.183

@Eelvex: This brings up a point... This performs horribly for larger values of n because of the resulting denominator, which is (for example) */!i.999x if it's not reduced. I wasn't even patient enough to let +/%!i.999x finish. Hints here, possibly...

– Jesse Millikan – 2011-03-13T19:46:20.980

@Jesse: Use 450x then. It's enough for every n<1000 and takes just a few seconds (~9s here). – Eelvex – 2011-03-13T20:33:48.597

5

Python, 67

import decimal as d
d.getcontext().prec=input()
print d._One.exp()

jfs

Posted 2011-03-13T00:55:56.937

Reputation: 209

@Timtech import * won't import _One due to the leading underscore. – jfs – 2016-04-24T10:24:44.827

Okay, didn't know that, sorry/ – Timtech – 2016-04-24T13:44:45.337

Save 3 chars? from decimal import* and remove both d. – Timtech – 2014-02-02T22:16:02.280

3

05AB1E, 4 3 2 bytes

Crossed out 4 is still regular 4 ;(

Thanks to @Adnan for a byte.

žt

Uses CP-1252 encoding.

Explanation:

žt - Push input. Pop a, push e to a places (up to 10000).

Update:

Remove I, as žt takes input anyway if none is on the stack.

George Gibson

Posted 2011-03-13T00:55:56.937

Reputation: 2 369

1I've marked this as non-competing because 05AB1E was created about 4 and a half years after this challenge was posted. – Mego – 2016-05-02T07:41:14.580

@Mego OK, I'll remember to do that next time. – George Gibson – 2016-05-02T07:41:56.080

1Welcome to Programming Puzzles and Code Golf! The ž character is 1 byte in the CP-1252 encoding, so this has a total score of 2 bytes instead of 3 :p. – Adnan – 2016-05-02T17:46:28.553

@Adnan Thanks! Nice language, btw. – George Gibson – 2016-05-02T17:47:08.280

@GeorgeGibson Thank you! :) – Adnan – 2016-05-02T17:47:42.903

2

Ruby, 68

require 'bigdecimal/math';include BigMath;puts E(gets.to_i).to_s 'F'

david4dev

Posted 2011-03-13T00:55:56.937

Reputation: 665

it doesn't work on ruby1.8. It produces invalid results on ruby1.9 – jfs – 2011-03-13T09:57:35.947

You're right, but it should work in Ruby 1.9. There seems to be something wrong with precision in the E function.

– david4dev – 2011-03-13T10:48:59.297

Looks fine here, it just adds some imprecise digits. Mayby like this? require'bigdecimal/math';puts BigMath::E(a=gets.to_i).to_s(?F)[0,a+1] – steenslag – 2011-03-13T22:48:33.297

1

GolfScript 43 41

~10\?1:b 461,{)b*:b@+\}460*;*b/`);(;'2.'\

Edit: I might as well replace the 0 with the leftover 1 from initializing b, the resulting difference is far too small to make it to the output.

I left the old version as that is what I have documented below.

~10\?1:b;0 461,{)b*:b@+\}460*;*b/`);(;'2.'\

~10\? Take input and calculate 10^input, leave the result on the stack.
1:b; Store 1 in b.
0 461, Put 0 on the stack, put the array [0 1 ... 459 460] on the stack.
{ }460* Execute function 460 times.
)b*:b Take the last element of the array, multiply it by b, store result in b and leave the result on the stack.
@+\ Switch the 0 (which is only a zero at the first iteration) to the top of the stack, add it to the leftover b value, and switch the result back again.
; Remove the rest of the array (only [0] is left).
The number that was initialized to 0 now hold the value e*460! and b hold 460!
* Multiply 10^input by e*460! (they are at this point the only 2 elements left on the stack).
b/ Divide the result by b.
The stack now hold the value e*10^input which when converted to a string will hold all the decimals, but not the dot.
`);(;'2.'\ A bunch of string operations to fit in the dot.

e*460! is calculated as 1 + 460 + 460*459 + 460*459*458 etc.

aaaaaaaaaaaa

Posted 2011-03-13T00:55:56.937

Reputation: 4 365

1

bc, 17 chars

scale=read()
e(1)

Tomas

Posted 2011-03-13T00:55:56.937

Reputation: 2 333

0

Mathematica, 7 bytes

N[E,#]&

My answer is a further golfing of this answer.

Try it online!

For some reason, the output over at TIO looks funny but I tested it on my machine and the code works just fine.

RGS

Posted 2011-03-13T00:55:56.937

Reputation: 5 047

0

Perl 5 -Mbignum=bexp -p, 12 bytes

$_=bexp 1,$_

Try it online!

Xcali

Posted 2011-03-13T00:55:56.937

Reputation: 7 671

0

J, 17

(":&(x:^1)@*&0j1)

Example:

(":&(x:^1)@*&0j1) 50
2.71828182845891281655718620537435347047040502245993

Uses built in exponential verb - so, "compute" is on shaky grounds. Basically:

^1 - computes e**1
x: - does extended precision
0jy ": - formats the number to y digit

barbermot

Posted 2011-03-13T00:55:56.937

Reputation: 111

*&0j1 is simply j. – FrownyFrog – 2017-10-27T00:29:10.440

0

GTB, 14

eS?`AS;_,1,A+1

Explanation

e - Put e as the last calculated value

S? - Convert e to string _

`A - Input A

S;_,1,A+1 Display the first A digits of e

Timtech

Posted 2011-03-13T00:55:56.937

Reputation: 12 038

0

Mathcad, [tbd] bytes

enter image description here

Uses a spigot algorithm - http://comjnl.oxfordjournals.org/content/11/2/229.full.pdf+html.


Byte count not given as Mathcad byte count equivalence is yet to be determined. However, using a symbol equivalence it is roughly 121 bytes ... and not going to win any brevity prizes no matter how the equivalence is determined.

Stuart Bruff

Posted 2011-03-13T00:55:56.937

Reputation: 501