Compute and approximat value for euler's constant


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Compute and approximat value for euler's constant

  1. #1
    Join Date
    Oct 2005
    Location
    Chicago
    Posts
    24

    Compute and approximat value for euler's constant

    the problem is for me to compute and approxiamte value for euler's constant by summing the series (adding up the terms 1, 1/2, ...., 1/n in that order and then subtracting the value fo In(n)).
    I have to do this with both double and float arithmetic.

    Here is what I have so far.

    import java.io.*;
    public class PP2 {
    public static void main(String[] args) throws

    IOException {
    System.out.println("Enter the limit for sum:");
    BufferedReader kbd = new BufferedReader(new

    InputStreamReader(System.in));
    String S = kbd.readLine();
    int loops = Integer.parseInt(S.trim());
    int n;
    for (n = 1; n <= loops; n++)
    {
    double sum = sum + (double)(1/n);
    float sum2 = sum2 + (float)(1/n);
    }
    double condoub = sum - (double)Math.log(loops);
    float conflout = sum2 - (float)Math.log(loops);
    System.out.println("Euler's const, double: =" + condoub);
    System.out.println("Euler's const, flout: =" + conflout);
    }
    }
    thanks for the help

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You should declare the sum and sum2 prior to the loop, the way you have
    coded it will not accumulate, it doesn't even compile

    Code:
    import java.io.*;
    
    public class PP2 {
      public static void main(String[] args) throws IOException {
    
        System.out.println("Enter the limit for sum:");
        BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));
        String S = kbd.readLine();
        int loops = Integer.parseInt(S.trim());
        int n;
        double sum=0;
        float sum2=0;
        for (n = 1; n <= loops; n++) {
          sum = sum + (double) (1 / n);
          sum2 = sum2 + (float) (1 / n);
        }
        double condoub = sum - (double) Math.log(loops);
        float conflout = sum2 - (float) Math.log(loops);
        System.out.println("Euler's const, double: =" + condoub);
        System.out.println("Euler's const, flout: =" + conflout);
      }
    }
    eschew obfuscation

  3. #3
    Join Date
    Oct 2005
    Location
    Chicago
    Posts
    24
    Thanks I just fixed that it works fine now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center