Find a sequence in the binary digits of π

JavaScript (Node.js),  167  165 bytes

This is long and slow ... but it should work for any input in theory.

s=>{for(k=t=1n,l=q=2n,n=0n,r=6n,o='#';!~(;)n=4n*q+t*~n<r?(o+=n,q+(q+=q)-r)/t-n<<(r+=r+t*n*2n)/r:(q*7n*k+2n-r*++l)/(r=(r-q-q)*l,q*=k++,t*=l++);return i}

Try it online!

This is derived from the C# entry on this Rosetta Code page.

The C# code is derived from Java, which is derived from Icon, which is derived from PicoLisp, which is derived from Haskell, which is based on this paper. :-p

Wolfram Language (Mathematica), 49 bytes


Try it online!

thanks @Roman for -2bytes and @att for -3 bytes

MATL, 28 bytes

`[email protected]$W*[email protected]&ZaGXftn~

Try it online!

The code is very inefficient. Test cases beyond the second time out in the online compiler, but work offline. Here's an example with the fourth case (it takes about 25 seconds):

enter image description here


The code keeps trying increasingly long truncated binary expansions of the fractional part of , until the input is contained in the expansion.

`        % Do...while
  YP     %   Push pi as a double
  X$     %   Convert to symbolic. This recognizes pi's double representation
  3-     %   Subtract 3
  @      %   Push iteration index, n, starting at 1
  X$     %   Convert to symbolic. This avoids overflow in the next operation
  W      %   Exponential with base 2
  *      %   Multiply
  k      %   Floor(round down). This gives floor((pi-3)*2^n) as a symbolic variable 
  c      %   Convert to char. This gives a sequence of decimal digits
  4Y2    %   Push '0123456789' (input alphabet for base conversion)
  2      %   Push 2 (output base for conversion)
  @      %   Push n (number of digits in the output)
  &Za    %   Base conversion. This gives the binary expansion with n digits
  G      %   Push input
  Xf     %   Index of ocurrences of the input. May be empty
  t      %   Duplicate
  n~     %   Number of elements, negate. This gives 1 if the input was not found
         % End (implicit). A new iteration is run if the top of the stack is 1
         % Display (implicit). The stack contains several empty arrays, and a 
         % non-empty array with the solution. Empty arrays are not displayed