Why would you need a recursive method any how? I think it would be better to write a simple for loop. I don't think your going to get a better performance with a recursive call.
Main Topics
Browse All TopicsI can't write a recursive function in java that can solve this:
e = 1/0! + 1/1! + 1/2! + … + 1/n!
for ex:
1 + 1 + 1/2 + 1/6 + 1/24 + 1/120 and so on given the user enters the value of n.
The value of e = 2.71828
The greater the value of n the greater the approximation.
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
i gave into the temptation ;-)
/**
* Title: <p>
* Description: <p>
* Copyright: Copyright (c) <p>
* Company: <p>
* @author
* @version 1.0
*/
import java.util.*;
public class test {
public test() {
}
public static void main(String[] args) {
int n=10;
System.out.println("1/0! + 1/1! +...+i/"+n+"! ="+(new test()).getTotal(n));
}
//1/0! + 1/1! + 1/2! + .. + 1/n!
public double getTotal(int n){
float factorial=1;
for(int i=1;i<=n;i++)
factorial*=i;
double sum=0;
if(n>=0)
sum=sum+(1/factorial)+getT
return sum;
}
}
i gave into the temptation ;-)
/**
* Title: <p>
* Description: <p>
* Copyright: Copyright (c) <p>
* Company: <p>
* @author
* @version 1.0
*/
import java.util.*;
public class test {
public test() {
}
public static void main(String[] args) {
int n=10;
System.out.println("1/0! + 1/1! +...+i/"+n+"! ="+(new test()).getTotal(n));
}
//1/0! + 1/1! + 1/2! + .. + 1/n!
public double getTotal(int n){
float factorial=1;
for(int i=1;i<=n;i++)
factorial*=i;
double sum=0;
if(n>=0)
sum=sum+(1/factorial)+getT
return sum;
}
}
God and Sasha_Mapa, please forgive me!!!
public class eRec extends Object {
public static double expRec(int n, double i, double fact) {
if (n<=1) return 0.0D;
return fact+expRec(n-1,i+1.0D,fac
}
public static void main(String[] args) {
System.out.println(expRec(
System.out.println(expRec(
}
}
As you no doubt know, a recursive method is one that calls itself to accomplish a specific purpose. The tricky part is usually to figure out when to _stop_. In these case of computing factorial values or raising a number to a power, the typical implementation when using recursion involves the method calling itself with a decremented value.
Here is an example of the factorial implemented as a recursive method.
public static long factorial(long n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
Calling this method to compute, say the value of 5! (120 by the way), would involve the following chain of method calls to occur:
Client calls the method: factorial(5), returns 120
method calls itself: factorial(4), returns 24
method calls itself: factorial(3), returns 6
method calls itself: factorial(2), returns 2
method calls itself: factorial(1), returns 1
method calls itself: factorial(0), returns 1
This is obviously a poor implementation of factorial because, as you can see, it calls itself one too many times. The best point at which to stop would have been when the argument value was 1 as this is the point at which the computation reached identity.
As another example, here is an example implementation of the power function implemented as a recursive method.
public static long power(long base, long pow) {
if (pow == 0)
return 1;
else
return base * power(base, pow - 1);
}
As long as the power to which the base is being raised is >0, the method calls itself, multiplying the base by the returned value. When the pow value reaches 0, 1 is returned. This handles the case that the method is originally called by a client with a pow of 0.
Using these as examples, you should be able to complete the implementation of a recursive method that uses the inverse of factorial to approximate e.
Best regards,
Jim Cakalic
Actually, I was typing my comments when the others posted their code. Still, what good is an answer if you don't understand it? And what good is an expert that doesn't explain their answer? Too bad for franjieh. He'll probably use the new computer to play Quake or some other mindless game. I hope his father is smart enough to see through the subterfuge.
Jim
Business Accounts
Answer for Membership
by: Sasha_MapaPosted on 2000-05-18 at 14:12:33ID: 2823379
I'm really tempted to give you a solution for this, but it sounds a lot like homework. Is it? What do you need this for if it's not homework?