Question about this java program

  • Context: Java 
  • Thread starter Thread starter vsingh165
  • Start date Start date
  • Tags Tags
    Java Program
Click For Summary

Discussion Overview

This discussion revolves around a Java program intended to approximate the mathematical constant e. Participants analyze the code, identify issues, and suggest corrections related to the series and limit expressions used for calculating e.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant points out that the series used in the original code is incorrect for approximating e, suggesting the correct series involves factorials.
  • Another participant explains that the code is computing a sum rather than a limit, which is essential for the correct approximation of e.
  • Concerns are raised about the use of integer division in the code, which leads to incorrect calculations for values of x greater than 1.
  • Participants highlight the misuse of the ^ operator in Java, which does not perform exponentiation but rather a bitwise operation.
  • A later reply indicates that the participant modified the code by removing the loop and directly using the limit expression, resulting in a more accurate approximation of e.
  • Some participants express skepticism about using the Math.pow() function, suggesting it may not align with the intended educational purpose of the exercise.

Areas of Agreement / Disagreement

Participants generally agree on the issues present in the original code and the importance of using the correct mathematical expressions. However, there is disagreement regarding the appropriateness of using the Math.pow() function versus implementing a manual exponentiation method.

Contextual Notes

Limitations include the original code's reliance on integer arithmetic, which affects the accuracy of the calculations, and the potential misunderstanding of the mathematical concepts involved in approximating e.

vsingh165
OK here is my source code for a small Java program I wrote to approximate the number e:

Code:
import java.util.Scanner;

public class E
{
	public static void main (String[] args)
	{
		double e = 0.00, current_e = 0.00;
		int x, number_of_digits;
		Scanner scan = new Scanner (System.in);

		System.out.println("How many digits?");
		number_of_digits = scan.nextInt();

		for (x=1; x<number_of_digits; x++)
		{
			current_e = (1 + (1/x)) ^ x;
			e += current_e;
		}

		System.out.println("e = " + e);
	}
}

What's wrong with this code? When I run this program and enter 100000 for number_of_digits, I get a result of e = 4.999950002E9. What's up with this?

P.S.: This is the latest Java SDK (1.6) I am compiling this program in.
 
Technology news on Phys.org
You're using the wrong series for e.

Your series:

[tex]e = \left(1 + \frac{1}{1} \right)^1 + \left(1 + \frac{1}{2} \right)^2 + \left(1 + \frac{1}{3} \right)^3 + ...[/tex]

Correct series:
[tex]e = 1 +\frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + ...[/tex]

You've used the limit expression for e instead of the series expression for e.

Also, you might want to sum the series "backwards".
 
At x = 1, your code sets e = (1 + 1)^1 = 2. At x = 2, your code sets e = 2 + (1.5)^2 = 2 + 2.25 = 4.25. At x = 3, your code sets e = 4.25 + (4/3)^3 ~= 6.6. I leave it as an exercise for you to figure out what's going (seriously) wrong here.
 
There are many things wrong with this.
  • the expression for e is

    [tex]e=\lim_{x\to\infty}(1+1/x)^x[/tex]

    You are computing the sum, not the limit.
  • You declared x as an integer. For all x but x=1, 1/x is zero.
  • You are using ^ for exponentiation. In Java, ^ represents the exclusive or operator. For x=1, 1+1/x evaluates to 2 and (1+1/x)^x is 3. For all other x, 1+1/x is just 1 =x. If x is even, 1^x is x+1. If x is odd, 1^x is x-1. Those plus and minus ones more or less cancel leaving just

    [tex]\text{current\_e} = \sum_{x=1}^{N-1} x[/tex]

    which is just N(N-1)/2. With N= 100000, this becomes 50000*99999=4.99995e9.

Use the right formula and use floating point numbers rather than integers where you need floating point numbers.
 
Oh, OK I see what all of you are trying to say. I am adding each approximation of e for each integer x which means my sum is going to be huge...so what I did was I took out the loop completely and just plugged in the number the user enters for x.

Code:
import java.util.Scanner;
import java.lang.Math;

public class E
{
	public static void main (String[] args)
	{
		double e = 0.00, x;
		Scanner scan = new Scanner (System.in);

		System.out.println("Please enter a positive integer or decimal number:");
		x = scan.nextDouble();

		e = Math.pow((1+(1/x)), x);

		System.out.println("e = " + e);
	}
}

And this is what happens when I enter 1 million for x:

Code:
ken@ken-laptop:~/java$ java E
Please enter a positive integer or decimal number:
1000000
e = 2.7182804690957534

Ta da!
 
Last edited by a moderator:
Ta da!

Were you supposed to use a series or the limit expression? Using math.pow() is "cheating" in a sense, as this function evaluates [itex]a^b[/itex] via the relation [itex]a^b = \exp(b\log a)[/itex].
 
D H said:
Ta da!

Were you supposed to use a series or the limit expression? Using math.pow() is "cheating" in a sense, as this function evaluates [itex]a^b[/itex] via the relation [itex]a^b = \exp(b\log a)[/itex].


To make this way easier I used the limit expression (no looping required) instead of the series expression.

Yeah, I see your point about the Math.pow() function in Java. Maybe if I have more time I'll try writing a better exponent function. I just started learning this language and I have to say it is pretty interesting.
 
Last edited by a moderator:

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 1 ·
Replies
1
Views
7K