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

后端 未结 5 640
一向
一向 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:09

    Ad hoc I have no good idea. Maybe continued fractions can help. I am going to think a bit about it ...

    UPDATE

    From Fermat's little theorem and because 39 is prime the following holds. (= indicates congruence)

    10^39 = 10 (39)
    

    Because 10 is coprime to 39.

    10^(39 - 1) = 1 (39)
    
    10^38 - 1 = 0 (39)
    
    [to be continued tomorow]
    

    I was to tiered to recognize that 39 is not prime ... ^^ I am going to update and the answer in the next days and present the whole idea. Thanks for noting that 39 is not prime.

    The short answer for a/b with a < b and an assumed period length p ...

    • calculate k = (10^p - 1) / b and verify that it is an integer, else a/b has not a period of p
    • calculate c = k * a
    • convert c to its decimal represenation and left pad it with zeros to a total length of p
    • the i-th digit after the decimal point is the (i mod p)-th digit of the paded decimal representation (i = 0 is the first digit after the decimal point - we are developers)

    Example

    a = 3
    b = 7
    p = 6
    
    k = (10^6 - 1) / 7
      = 142,857
    
    c = 142,857 * 3
      = 428,571
    

    Padding is not required and we conclude.

    3     ______
    - = 0.428571
    7
    

提交回复
热议问题