Re: Seaching in Pi a sequence. Looking for a faster method
- To: mathgroup at smc.vnet.net
- Subject: [mg119606] Re: Seaching in Pi a sequence. Looking for a faster method
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Sun, 12 Jun 2011 05:07:13 -0400 (EDT)
> searching for 9999998 didn't return correct results until I added the
> _Integer qualifier to the start and end rules.
That's because exactly two matches were found:
piStringSearch[n_, s_, c_] :=
StringPosition[ToString@N[Pi, n], ToString@s, c]
piStringSearch[10^7, "9999998", 100]
% /. {start_, end_} :> start - 2
{{764, 770}, {3062883, 3062889}}
{762, 768}
Do this instead:
piStringSearch[n_, s_, c_] := -2 +
StringPosition[ToString@N[Pi, n], ToString@s, c][[All, 1]]
piStringSearch[10^7, "9999998", 100]
{762, 3062881}
That still fails if NO matches are found, so this is safer than either
method above:
Clear[piStringSearch]
piStringSearch[n_, s_, c_] :=
Replace[StringPosition[ToString@N[Pi, n], ToString@s,
c], {start_, end_} :> start - 2, 1]
piStringSearch[10^7, "9999998", 100]
{762, 3062881}
piStringSearch[10^7, "9999999", 100]
{1722776, 3389380, 4313727, 5466169}
Table[piStringSearch[10^7, "000000" <> ToString@i, 100], {i, 0, 9}]
{{3794572}, {3794573}, {2609392,
7257528}, {}, {}, {1699927}, {}, {}, {2328783}, {}}
piStringSearch[10^7, "000000", 100]
{1699927, 2328783, 2609392, 3794572, 3794573, 7257528}
What is the "leading zeros problem"?
Bobby
On Sat, 11 Jun 2011 02:59:35 -0500, Phil J Taylor <xptaylor at gmail.com>
wrote:
> I made a couple of minor modifications after noticing problems with
> Feynman's Point and patterns that begin with zeros. I'm not sure why, but
> searching for 9999998 didn't return correct results until I added the
> _Integer qualifier to the start and end rules. I haven't fixed the
> leading
> zeros problem. Just put the pattern in quotes.
>
> (* loosely validated at http://www.angio.net/pi/bigpi.cgi *)
>
> piStringSearch[n_, s_, c_] :=
> Module[{piString = ToString[N[Pi, n]]},
> StringPosition[piString, ToString[s],
> c] /. {start_Integer, end_Integer} :> start - 2]
>
> piStringSearch[10^5, "00000", 100]
>
> {17534}
>
> On Fri, Jun 10, 2011 at 2:45 PM, Phil J Taylor <xptaylor at gmail.com>
> wrote:
>
>> This works well for me ... it's about 3x faster than piesimo on my
>> machine
>> and I can search for any sequence.
>> The final argument c is the number of matches that you are interested in
>> seeing.
>>
>> piStringSearch[n_, s_, c_] :=
>> Module[{a}, a = ToString[N[Pi, n]];
>> StringPosition[a, ToString[s], c] /. {start_, end_} :> start - 2]
>>
>> piStringSearch[10^7, 9999999, 100]
>>
>> {1722776, 3389380, 4313727, 5466169}
>>
>> On Fri, Jun 10, 2011 at 6:38 AM, Guillermo Sanchez <
>> guillermo.sanchez at hotmail.com> wrote:
>>
>>> Dear Group
>>>
>>> I have developed this function
>>>
>>> piesimo[n_, m_, r_] := Module[{a}, a = Split[RealDigits[Pi - 3, 10, n]
>>> [[1]]]; Part[Accumulate[Length /@ a], Flatten[Position[a, Table[m,
>>> {r}]]] - 1] + 1]
>>>
>>> n is the digits of Pi, after 3, where to search a sequence of m digit
>>> r times consecutives.
>>> For instance:
>>>
>>> piesimo[10^7, 9, 7]
>>>
>>> Gives that the sequence 9999999 happens in positions:
>>>
>>> {1722776, 3389380, 4313727, 5466169}
>>>
>>> I know that in this group I will find faster methods. Any idea?
>>>
>>> Guillermo
>>>
>>>
>>>
>>
--
DrMajorBob at yahoo.com