Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the infinite value of a division? [duplicate]

Tags:

c#

oop

algorithm

Possible Duplicate:
How to know the repeating decimal in a fraction?

1/3 is different by 3/10. 0.33333 != 0.3

So 1/3 will be 0.3 (with a line above number three)

1/12 = 0.833333 = 0.083 (with a line above number three)

1/13 = 0.076923076923 = 0.|076923|

Those lines represent the repeating part.

I plan to have this model in a class. I'm a bit lost on this situation. I just need some ideas to know determine the repeating value. Thanks.

like image 882
Darf Zon Avatar asked Jan 16 '23 05:01

Darf Zon


1 Answers

Cycle detection algorithm is the answer. You can use Floyd's cycle detection algorithm or Brent's cycle detection algorithm.

The function to plug into those algorithms is the function to produce the next digit of the quotient.

like image 63
nhahtdh Avatar answered Feb 04 '23 17:02

nhahtdh