Self-Locating Strings In Pi
January 4, 2019
[ I worked half-time during December, still suffering fatigue, then during the Christmas break I visited my daughter in Houston; I’m back at work full-time now. Thanks to all for your good wishes. ]
Numberphile has a short episode about self-locating strings in π; for instance, if you ignore the part before the decimal point and number the digits of π starting from zero, the sixth digit of π is 6 and the two digits starting at the 27th digit are 27.
Your task is to write a program that finds self-locating strings in π (A064810). When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
I’m glad your getting better, back at work, and had family time at Christmas.
I came across your website because mine’s called https://www.praxismachinecodinglessons.com
Adapting your coding challenges to developing curriculum for high school computer science teachers and students will be a main theme of mine in 2019.
I’ll keep you posted,
Peter L.
________________________________
Hi Phil, welcome back.
I cheated a little with this exercise and downloaded 1 million digits from the internet. Results were the same as from @programmingpraxis. Running time is about 4 seconds.
$ date && awk ‘END { for (i = 0; i <= 999999; i++) { len = log(i)/log(10); len = int(len) + 1; str = substr($0,i+1,len); if (str ==i) print str } }’ value_of_pi.txt && date
Fri, Jan 4, 2019 10:32:49 AM
6
27
13598
43611
Fri, Jan 4, 2019 10:32:50 AM
AWK version
test
Welcome back @programmingpraxis, and Hacky New Year to all.
For the digits of pi, I cheated and used Simon Tatham’s excellent
‘spigot’ program to do the real work, so my main code isn’t much worth
posting. However, apropos the digits bug you mention, here’s the
simple implementation in R7RS Scheme and SRFI 8 (for receive) from my
homegrown utility library.
Great to have you back again! Hope recovery continues to go well.
Interesting problem, never really looked at generation of digits of pi before. The Gibbons spigot is neat but seems to slow down drastically as we get further into the number – lots of very large numbers in the internal state that presumably are needed for later digits. The Machin formula is neat as we can just sum lot of reciprocals to get our approximation, using some sort of fixed-point representation. I tried some of the “optimal” Machin formulas, but there wasn’t an enormous speedup that I saw (maybe 20-30%) so stuck with the original 1706 method: