Results 1 to 9 of 9
- 03-01-2013, 10:29 PM #1
Member
- 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; 03-01-2013 at 11:55 PM.
- 03-01-2013, 11:13 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 639
- Rep Power
- 1
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; 03-01-2013 at 11:27 PM.
The Java™ Tutorial
YAT -- Yet Another Typo
- 03-01-2013, 11:37 PM #3
Member
- 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.
- 03-01-2013, 11:42 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 639
- Rep Power
- 1
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
YAT -- Yet Another Typo
- 03-01-2013, 11:50 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.
- 03-01-2013, 11:52 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 639
- Rep Power
- 1
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
YAT -- Yet Another Typo
- 03-01-2013, 11:56 PM #7
Member
- 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
- 03-02-2013, 12:04 AM #8
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 639
- Rep Power
- 1
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
YAT -- Yet Another Typo
- 03-02-2013, 01:38 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
Similar Threads
-
using decimal numbers
By iminhell in forum New To JavaReplies: 3Last Post: 10-31-2010, 03:47 AM -
java division and decimal error
By heartysnowy in forum New To JavaReplies: 5Last Post: 10-07-2009, 04:57 PM -
round down to 2 sig. numbers after the decimal point
By soc86 in forum New To JavaReplies: 4Last Post: 01-16-2009, 06:17 PM -
Truncating decimal numbers in the output
By gbade in forum New To JavaReplies: 2Last Post: 11-21-2008, 06:25 PM -
numbers with two decimal places
By little_polarbear in forum New To JavaReplies: 8Last Post: 08-27-2008, 11:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks