Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #3117

Re: Seaching in Pi a sequence. Looking for a faster method

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!newspump.sol.net!posts.news.twtelecom.net!nnrp3.twtelecom.net!not-for-mail
From Anthony Hodsdon <ajhodsd@hotmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Seaching in Pi a sequence. Looking for a faster method
Date Thu, 16 Jun 2011 08:00:50 +0000 (UTC)
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Sender steve@smc.vnet.net
Approved Steven M. Christensen <steve@smc.vnet.net>, Moderator
Message-ID <itcd7i$ct2$1@smc.vnet.net> (permalink)
References <201106151119.HAA22777@smc.vnet.net>
Lines 130
NNTP-Posting-Date 16 Jun 2011 07:02:11 GMT
NNTP-Posting-Host c9e8f384.news.twtelecom.net
X-Trace DXC=RYog?MR]e_ee?MSbgY:QQnC_A=>8kQj6m=_1NR_H?JPmD4>5N:m;K;oEFiONJ7[GofAoP=DEL>^fo
X-Complaints-To abuse@twtelecom.net
Xref x330-a1.tempe.blueboxinc.net comp.soft-sys.math.mathematica:3117

Show key headers only | View raw


Very interesting! I'm surprised that the regular expression path is actually
faster than the simple string. I guess Mathematica doesn't bother to
preprocess the string to use a more efficient searching algorithm like KMP
or Boyer-Moore.

It looks like you get the same optimization by making a regular expression
out of the explicit string:

In[91]:= piesimoSPlus[m_String] := 
 First /@ StringPosition[pi, RegularExpression[m]]

In[92]:= piesimoSPlus["9999999"] // Timing

Out[92]= {0.234, {1722776, 3389380, 4313727, 5466169}}

In[82]:= piesimoS["9999999"] // Timing

Out[82]= {0.53, {1722776, 3389380, 4313727, 5466169}}

In[84]:= DigitSequence[pi, 9, 7] // Timing

Out[84]= {0.28, {{7, 1722776}, {7, 3389380}, {7, 4313727}, {7, 
   5466169}}}

--Anthony

-----Original Message-----
From: Dana DeLouis [mailto:dana.del@gmail.com] 
Sent: Wednesday, June 15, 2011 4:20 AM
Subject: Re: Seaching in Pi a sequence. Looking for a faster
method

Hi.  Just something a little different.
Given piesimo[10^7, 9, 7], You are looking for a specific digit (9) repeated
a number of times (7).

Here's what we have so far.

Our String:
pi=StringDrop[ToString[N[Pi,10^7]],2];

Function:
piesimoS[m_String]:=First/@StringPosition[pi,m]

Check Timing:

piesimoS["9999999"]//Timing
{0.2513,{1722776,3389380,4313727,5466169}}

This idea finds all sequences of the digit 9 repeated 7 or more times.

DigitSequence[pi,9,7]//Timing
{0.08725,{{7,1722776},{7,3389380},{7,4313727},{7,5466169}}}

It appears to be about 3 times faster.
That code returned the length of the sequence, and the starting position.

(* s - String, d - digit to check, start - Minimum length *)

DigitSequence[s_,d_,start_]:=Module[
{re,z,t},
z=StringReplace["n{s,}",{"n"->ToString[d],"s"->ToString[start]}];
re=RegularExpression[z];
t=StringPosition[s,re,Overlaps->False];
t=t/.{x_Integer,y_Integer}:>{y-x+1,x};
SortBy[SortBy[t,Last],First]
]


If you only wanted a specific length, then change rule to "n{s,s}"

If you didn't know how many consecutive 9's there are in a string, then this
finds 6 or more:

DigitSequence[pi,9,6]//Timing
{0.08989,
{{6,762},{6,193034},{6,1985813},{6,2878443},{6,3062881},
{6,3529731},{6,6951812},{6,7298585},{6,8498459},{7,1722776},
{7,3389380},{7,4313727},{7,5466169}}}

So, the digit 9 occurs at most 7 times in a row.

This is more in line with your specific length example.
This does not look at overlap:

DigitSequence2[s_,d_,length_]:=Module[
{z,t},
z=StringReplace["n{s,s}",{"n"->ToString[d],"s"->ToString[length]}];
t=StringPosition[s,RegularExpression[z],Overlaps->False];
t/.{x_Integer,y_Integer}:>x
]

DigitSequence2[pi,9,7]//Timing
{0.08325,{1722776,3389380,4313727,5466169}}

= = = = = = = = = =
HTH  : >)
Dana DeLouis
$Version
8.0 for Mac OS X x86 (64-bit) (November 6, 2010)




On Jun 10, 6:38 am, Guillermo Sanchez <guillermo.sanc...@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



Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Seaching in Pi a sequence. Looking for a faster method Anthony Hodsdon <ajhodsd@hotmail.com> - 2011-06-16 08:00 +0000

csiph-web