Results 1 to 9 of 9
  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default More efficient way to achieve most decimal numbers after division

    I currently have this but it takes forever to run. Is there a faster possible solution?

    Java Code:
    int scale = 1000000;
    BigDecimal num1 = new BigDecimal(103993);
    BigDecimal num2 = new BigDecimal(33102);
    System.out.println(num1.divide(num2, scale, RoundingMode.HALF_UP).toString());
    Last edited by hoosierfan24; 03-01-2013 at 11:55 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: More efficient way to achieve most decimal numbers after division

    Hmm. Define forever. You are calculating a ratio to 10000 digits. What exactly are you trying to do?

    Regards,
    Jim
    Last edited by jim829; 03-01-2013 at 11:27 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default Re: More efficient way to achieve most decimal numbers after division

    forever being 10 minutes. I feel like there has to be a quick way to calculate it in a few seconds at most What I want to do is to be able to compare similar fractions that display a number close to pi and see at which digits they differ for example the numbers

    22/7, 355/113, 103993/33102.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: More efficient way to achieve most decimal numbers after division

    Well, I ran the snippet you included in the thread and it ran instantly (less than a second).

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: More efficient way to achieve most decimal numbers after division

    It takes about 50ms when I try it.

    And that includes printing the 10K digits which is a bit pointless since they don't need to be read and they mostly consist of 11902604072261494773729684007008639961331641592653 0 repeated over and over again.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: More efficient way to achieve most decimal numbers after division

    Yeah. I didn't mention the repeating fraction part. My particular segment repeated 195 times. And I ran 100 of these in less than 10 seconds.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default Re: More efficient way to achieve most decimal numbers after division

    Sorry I didnt have the correct amount of zeros in that one. With the new code it takes forever

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: More efficient way to achieve most decimal numbers after division

    Okay, well you might want to add some logic to the code to detect when the repetition starts. Rational numbers are either exact or will repeat. And the cycle of repetition is related to the denominator. You can search the web for "decimal expansions" to get more on the subject.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default Re: More efficient way to achieve most decimal numbers after division

    thanks! I believe thats my problem

Similar Threads

  1. using decimal numbers
    By iminhell in forum New To Java
    Replies: 3
    Last Post: 10-31-2010, 03:47 AM
  2. java division and decimal error
    By heartysnowy in forum New To Java
    Replies: 5
    Last Post: 10-07-2009, 04:57 PM
  3. Replies: 4
    Last Post: 01-16-2009, 06:17 PM
  4. Truncating decimal numbers in the output
    By gbade in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 06:25 PM
  5. numbers with two decimal places
    By little_polarbear in forum New To Java
    Replies: 8
    Last Post: 08-27-2008, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •