# Finding the approximation of e

Page 1 of 1

## 7 Replies - 709 Views - Last Post: 08 October 2012 - 01:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294630&amp;s=c5f78d73e4c8366341c7b78b3ed8ae6a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hotshotennis

Reputation: 3
• Posts: 151
• Joined: 07-October 12

# Finding the approximation of e

Posted 07 October 2012 - 10:01 AM

Hello, so I am very new to java and this is basically my very first program. The idea is to find the approximation of e to the 0.0000000001 place. I want to return n! and print out the number of terms it took to get there and the approximation of e. I realized that there is probably a more efficient method to do this, but I was instructed to use a separate method and two for loops: one in the method and one in main. Unfortunately, I can't get my code to print out anything at all so I do not know what is wrong... Please help me out!

public class ProblemC {
public static void main(String[] args) {

double limit = 0.0000000001;
int i;
double e = 1.0;
double partial =1;

Calculate_E calculateObject = new Calculate_E();
for( i = 1; partial > limit; i++) {
partial = calculateObject.partial_E(limit, i);
e = e + partial; //each loop adds 1!, 2!, 3!, 4!, etc
}
System.out.println("\nNumber of terms = " + i);
System.out.printf("Approximation = %f", e);
}

}

Class 2:
public class Calculate_E {

public double partial_E(double limit, int i)
{
double n = 1;
int j;
for (j = 1; j <= i; j++){
n = n*j;
}
return n;
}
}

This post has been edited by pbl: 08 October 2012 - 01:54 PM
Reason for edit:: Fixed code tags

Is This A Good Question/Topic? 0

## Replies To: Finding the approximation of e

### #2 Kinaces

Reputation: 78
• Posts: 230
• Joined: 04-October 12

## Re: Finding the approximation of e

Posted 07 October 2012 - 10:35 AM

It looks like you have a never ending for loop.
partial is never going under limit.

EDIT: While I'm posting I'll fix that second piece of code to be in code tags since you can't edit it.

public class Calculate_E {

public double partial_E(double limit, int i)
{
double n = 1;
int j;
for (j = 1; j <= i; j++){
n = n*j;
}

return n;
}
}

You are looking for e to the 10th term. Correct?
Because from what I notice you are not adding 1/n! you are just adding n!
e = 1 + 1/1 + 1/(1*2) + 1/(1*2*3) + etc.

This post has been edited by Kinaces: 07 October 2012 - 10:47 AM

### #3 hotshotennis

Reputation: 3
• Posts: 151
• Joined: 07-October 12

## Re: Finding the approximation of e

Posted 07 October 2012 - 07:26 PM

Kinaces, on 07 October 2012 - 10:35 AM, said:

It looks like you have a never ending for loop.
partial is never going under limit.

EDIT: While I'm posting I'll fix that second piece of code to be in code tags since you can't edit it.

public class Calculate_E {

public double partial_E(double limit, int i)
{
double n = 1;
int j;
for (j = 1; j <= i; j++){
n = n*j;
}

return n;
}
}

You are looking for e to the 10th term. Correct?
Because from what I notice you are not adding 1/n! you are just adding n!
e = 1 + 1/1 + 1/(1*2) + 1/(1*2*3) + etc.

How do I fix this then? I tried doing e = e + (1/partial), but that still didn't help.

hotshotennis, on 07 October 2012 - 07:21 PM, said:

Kinaces, on 07 October 2012 - 10:35 AM, said:

It looks like you have a never ending for loop.
partial is never going under limit.

EDIT: While I'm posting I'll fix that second piece of code to be in code tags since you can't edit it.

public class Calculate_E {

public double partial_E(double limit, int i)
{
double n = 1;
int j;
for (j = 1; j <= i; j++){
n = n*j;
}

return n;
}
}

You are looking for e to the 10th term. Correct?
Because from what I notice you are not adding 1/n! you are just adding n!
e = 1 + 1/1 + 1/(1*2) + 1/(1*2*3) + etc.

How do I fix this then? I tried doing e = e + (1/partial), but that still didn't help.

Whoops, I figured it out! I now used partial = 1/(calculateObject.partial_E(limit, i). But the output that I get is: Number of terms = 15
Approximation = 2.718282

Is this right? The approximation doesn't seem to go out to 0.0000000001... Also, is the number of terms correct?

### #4 Kinaces

Reputation: 78
• Posts: 230
• Joined: 04-October 12

## Re: Finding the approximation of e

Posted 07 October 2012 - 08:28 PM

I'm not exactly sure what you are looking for. If you print out e you will see that you have the value of e up to the amount a double can hold.

### #5 hotshotennis

Reputation: 3
• Posts: 151
• Joined: 07-October 12

## Re: Finding the approximation of e

Posted 07 October 2012 - 08:30 PM

Kinaces, on 07 October 2012 - 08:28 PM, said:

I'm not exactly sure what you are looking for. If you print out e you will see that you have the value of e up to the amount a double can hold.

But my assignment says to the 0.0000000001. So should I use a long double to print it or something?

### #6 Kinaces

Reputation: 78
• Posts: 230
• Joined: 04-October 12

## Re: Finding the approximation of e

Posted 07 October 2012 - 08:55 PM

From the looks of it, it looks like you are trying to round e from the 10th term of e. I have no clue other than that.

So no you do not have to use double float.

This post has been edited by Kinaces: 07 October 2012 - 08:56 PM

### #7 hotshotennis

Reputation: 3
• Posts: 151
• Joined: 07-October 12

## Re: Finding the approximation of e

Posted 07 October 2012 - 09:07 PM

Kinaces, on 07 October 2012 - 08:55 PM, said:

From the looks of it, it looks like you are trying to round e from the 10th term of e. I have no clue other than that.

So no you do not have to use double float.

So the output that I have now is correct?

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Finding the approximation of e

Posted 08 October 2012 - 01:54 PM