Results 1 to 9 of 9
 03012013, 10:29 PM #1Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
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; 03012013 at 11:55 PM.
 03012013, 11:13 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,947
 Rep Power
 4
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,
JimLast edited by jim829; 03012013 at 11:27 PM.
The Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 03012013, 11:37 PM #3Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
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.
 03012013, 11:42 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,947
 Rep Power
 4
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).
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 03012013, 11:50 PM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
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.
 03012013, 11:52 PM #6Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,947
 Rep Power
 4
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.
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 03012013, 11:56 PM #7Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
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
 03022013, 12:04 AM #8Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,947
 Rep Power
 4
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 03022013, 01:38 AM #9Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
Similar Threads

using decimal numbers
By iminhell in forum New To JavaReplies: 3Last Post: 10312010, 03:47 AM 
java division and decimal error
By heartysnowy in forum New To JavaReplies: 5Last Post: 10072009, 04:57 PM 
round down to 2 sig. numbers after the decimal point
By soc86 in forum New To JavaReplies: 4Last Post: 01162009, 06:17 PM 
Truncating decimal numbers in the output
By gbade in forum New To JavaReplies: 2Last Post: 11212008, 06:25 PM 
numbers with two decimal places
By little_polarbear in forum New To JavaReplies: 8Last Post: 08272008, 11:04 PM
Bookmarks