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:


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).

  • 1
    $\begingroup$ Starting with SequencePosition[First@RealDigits@FractionalPart@N[Pi, 30], {7, 9, 3}, 1][[1, 1]], convert this to a function. $\endgroup$
    – Bob Hanlon
    Commented Dec 4, 2022 at 13:51
  • 4
    $\begingroup$ Google search came up with this. $\endgroup$ Commented Dec 4, 2022 at 13:52
  • $\begingroup$ try FirstPosition function $\endgroup$
    – nufaie
    Commented Dec 4, 2022 at 14:39
  • $\begingroup$ This is related closely Finding long strings of identical digits in transcendental numbers. $\endgroup$
    – Artes
    Commented Dec 5, 2022 at 4:16
  • $\begingroup$ 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 $\endgroup$
    – Lingling
    Commented Dec 5, 2022 at 7:02

3 Answers 3


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).


Using string-functions

ds = StringRiffle[First @ RealDigits @ N[Pi, 1000], ""];

StringPosition["793"] @ ds

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

For repeated queries this is faster than SequencePosition (there is a certain time expense in creating ds).


@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.