Q: 4 (b) Write an application that estimates the
value of the mathematical constant e
by using the formula
public class euler {
public static void main(String args[])
{
double e = 1;
for(int x = 1; x < 18; x++)
e = e + 1/(double)fictorial(x);
System.out.println("e: "+ e);
}
public static double fictorial(int n)
{
int x;
if(n < 1)
{
return 0;
}
else
{
if(n == 1)
{
return 1;
}
else if(n > 1)
{
return n*fictorial(n-1);
}
}
return 0;
}
}
e = 1+ (1/1!) + (1/2!) + (1/3!) . . .
//////////////////////////////////////////////////////////////////////////////////////////////
public class euler {
public static void main(String args[])
{
double e = 1;
for(int x = 1; x < 18; x++)
e = e + 1/(double)fictorial(x);
System.out.println("e: "+ e);
}
public static double fictorial(int n)
{
int x;
if(n < 1)
{
return 0;
}
else
{
if(n == 1)
{
return 1;
}
else if(n > 1)
{
return n*fictorial(n-1);
}
}
return 0;
}
}
Post a Comment