Getting a specific digit from a ratio expansion in any base (nth digit of x/y)

后端 未结 5 629
一向
一向 2020-12-09 06:23

Is there an algorithm that can calculate the digits of a repeating-decimal ratio without starting at the beginning?

I\'m looking for a solution that doesn\'t use ar

5条回答
  •  醉梦人生
    2020-12-09 07:15

    As a general technique, rational fractions have a non-repeating part followed by a repeating part, like this:

    nnn.xxxxxxxxrrrrrr

    xxxxxxxx is the nonrepeating part and rrrrrr is the repeating part.

    1. Determine the length of the nonrepeating part.
    2. If the digit in question is in the nonrepeating part, then calculate it directly using division.
    3. If the digit in question is in the repeating part, calculate its position within the repeating sequence (you now know the lengths of everything), and pick out the correct digit.

    The above is a rough outline and would need more precision to implement in an actual algorithm, but it should get you started.

提交回复
热议问题