Hey common folks let a "newbie" in and please answer my question! :) (even if its darn simple!)

Discussion in 'Java' started by Robocop, Feb 22, 2004.

  1. Robocop

    Robocop Guest

    Hi Folks!
    heres my question

    adding: 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15....

    this is what i tried

    to get ODD nos. i wrote 2*i+1 when *first line* is executed it adds
    up all the odd deno. fraction, and in second sum it subtracts 1/3, 1/7
    ....., so shouldn't i multiple the second line by 2? so that it
    subtract 1/3 , 1/7 and then again subtracts from the remaining sum?
    like this::: sum = sum - 2*sign/(2*i+1) ; // MODIFIED SECOND LINE

    sum = sum + sign /i; //----FIRST LINE
    sum = sum - sign / (2*i+1); //----SECOND LINE



    Code:
    public class Strtry
    {
    public static void main (String[] parameters)
    {
    
    double sum = 0;
    double sign = 1;
    long start = System.currentTimeMillis();
    for (int i = 1; i < 1000000000; i = 2*i +1)
    {
    sum = sum + sign /i;
    sum = sum - sign / (2*i+1);
    
    }
    long finish = System.currentTimeMillis();
    IO.println((finish - start)/1000);
    IO.println(4*sum);
    }
    }
    Robocop, Feb 22, 2004
    #1
    1. Advertising

  2. Robocop

    nos Guest

    "Robocop" <> wrote in message
    news:...
    > Hi Folks!
    > heres my question
    >
    > adding: 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15....
    >
    > this is what i tried
    >
    > to get ODD nos. i wrote 2*i+1 when *first line* is executed it adds
    > up all the odd deno. fraction, and in second sum it subtracts 1/3, 1/7
    > ...., so shouldn't i multiple the second line by 2? so that it
    > subtract 1/3 , 1/7 and then again subtracts from the remaining sum?
    > like this::: sum = sum - 2*sign/(2*i+1) ; // MODIFIED SECOND LINE
    >
    > sum = sum + sign /i; //----FIRST LINE
    > sum = sum - sign / (2*i+1); //----SECOND LINE
    >
    >
    >
    >
    Code:
    > public class Strtry
    > {
    >   public static void main (String[] parameters)
    >    {
    >
    >    double sum = 0;
    >    double sign = 1;
    >    long start = System.currentTimeMillis();
    >    for (int i = 1; i < 1000000000; i = 2*i +1)
    >    {
    >     sum = sum + sign /i;
    >     sum = sum - sign / (2*i+1);
    >
    >    }
    >    long finish = System.currentTimeMillis();
    >    IO.println((finish - start)/1000);
    >    IO.println(4*sum);
    >       }
    >    }[/color]
    --
    System.out.println(2);
    nos, Feb 22, 2004
    #2
    1. Advertising

  3. Robocop

    Andrew Hobbs Guest

    We are not paid servants. It is Saturday night Sunday morning and there are
    probably not many people lurking at the moment. They have better things to
    do. (Plus it would probably be better to try asking these sort of questions
    on comp.lang.java.help).

    Anyway. See my answer to your earlier post.

    In addition it might have helped if you had asked a specific question. ie
    what was wrong with your code?

    I can also give you a hint about problems. In a case like this, what is the
    problem. I presume you aren't getting the answer you expect. (If not why
    are you asking a question. Especially on something as simple as this
    arithmetic problem) In that case the first thing you should suspect is that
    your code is wrong. So why not limit the case to just a few cycles in the
    for loop and get it to print out the first dozen numbers. Just to make sure
    it is doing what you expected. It would have been obvious what is wrong and
    would have suggested a solution. You would learn a whole lot more than
    trying to get other people to write it for you.


    Cheers

    Andrew


    --
    ********************************************************
    Andrew Hobbs PhD

    MetaSense Pty Ltd - www.metasense.com.au
    12 Ashover Grove
    Carine W.A.
    Australia 6020

    61 8 9246 2026
    metasens AntiSpam @iinet dot net dot au


    *********************************************************



    "Robocop" <> wrote in message
    news:...
    > Hi Folks!
    > heres my question
    >
    > adding: 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15....
    >
    > this is what i tried
    >
    > to get ODD nos. i wrote 2*i+1 when *first line* is executed it adds
    > up all the odd deno. fraction, and in second sum it subtracts 1/3, 1/7
    > ...., so shouldn't i multiple the second line by 2? so that it
    > subtract 1/3 , 1/7 and then again subtracts from the remaining sum?
    > like this::: sum = sum - 2*sign/(2*i+1) ; // MODIFIED SECOND LINE
    >
    > sum = sum + sign /i; //----FIRST LINE
    > sum = sum - sign / (2*i+1); //----SECOND LINE
    >
    >
    >
    >
    Code:
    > public class Strtry
    > {
    >   public static void main (String[] parameters)
    >    {
    >
    >    double sum = 0;
    >    double sign = 1;
    >    long start = System.currentTimeMillis();
    >    for (int i = 1; i < 1000000000; i = 2*i +1)
    >    {
    >     sum = sum + sign /i;
    >     sum = sum - sign / (2*i+1);
    >
    >    }
    >    long finish = System.currentTimeMillis();
    >    IO.println((finish - start)/1000);
    >    IO.println(4*sum);
    >       }
    >    }[/color]
    Andrew Hobbs, Feb 22, 2004
    #3
    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. Stan Goodman

    Even older fart, even newer newbie

    Stan Goodman, Jul 3, 2003, in forum: Java
    Replies:
    11
    Views:
    688
    Stan Goodman
    Jul 4, 2003
  2. Luigi Donatello Asero

    Let or not let the text float

    Luigi Donatello Asero, Jan 15, 2004, in forum: HTML
    Replies:
    6
    Views:
    464
    Steve R.
    Jan 15, 2004
  3. Charles Taylor
    Replies:
    0
    Views:
    122
    Charles Taylor
    Oct 24, 2005
  4. thunk
    Replies:
    1
    Views:
    303
    thunk
    Mar 30, 2010
  5. thunk
    Replies:
    0
    Views:
    469
    thunk
    Apr 1, 2010
Loading...

Share This Page