【C++】Calculate the exact number of e.

As we know,the Natural Constant,which usually called e,has two 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 we have a try,the following two python(Version 3.4) program shows you the cruel fact.:(

  Number The level of deviation
Number of Formula One 2.7182818284590455 \( {10}^{-16} \)
Number of Formula Two 2.7181459268249255 \( {10}^{-4} \)
The exact number of e 2.7182818284590452 ------

So obviously,the convergence rate of Formula One is 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 is how many accurate digits we can reach,not how big or small the \( n \) is.

Though the high accuration calculation is required, the program still have two variables : \( s \) and \( t \) ,which is usually used to store the sum and the factorial.When the \( n \) is large enough, the very digit of s can be stable, which means the same digit of t is \( 0 \), to be safer ,we should put another sereval \( 0 \) behind it, which won‘t cost much more time.

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

So here is the program:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#pragma warning(disable:4996)
#define ll long long
using namespace std;
const int maxn=100010;
ll *a,*b;
int n,m,x;
ll p;
void div(int t){  //high-accuration division
    ll s=0;
    for (int i=x;i<=n;i++){
        s=s*p+b[i];
        b[i]=s/t;
        s%=t;
    }
    while (b[x]==0) x++;
}
string op(ll x,bool fx=false){   //Translate 10^9 number into string
    string s="";

    for (int i=1;i<=m;i++){
        string s1="";
        s1=(char)(x%10+48);
        s1+=s;s=s1;
        x=x/10;
    }
    if (fx){
        int i=0;
        while (s.at(i)==‘0‘) i++;
        s=s.substr(i);
    }
    return s;
}
int main(){
    cin >> n;n++;
    m=9;int v=n;n=n/m+5;
    int nn=n;n=n+8;  // some more space should be spared
    p=1;for (int i=1;i<=m;i++) p*=10;
    a=new ll[n+1];b=new ll[n+1];
    for (int i=2;i<=n;i++) a[i]=0;a[1]=1;a[n+1]=1;
    for (int i=2;i<=n;i++) b[i]=0;b[1]=1;b[n+1]=1;  // initialize a and b ,which means s and t.
    int t=1;x=1;
    while (x<=nn){
        div(t++);ll k=0;
        for (int i=n;i>=1;i--){
            a[i]+=b[i]+k;
            k=a[i]/p;a[i]%=p;
        }
        int i=x-1;
        while (k!=0){
            a[i]+=k;k=a[i]/p;
            a[i]%=p;i--;   //high-accuraton plus
        }
    }
    string s1="";
    for (int i=1;i<=nn;i++) s1+= op(a[i],i==1);
    s1=s1.substr(0,v);
    string s2=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

时间: 2024-07-30 13:48:41

【C++】Calculate the exact number of e.的相关文章

【LeetCode】Excel Sheet Column Number

题意: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 思路: 其实就是个进制转换.水水就过.倒是 Python 的代码让我

【42】414. Third Maximum Number

414. Third Maximum Number Description Submission Solutions Add to List Total Accepted: 20624 Total Submissions: 76932 Difficulty: Easy Contributors: ZengRed, 1337c0d3r Given a non-empty array of integers, return the third maximum number in this array

【leetcode73】经典算法-Guess Number Higher or Lower

题目描述: 从1-n中,随便的拿出一个数字,你来猜测. 提示 提供一个guess(int num)的api,针对猜测的数字,返回三个数值.0,-1,1 0;猜中返回num -1:比猜测的数值小 1:比猜测的数值大 例如: n = 10, I pick 6. Return 6. 原文描述: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess whi

【转】去掉HTML5中number类型input字段的小箭头

第一种方案: 在chrome下: input::-webkit-outer-spin-button,input::-webkit-inner-spin-button{    -webkit-appearance: none !important;    margin: 0; } Firefox下: input[type="number"]{-moz-appearance:textfield;} 第二种方案: 将type="number"改为type="te

【lintcode】Count of Smaller Number before itself

http://www.lintcode.com/en/problem/count-of-smaller-number-before-itself/ 这道题目是动态添加线段树的元素,然后再查询. 数据集和描述不相符,坑 class Solution { public: /** * @param A: An integer array * @return: Count the number of element before this element 'ai' is * smaller than i

【bzoj4604】The kth maximum number

暴力 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> using namespace std; #define MAXN 50010 int a[MAXN]; int X[MAXN],Y[MAXN],W[MAXN]; int C,N,Q,L; int read() {

【HDOJ】1394 Minimum Inversion Number

逆序数的性质.1. 暴力解 1 #include <stdio.h> 2 3 #define MAXNUM 5005 4 5 int a[MAXNUM]; 6 7 int main() { 8 int n; 9 int i, j, sum, min; 10 11 while (scanf("%d", &n) != EOF) { 12 for (i=0; i<n; ++i) 13 scanf("%d", &a[i]); 14 sum

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,