Velocity Reviews > Java > problem in my algorithm...

# problem in my algorithm...

[G.P]Georgy™
Guest
Posts: n/a

 01-23-2009
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

Fred
Guest
Posts: n/a

 01-23-2009
On Jan 23, 11:01*am, [G.P]Georgy™ <(E-Mail Removed)> 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

[G.P]Georgy™
Guest
Posts: n/a

 01-24-2009
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

Mark Space
Guest
Posts: n/a

 01-24-2009
[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.

Lew
Guest
Posts: n/a

 01-24-2009
[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

[G.P]Georgy™
Guest
Posts: n/a

 01-24-2009
Ok now!!
and the best performance of the program...

Thanks a lot to who helped me!

--
Georgy Passos

On 24 jan, 02:46, Lew <(E-Mail Removed)> 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???

>

Roedy Green
Guest
Posts: n/a

 01-24-2009
On Sat, 24 Jan 2009 07:39:42 -0800 (PST), [G.P]Georgy™
<(E-Mail Removed)> 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

vj
Guest
Posts: n/a

 01-26-2009
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[i] + 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

John B. Matthews
Guest
Posts: n/a

 01-26-2009
In article
<(E-Mail Removed)>,
vj <(E-Mail Removed)> 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[i] + 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/Charact...nential_functi
on>

--
John B. Matthews
trashgod at gmail dot com