# Thread: More efficient way to achieve most decimal numbers after division

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-02-2013 at 12:55 AM.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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-02-2013 at 12:27 AM.

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.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

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

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

9. Member
Join Date
Oct 2010
Posts
45
Rep Power
0

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

thanks! I believe thats my problem

#### Posting Permissions

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