Project Euler 751 - Concatenation Coincidence

Official link: https://projecteuler.net/problem=751

Thought Process

First I made a quick function:

  1. sequence(theta, length)

    • Takes 2 input's, first one is a float(theta) from the problem statement and a int(length)

    • returns tau up till desired length

      1. For example sequence(2.956938891377988, 3) = '2.3' (Includes the dot)

      2. sequence(2.956938891377988, 4) = '2.35' (Includes the dot)


Using this function I almost solved this problem by hand:

  1. We know a1 = 2, this implies that 2 < theta < 3

  2. Try theta = 2.1 which means sequence(2.1, 3) = 2.2, therefore 2.1 is too small

  3. Try theta = 2.2 => sequence(2.2, 3) = 2.2 so we can continue from here

  4. As a sanity check let theta = 2.3 => sequence(2.3, 3) = 2.2, so 2.3 is too large

So then we continue knowing theta > 2.2

  1. Try theta = 2.20, 2.21, 2.22, ... etc and test sequence(theta, 4) in the same way as above, and we continue when we find a new digit that matches get a closer estimation

  2. Continue doing this up till 26 digits (too allow some leeway and the dot) and you can get the answer

Arbitrary digit precision can be used with the Decimal module, but it works fine up till around the 30th digit from what others have shown

Interactive Code

Enter a number (yourinput)

Code will output theta rounded to the yourinput decimal place