"C + +" Calculate The exact number of E.

Source: Internet
Author: User

As we know,the Natural Constant,which usually called E,has both main ways of expressing:

1. \ (\lim_{n \to \infty}\sum_{i=0}^{n} (\frac{1}{i!}) \)

2. \ (\lim_{n \to \infty} (1+\frac{1}{n}) ^{n} \)

Let's have a try,the following-a-Python (Version 3.4) program shows you the cruel fact:(

2.7181459268249255
  number the level of deviation
number of Formula one 2.7182818284590455 \ ({10}^{-16} \)
number of Formula both \ ({10}^{-4} \
the exact number of e 2.7182818284590452 ------

So obviously,the convergence rate of Formula One are much more fast-speed. Also the calculate time it costs is not far more than Formule one.

So take action.:)

Another fact is,what most of us care most are how many accurate digits we can reach,not what big or small the \ (n \) is.

Though The high accuration calculation are required, the program still has both variables: \ (s \) and \ (t \), which is U Sually used to store the sum and the factorial. When the \ (n \) are large enough, the very digit of s can be stable, which means the same digit of T are \ (0 \), to be SAF Er, we should put another sereval \ (0 \) behind it, which won ' t cost much more time.

The One billion (\ ({10}^{9} \)) binary system should is used in order to minimize the time use.

So are the program:

#include <cstdio>#include<iostream>#include<algorithm>#include<string.h>#pragmaWarning (disable:4996)#definell Long Longusing namespacestd;Const intmaxn=100010; ll*a,*b;intN,m,x;ll p;voidDivintT) {//High-accuration Divisionll s=0;  for(inti=x;i<=n;i++) {s=s*p+B[i]; B[i]=s/T; S%=T; }     while(b[x]==0) x + +;}stringOP (ll x,BOOLfx=false){//Translate 10^9 number into string    strings="";  for(intI=1; i<=m;i++){        strings1=""; S1=(Char) (%Ten+ -); S1+=s;s=S1; X=x/Ten; }    if(FX) {intI=0;  while(s.at (i) = ='0') i++; S=s.substr (i); }    returns;}intMain () {CIN>> n;n++; M=9;intv=n;n=n/m+5; intnn=n;n=n+8;//Some more space should be sparedp=1; for(intI=1; i<=m;i++) p*=Ten; A=Newll[n+1];b=Newll[n+1];  for(intI=2; i<=n;i++) a[i]=0; a[1]=1; a[n+1]=1;  for(intI=2; i<=n;i++) b[i]=0; b[1]=1; b[n+1]=1;//Initialize A and B, which means s and T.    intt=1; x=1;  while(x<=nn) {div (t+ +); ll k=0;  for(inti=n;i>=1; i--) {A[i]+=b[i]+K; K=a[i]/p;a[i]%=p; }        inti=x-1;  while(k!=0) {A[i]+=k;k=a[i]/p; A[i]%=p;i--;//High-accuraton plus        }    }    strings1="";  for(intI=1; i<=nn;i++) s1+= op (a[i],i==1); S1=S1.SUBSTR (0, V); stringS2=S1.SUBSTR (0,1); S2+="."; S2+=S1.SUBSTR (1); printf ("%s\n", S2.c_str ()); return 0;}//This code can run properly on vs2012
Number of digit required Time Use (MS)
100 0
1000 31
5000 109
9500 265
10000 297
20000 873
50000 4446
100000 16365
1000000 About 20min

"C + +" Calculate The exact number of E.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.