# problem in my algorithm...

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

1. ### [G.P]Georgy™Guest

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.

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

2. ### FredGuest

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.
>
>
> 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

3. ### [G.P]Georgy™Guest

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".

Thanks for replies!
Greetings from Brazil.

--
Georgy C. C. Passos

[G.P]Georgy™, Jan 24, 2009
4. ### Mark SpaceGuest

[G.P]Georgy™ wrote:

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

"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
5. ### LewGuest

[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".

--
Lew

Lew, Jan 24, 2009
6. ### [G.P]Georgy™Guest

Ok now!!
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???

>

[G.P]Georgy™, Jan 24, 2009
7. ### Roedy GreenGuest

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

>and the best performance of the program...

One more hint to problem of this sort. When you have a large number of
the smallest and work toward the biggest.
--
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
8. ### vjGuest

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
9. ### John B. MatthewsGuest

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