7 Replies - 466 Views - Last Post: 08 October 2012 - 01:54 PM Rate Topic: -----

#1 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 149
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 149
  • Joined: 07-October 12

Re: Finding the approximation of e

Posted 07 October 2012 - 07:26 PM

View PostKinaces, 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.

View Posthotshotennis, on 07 October 2012 - 07:21 PM, said:

View PostKinaces, 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?
Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 149
  • Joined: 07-October 12

Re: Finding the approximation of e

Posted 07 October 2012 - 08:30 PM

View PostKinaces, 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?
Was This Post Helpful? 0
  • +
  • -

#6 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 149
  • Joined: 07-October 12

Re: Finding the approximation of e

Posted 07 October 2012 - 09:07 PM

View PostKinaces, 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?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Finding the approximation of e

Posted 08 October 2012 - 01:54 PM

Please indent your code properly
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1