Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exponent of mathematical constant e
12-04-2009, 04:07 AM (This post was last modified: 12-04-2009 04:32 AM by drdebcol.)
Post: #1
Exponent of mathematical constant e
Base of natural algorithm "e" is a number that you can not write in p/q way so it is irrational. You need to make like an array of numbers with certain rule so you can get it with accuracy. "e" is around :
Code:
2.71828 18284 59045 23536 . . .
More about natural logarithm you have here :
http://en.wikipedia.org/wiki/Natural_logarithm
And about "e" you have here :
http://en.wikipedia.org/wiki/E_%28mathem...onstant%29
Now you can calculate exponent of "e" with this formula :
[Image: 22783_exponente.jpg]
And i made program for calculating this in Pascal. Input data are "n" and "x". "n" is number of loop circulations and "x" is exponent.
If you enter :
n=20
x=1
you should get around :
2.7182818
So here is the code in TPW for this :
Code:
program natural_logarithm_exponent;
uses wincrt;
var
i,n:longint;
e,x,p:real;
function power(a:real;b:longint):real;
var
  i:longint;
  p:real;
begin
  p:=1;
  for i:=1 to b do
    p:=p*a;
  power:=a;
end;
begin
readln(n);
readln(x);
p:=1;
e:=0;
for i:=1 to n do
begin
  e:=e+power(x,i)/p;
  p:=p*i;
end;
writeln(e:9:7);
end.
And now solution in C++. Basically you didn't have to make power function in Pascal. As you can see, C++ solution is much
more simple and evidently faster :
Code:
# include <iostream>
# include <cstdio>

using namespace std;

int main()
{
    int i,n;
    float e,x,p;
    cin>>n;
    cin>>x;
    p=1;
    e=0;
    for (i=1;i<=n;i++)
    {
        e+=(x/p);
        x=x*x;
        p*=i;
    }
    cout<<e<<endl;
    system("pause");
}

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: