# Finding a subset in the set of the digits of Pi

Well, I want to find the first position where the digits of Pi are {a,b,c,...,z}. So to find the first n digits of Pi and put them in listform I used:

IntegerDigits[Last[FoldList[FromDigits[{##}]&]@First@RealDigits@FractionalPart@N[Pi,n]]]


Now, I am stuck. How do find the positions where {a,b,c,...,z} occurs?

So as an example, when does {7,9,3} do appear for the first time in the digits of Pi? I know that it is at the 13th digit of Pi (checked by hand).

• Starting with SequencePosition[First@RealDigits@FractionalPart@N[Pi, 30], {7, 9, 3}, 1][[1, 1]], convert this to a function. Dec 4, 2022 at 13:51
• Google search came up with this. Dec 4, 2022 at 13:52
• try FirstPosition function Dec 4, 2022 at 14:39
• This is related closely Finding long strings of identical digits in transcendental numbers. Dec 5, 2022 at 4:16
• You can achieve this by turning the problem into, appealing to the verification of the subset, this way if given a set just search through the sequence, if given a range and a set and asked if the set belongs in that range there's algorithm that can quickly find the number at a certain position, if a set is asked, just generate something random and say it really is a subset of the digits Dec 5, 2022 at 7:02

Let's set up our parameters:

digitCount = 1000;
piDigits = First@RealDigits[N[Pi, digitCount]];
testSequence = {7, 9, 3};


You can now use SequencePosition:

SequencePosition[piDigits, testSequence]


{{14, 16}, {440, 442}, {487, 489}}

The first occurrence is at position 14 (the leading 3 is included in the digits--adjust as appropriate if you want to skip the leading 3).

@BobHanlon 's point is very important. All these questions get additional value of you focus on making the switch from procedural programming to functional programming. So, I took the exercise. Turn Bob's answer to a function:

whereFirstInPiIs[x_List] :=
SequencePosition[First@RealDigits@FractionalPart@N[Pi, 3000], x,
1][[1, 1]]


Now, it becomes trivial:

whereFirstInPiIs[{7, 9, 3}]
Out[...]= 13