📜 ⬆️ ⬇️

Most natural logarithm

Inspired by the post about calculating pi , I decided to calculate the number e in a similar way. Along the way, we got the function of the natural logarithm.

image

Actually

#include <iostream> #define I r= #define l ; #define o #define x if(1+(d*2)*(1/(__*2))<=k)d++; #define e p+=d;d=1; #define h _++; #define s __++; double ln(double k){double p=0,n,y,r,d,_=0,__=0; I 3.30 loooooo I 3.25 loooooo I 3.20 loooooo I 3.15 loooooo I 3.10 loooooo I 3.05 loooooo I 3.00 loooooo I 2.95 loooooo I 2.90 loooooo I 2.85 loooooo I 2.80 loooooo I 2.75 looooooo I 2.70 looooooo I 2.65 looooooo I 2.60 looooooo I 2.55 looooooo I 2.50 looooooo I 2.45 looooooo I 2.40 loooooooo I 2.35 loooooooo I 2.30 loooooooo I 2.25 loooooooo I 2.20 loooooooo I 2.15 loooooooo I 2.10 loooooooo I 2.05 loooooooo I 2.00 loooooooo I 1.95 looooooooo I 1.90 looooooooo I 1.85 looooooooo I 1.80 looooooooo I 1.75 looooooooo I 1.70 looooooooo I 1.65 looooooooo I 1.60 looooooooo I 1.55 loooooooooo I 1.50 loooooooooo I 1.45 loooooooooo I 1.40 loooooooooo I 1.35 looooooooooo I 1.30 looooooooooo I 1.25 looooooooooo I 1.20 loooooooooooo I 1.15 loooooooooooo I 1.10 loooooooooooo I 1.05 loosssssssssso I 1.00 loohhhhhhhhhho I 0.95 loohhhhhhhhhheo I 0.90 loohhhhhhhhhheo I 0.85 loohhhhhhhhhhexo I 0.80 loohhhhhhhhhhexxo I 0.75 loohhhhhhhhhhexxxo I 0.70 loohhhhhhhhhhexxxxo I 0.65 loohhhhhhhhhhexxxxxo I 0.60 loohhhhhhhhhhexxxxxxo I 0.55 loohhhhhhhhhhexxxxxxx xo I 0.50 loohhhhhhhhhhexxxxxxx xxxo I 0.45 loohhhhhhhhhhexxxxxxx xxxxxxxo I 0.40 loohhhhhhhhhhexxxxxxx xxxxxxxxxxo I 0.35 loohhhhhhhhhhexxxxxxx xxxxxxxxxxxxxxxo I 0.30 loohhhhhhhhhhexxxxxxx xxxxxxxxxxxxxxxxxxxxo I 0.25 loohhhhhhhhhhexxxxxxx xxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx I 0.20 loohhhhhhhhhhexxxxxxx xxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx I 0.15 loohhhhhhhhhhexxxxxxx xxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx I 0.10 loohhhhhhhhhhexxxxxxx xxxxxxxxxxxxxxxxxxxxx xxxxxxxxe return p/_; // 0 +1 +2 +3 +4 +5 } 

')
The principle is simple - the natural logarithm of a is the area under the graph 1 / x from one to a.

image

Accordingly, the more accurately the graph is drawn, the more accurate the calculations will be. A little bit about plotting. The symbols s denote the unit segment, h is the square of the unit area, e is the function f (x) = 1, x is the area under the graph 1 / x on the segment (1, + inf).
Having the function of the natural logarithm and knowing that ln (e) = 1 now it is not difficult to find e by enumeration.

 for(double i = 0; i <= 3; i += 0.01) if (ln(i) > 0.98) { std::cout << i << std::endl; break; } 


Some results:

ExpressionValueTrue value
ln (2)0.7210530.69315
ln (2.7)one0.99325
ln (3)1.094741.09861
ln (4)1.352631.38629
ln (5)1.542111.60943
e2.72.718281828


Link to the full code.

Source: https://habr.com/ru/post/209278/


All Articles