Efficiently computing the first 20-digit substring to repeat in the decimal expansion of Pi

前端 未结 4 787
天涯浪人
天涯浪人 2020-12-15 23:52

Problem

Pi = 3.14159 26 5358979323846 26 433... so the first 2-digit substring to repeat is 26.

What is an efficient way

4条回答
  •  清酒与你
    2020-12-16 00:26

    Perhaps something like this will work:

    1. Search for repeated substrings of length 2 (or some small base case), record starting indicies S={s_i}

    2. For n=3..N, look for substrings of length n from the indices in S

    3. Each iteration, update S with substrings of length n

    4. at n=20, the first two indicies will be your answer

    you might want to adjust the initial size and step size (it might not be necessary to step by 1 each time)

提交回复
热议问题