[Edu-sig] <FUN>more math with Python</FUN>
Kirby Urner
urnerk@qwest.net
Sun, 25 Nov 2001 23:10:47 -0800
Here's somethin' I wrote to compute e. Uses 2.2's ability
to go with long integers on the fly, no explicit conversion
needed:
def gete(d):
"Return the value of e with d digits"
i = n = 1
# approx number of terms needed
terms = int(math.ceil(d * 4/math.log10(d)))
e = val = 10**terms
while i < terms:
n *= i
e += val/n
i += 1
val = str(e)
return val[0]+"."+val[1:d]
Usage:
>>> numb.gete(600)
'2.718281828459045235360287471352662497757247093699959574966967627724
076630353547594571382178525166427427466391932003059921817413596629043
572900334295260595630738132328627943490763233829880753195251019011573
834187930702154089149934884167509244761460668082264800168477411853742
345442437107539077744992069551702761838606261331384583000752044933826
560297606737113200709328709127443747047230696977209310141692836819025
515108657463772111252389784425056953696770785449969967946864454905987
931636889230098793127736178215424999229576351482208269895193668033182
52886939849646510582093923982948879332036250944311'
I checked it against a web page going for thousands more digits. So
far so good. :-D
The approximation re terms needed, given request for d digits, could
probably use some fine tuning no doubt.
The approach uses the fact that e = 1/0! + 1/1! + 1/2! + 1/3!...
but with every term multiplied by 100000000000000000000000000000000...
or whatever so that computations all happen in positive long integer
world.
The final step is simply to take the digits in string form, and stick
in a decimal point after the 2.
Kirby