problem in my algorithm...

Discussion in 'Java' started by [G.P]Georgy™, Jan 23, 2009.

  1. Hey guys !
    I have to solve this following question:

    Determine the value of e(x), where:
    e(x) = 1 + x/1! + x²/2! + x³/3! + …
    Consider the terms of the sum until the difference between the values
    obtained
    with addiction of two consecutive terms be inferior to 0,0001.


    What I succeed was this code:

    public class Factorial {

    static int fact(int n){
    int f = 1, i = 1;
    while (i < n){
    i = i + 1;
    f = f * i;
    }
    return f;
    }

    public static void main (String args[]){

    float difference = 1;

    //this variable will acumulate the sum until (difference <
    0.0001).
    //starts with 1, the first term of the sum.
    float e = 1;

    int x = 8;

    while(difference < 0.0001){
    //(????)
    //I don't know how can I calculate this difference; what
    terms I use to do this difference?
    }

    }
    }


    any idea for me??

    thank you !

    --
    Georgy C. C. Passos - from Brazil
     
    [G.P]Georgy™, Jan 23, 2009
    #1
    1. Advertising

  2. [G.P]Georgy™

    Fred Guest

    On Jan 23, 11:01 am, [G.P]Georgy™ <> wrote:
    > Hey guys !
    > I have to solve this following question:
    >
    > Determine the value of e(x), where:
    > e(x) = 1 + x/1! + x²/2! + x³/3! + …
    > Consider the terms of the sum until the difference between the values
    > obtained
    > with addiction of two consecutive terms be inferior to 0,0001.
    >
    > What I succeed was this code:
    >
    > public class Factorial {
    >
    >     static int fact(int n){
    >         int f = 1, i = 1;
    >         while (i < n){
    >             i = i + 1;
    >             f = f * i;
    >         }
    >         return f;
    >     }
    >
    >     public static void main (String args[]){
    >
    >         float difference = 1;
    >
    >         //this variable will acumulate the sum until (difference <
    > 0.0001).
    >         //starts with 1, the first term of the sum.
    >         float e = 1;
    >
    >         int x = 8;
    >
    >         while(difference < 0.0001){
    >             //(????)
    >             //I don't know how can I calculate this difference; what
    > terms I use to do this difference?
    >         }
    >
    >     }
    >
    > }
    >
    > any idea for me??
    >
    > thank you !
    >


    You don't want to keep computing factorials.
    You want something like this:

    sum=1.;
    n=1;
    b=x;
    a = 0;
    while ( fabs(a-b) > tolerance ) {
    a = b;
    sum += a;
    b = (a * x) / ++n;
    }

    --
    Fred K
     
    Fred, Jan 23, 2009
    #2
    1. Advertising

  3. Hey Fred and Rossum!
    I think I got what you are explaining to me:

    /** Calculates factorial */
    static int fact(int n) {
    int result = 1;
    while (n > 1) {
    result *= n--;
    }
    return result;
    }

    /** Calculates e(x) */
    static double e(int x) {
    final double delta = 0.0001;
    double result = 1.0;
    double a = 1.0;
    double b = 1.0;
    double difference = 1.0;
    int i = 1;

    while (difference > delta) {
    b = Math.pow(x, i)/fact(i);
    result += b;

    difference = b - a;

    a = b;
    i++;
    }

    return result;
    }

    I think it's solved.
    But...
    I'm still confused in the following part of question: "with addiction
    of two consecutive terms".
    where is this addiction???


    Thanks for replies!
    Greetings from Brazil.

    --
    Georgy C. C. Passos
     
    [G.P]Georgy™, Jan 24, 2009
    #3
  4. [G.P]Georgy™

    Mark Space Guest

    [G.P]Georgy™ wrote:

    > I'm still confused in the following part of question: "with addiction
    > of two consecutive terms".
    > where is this addiction???


    "Determine the value of e(x), where:
    e(x) = 1 + x/1! + x²/2! + x³/3! + …
    Consider the terms of the sum until the difference between the values
    obtained..."

    See the plus signs up there? That's the addition. Each item involving
    an X there is called a "term" in algebra.
     
    Mark Space, Jan 24, 2009
    #4
  5. [G.P]Georgy™

    Lew Guest

    [G.P]Georgyâ„¢ wrote:
    > Hey Fred and Rossum!
    > I think I got what you are explaining to me:
    >
    > /** Calculates factorial */
    > static int fact(int n) {
    > int result = 1;
    > while (n > 1) {
    > result *= n--;
    > }
    > return result;
    > }
    >
    > /** Calculates e(x) */
    > static double e(int x) {
    > final double delta = 0.0001;
    > double result = 1.0;
    > double a = 1.0;
    > double b = 1.0;
    > double difference = 1.0;
    > int i = 1;
    >
    > while (difference > delta) {
    > b = Math.pow(x, i)/fact(i);
    > result += b;
    >
    > difference = b - a;
    >
    > a = b;
    > i++;
    > }
    >
    > return result;
    > }
    >
    > I think it's solved.


    Fred's version will be much faster and kinder to the stack.

    Note that Fred omitted type declarations from his snippet:
    You want something like this:

    >> sum=1.;
    >> n=1;
    >> b=x;
    >> a = 0;
    >> while ( fabs(a-b) > tolerance ) {
    >> a = b;
    >> sum += a;
    >> b = (a * x) / ++n;
    >> }


    I'd go with
    <snippet>
    private static final double TOLERANCE = 0.0001;

    public final double ePower( double x )
    {
    return ePower( x, TOLERANCE );
    }

    public final double ePower( double x, double tolerance )
    {
    if ( tolerance <= Double.MIN_NORMAL )
    {
    tolerance = TOLERANCE;
    }
    double sum = 1.;
    long n = 1L;
    for ( double a = 0.0, b = x; Math.abs( a - b ) > tolerance; )
    {
    a = b;
    sum += a;
    b = (a * x) / ++n;
    }
    return sum;
    }
    </snippet>

    > I'm still confused in the following part of question: "with addiction
    > of two consecutive terms".
    > where is this addiction???


    "Addition" - see Mark Space's answer.

    --
    Lew
     
    Lew, Jan 24, 2009
    #5
  6. Ok now!!
    Everything understood about the "addition",
    and the best performance of the program...

    Thanks a lot to who helped me!

    --
    Georgy Passos

    On 24 jan, 02:46, Lew <> wrote:
    > [G.P]Georgy™ wrote:
    > > Hey Fred and Rossum!
    > > I think I got what you are explaining to me:

    >
    > >         /** Calculates factorial */
    > >    static int fact(int n) {
    > >        int result = 1;
    > >        while (n > 1) {
    > >            result *= n--;
    > >        }
    > >        return result;
    > >    }

    >
    > >      /** Calculates e(x) */
    > >      static double e(int x) {
    > >        final double delta = 0.0001;
    > >        double result = 1.0;
    > >        double a = 1.0;
    > >             double b = 1.0;
    > >        double difference = 1.0;
    > >        int i = 1;

    >
    > >        while (difference > delta) {
    > >            b = Math.pow(x, i)/fact(i);
    > >            result += b;

    >
    > >            difference = b - a;

    >
    > >            a = b;
    > >            i++;
    > >        }

    >
    > >        return result;
    > >      }

    >
    > > I think it's solved.

    >
    > Fred's version will be much faster and kinder to the stack.
    >
    > Note that Fred omitted type declarations from his snippet:
    > You want something like this:
    >
    > >> sum=1.;
    > >> n=1;
    > >> b=x;
    > >> a = 0;
    > >> while ( fabs(a-b) > tolerance ) {
    > >>    a = b;
    > >>    sum += a;
    > >>    b = (a * x) / ++n;
    > >> }

    >
    > I'd go with
    > <snippet>
    >   private static final double TOLERANCE = 0.0001;
    >
    >   public final double ePower( double x )
    >   {
    >     return ePower( x, TOLERANCE );
    >   }
    >
    >   public final double ePower( double x, double tolerance )
    >   {
    >    if ( tolerance <= Double.MIN_NORMAL )
    >    {
    >     tolerance = TOLERANCE;
    >    }
    >    double sum = 1.;
    >    long n = 1L;
    >    for ( double a = 0.0, b = x; Math.abs( a - b ) > tolerance; )
    >    {
    >      a = b;
    >      sum += a;
    >      b = (a * x) / ++n;
    >    }
    >    return sum;
    >   }
    > </snippet>
    >
    > > I'm still confused in the following part of question: "with addiction
    > > of two consecutive terms".
    > > where is this addiction???

    >
    > "Addition" - see Mark Space's answer.
     
    [G.P]Georgy™, Jan 24, 2009
    #6
  7. [G.P]Georgy™

    Roedy Green Guest

    On Sat, 24 Jan 2009 07:39:42 -0800 (PST), [G.P]Georgy™
    <> wrote, quoted or indirectly quoted someone who
    said :

    >Everything understood about the "addition",
    >and the best performance of the program...


    One more hint to problem of this sort. When you have a large number of
    things to add up, the result will be more accurate if you start with
    the smallest and work toward the biggest.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    We are almost certainly going to miss our [global warming] deadline.
    We cannot get the 10 lost years back, and by the time a new global agreement to
    replace the Kyoto accord is negotiated and put into effect, there will probably
    not be enough time left to stop the warming short of the point where we must not
    go. ~ Gwynne Dyer
     
    Roedy Green, Jan 24, 2009
    #7
  8. [G.P]Georgy™

    vj Guest

    I see a problem in freds algorithm for x < 0.

    As evident that for x<0 terms will alternate between +ve and -ve
    values. So its quite quite easy to see two terms T(i),T(i+1) such
    that T(i)+T(i+1) < Tolerence but FABS(T(i)-T(i+1)) >Tolerence.

    Since the basic requirement is that iteration should continue untill
    SUM of TWO Consicutive terms (T + T[i+1]) is inferior(less) then
    0.0001 hence freds algorithm would continue for some extra iterations
    before concluding.

    So i suggest that the loop predicate "fabs(a-b) > tolerance" be
    changed to "a+b>tolerance".

    Kindly correct me if i am wrong,

    VJ
     
    vj, Jan 26, 2009
    #8
  9. In article
    <>,
    vj <> wrote:

    > I see a problem in freds algorithm for x < 0.


    Also for positive x that result in a == b.

    > As evident that for x<0 terms will alternate between +ve and -ve
    > values.


    Interesting. For x > 0, the sum advances smoothly toward the correct
    value from below; for x < 0, the increment alternates between plus and
    minus as the new term is even or odd, respectively. There's an animated
    GIF showing this effect, here:

    <http://en.wikipedia.org/wiki/Exponential_function>

    > So its quite quite easy to see two terms T(i),T(i+1) such
    > that T(i)+T(i+1) < Tolerence but FABS(T(i)-T(i+1)) >Tolerence.
    >
    > Since the basic requirement is that iteration should continue untill
    > SUM of TWO Consicutive terms (T + T[i+1]) is inferior(less) then
    > 0.0001 hence freds algorithm would continue for some extra iterations
    > before concluding.
    >
    > So i suggest that the loop predicate "fabs(a-b) > tolerance" be
    > changed to "a+b>tolerance".
    >
    > Kindly correct me if i am wrong,


    I'm not sure. The series converges, so I'd use Math.abs(b) > tolerance:

    <http://en.wikipedia.org/wiki/Characterizations_of_the_exponential_functi
    on>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jan 26, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam

    algorithm problem

    Adam, Nov 3, 2003, in forum: VHDL
    Replies:
    5
    Views:
    3,159
    A123b456c
    Nov 8, 2003
  2. Ahmed Moustafa
    Replies:
    0
    Views:
    797
    Ahmed Moustafa
    Nov 15, 2003
  3. JimC
    Replies:
    3
    Views:
    528
  4. Allan W
    Replies:
    4
    Views:
    535
    Jos A. Horsmeier
    Jan 22, 2004
  5. Bapaiah Katepalli
    Replies:
    1
    Views:
    1,512
    Mike Treseler
    Jun 23, 2006
Loading...

Share This Page