Jump to content

Need help with a mathematical program (only 17 lines long)

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Dethlehem

Dethlehem

    Newbie

  • Members
  • Pip
  • 7 posts
Hi, I have been trying to complete the following task for a while now:

"Every mathematician knows that the sum of the series
1 + 1/2 + 1/3 + 1/4 + ...
is infinite. But computers are funny things. Write a program which adds up the numbers in this series until the result of adding another one doesn’t change the sum. Use float variables for the arithmetic. Also, find out how many terms in the series are used.
[ The answer is 15.403683 and the number of terms added is 2097152. ] "



I have got this far:

class Terms {


    public static void main(String[] args) {


        int n=1;

        float lastFx = 0;

        float fx=1;


        while (lastFx != fx) {

            n++;

            lastFx = 1/n;

            fx = 1/(n+1);

        }


        System.out.println("number of terms used: " + n);

    }

}

from this, all the system prints out is: "number of terms used: 2" or "number of terms is 3", or 0.

Why can't i get this program working? :/

Any help is much appreciated

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You're doing integer division, not floating point division.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Dethlehem

Dethlehem

    Newbie

  • Members
  • Pip
  • 7 posts

WingedPanther said:

You're doing integer division, not floating point division.

ok thanks :), I corrected the mistake, all variables, including n are now of float type.

But now i get "number of terms used: 1.1864338E7", and not "2097152", and apparently, that's what it should be? o_O

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
You're losing some precision.
If you want to retain this precision use the BigDecimal class.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
If you'd use BigDecmal, the while loop lasts forever I think.

2 errors you have:
  • Calculation of lastFx
  • calculation of fx
lastFx is simple, just do

lastFx = fx;

I'll let you think again for the calculation of fx itself :P

Also note, that to declare a floating point number you must add the letter 'f' behind the number:

1 = int

1.0 = double

[B]1f = float

1.0f = float[/B]

1L = long


It's not required for
float fx = 1;
That will get autocasted, but in the formula of calculating fx you propably need it.

And finally: variable 'n' CAN be an integer. It'll get autocasted anyway.

And finally finally: 2097152 is indeed the correct answer

#6
Dethlehem

Dethlehem

    Newbie

  • Members
  • Pip
  • 7 posts

wim DC said:

If you'd use BigDecmal, the while loop lasts forever I think.

2 errors you have:
  • Calculation of lastFx
  • calculation of fx
lastFx is simple, just do

lastFx = fx;

I'll let you think again for the calculation of fx itself :P

Also note, that to declare a floating point number you must add the letter 'f' behind the number:

1 = int

1.0 = double

[B]1f = float

1.0f = float[/B]

1L = long


It's not required for
float fx = 1;
That will get autocasted, but in the formula of calculating fx you propably need it.

And finally: variable 'n' CAN be an integer. It'll get autocasted anyway.

And finally finally: 2097152 is indeed the correct answer

Thanks for the reply, it has been the most helpful so far, but I still can't do this :/ I convinced myself it's my lack of understanding of float variables, because the series is easy as hell to understand o_O

this is how far i got now:
class Terms {


    public static void main(String[] args) {


        int n = 1;

        float lastFx = 0f;

        float fx=1f/n;


        while (lastFx != fx) {

        

            lastFx = fx;

        fx = 1f/n;

        n++;

        }


        System.out.println("number of terms used: " + n);

    }

}

& the output i get from this is "numer of terms used: 2" :S

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Step through your logic:
You set fx = lf/n
You set lastFx = fx
You set fx = lf/n (no change)
You increment n to 2
loop exits
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You still calculate fx wrong. It's not 1/n

#9
Dethlehem

Dethlehem

    Newbie

  • Members
  • Pip
  • 7 posts
can someone give an answer to end my little bit of frustration with this question please? Because I just don't understand this :/




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users